4
Cross Site Scripting Cheat Sheet有許多防範XSS攻擊的規則。我想在使用Spring MVC + Jackson + JPA + Hibernate Bean驗證的Web應用程序中實現這些建議。作爲一個例子,請考慮下面的代碼,它類似於我在我的應用程序中的代碼。如何用SpringMVC + Jackson應用程序防止XSS攻擊?
public class MessageJson {
@NotEmpty // Bean Validation annotation
private String title;
@NotEmpty
private String body;
// ... etc getters/setters
}
public class BolgPostController
{
@RequestMapping(value="messages",method=RequestMethod.POST)
public void createMessage(@Valid @RequestBody MessageJson message)
{
// **Question** How do I check that the message title and body don't contain
// nasty javascripts and other junk that should not be there?
// Call services to write data to the datababse
}
@RequestMapping(value="messages",method=RequestMethod.get)
public @ResponseBody List<MessageJson> createMessage()
{
// get data from the database
// **Question** How do I escape all the data in the list of MessageJson before
I send it back to the data.
}
}
我可以看到下面的方式來實現備忘單規則:
- 選項A在每個控制器方法手動實現它們。
- 選項B配置一些擴展Spring MVC的,可以自動爲我做
- 選項C配置傑克遜,以便它可以因爲我的大部分輸入的爲我做的/輸出通過傑克遜
我找的用SpringMVC任何這些三個選項的一些示例配置,與選項B和C的偏好
這是最簡單的解決方案,我使用它與http://owasp-java-html-sanitizer.googlecode.com它工作得很好。 – ams