Spring mvc 如何使用SpringMVC+;杰克逊申请?
有许多针对XSS攻击的安全规则。我想在使用SpringMVC+Jackson+JPA+HibernateBean验证的web应用程序中实现这些建议。作为一个例子,考虑下面的代码,与我的应用程序中的代码类似。Spring mvc 如何使用SpringMVC+;杰克逊申请?,spring-mvc,xss,security,jackson,Spring Mvc,Xss,Security,Jackson,有许多针对XSS攻击的安全规则。我想在使用SpringMVC+Jackson+JPA+HibernateBean验证的web应用程序中实现这些建议。作为一个例子,考虑下面的代码,与我的应用程序中的代码类似。 public class MessageJson { @NotEmpty // Bean Validation annotation private String title; @NotEmpty private String body;
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.
}
}
public类MessageJson{
@NotEmpty//Bean验证注释
私有字符串标题;
@空空如也
私有字符串体;
//…etc接受者/接受者
}
公共类BolgPostController
{
@RequestMapping(value=“messages”,method=RequestMethod.POST)
public void createMessage(@Valid@RequestBody MessageJson message)
{
//**问题**如何检查邮件标题和正文不包含
//讨厌的Java脚本和其他不应该存在的垃圾?
//调用服务将数据写入数据库
}
@RequestMapping(value=“messages”,method=RequestMethod.get)
public@ResponseBody List createMessage()
{
//从数据库中获取数据
//**问题**如何转义MessageJson列表中的所有数据
我把它发回数据。
}
}
我可以看到以下方法来实施备忘单规则:
- 选项A在每个控制器方法中手动执行
- 选项B为Spring MVC配置一些扩展,这些扩展可以自动完成
- 选项C配置Jackson,这样它就可以为我做,因为我的大部分输入/输出都通过Jackson完成
我在这三个选项中的任何一个中寻找SpringMVC的一些示例配置,并优先选择选项B和C。这是在读取JSON时在属性设置器(如
setTitle()
fortitle
property)中最容易做到的
或者,如果您正在考虑转义其他字符(例如,防止嵌入HTML标记),请查看以下日志:。这是最简单的解决方案,我正在使用它,它工作得非常好。