spring mvc
1.防止Xss注入攻击
@InitBinder public void initBinder(WebDataBinder binder) { /** 防止XSS攻击 */ binder.registerCustomEditor(String.class, new StringEscapeEditor(true, false)); /** 日期转换 */ binder.registerCustomEditor(Date.class, new DateEditor()); }
StringEscapeEditor
@Override public void setAsText(String text) throws IllegalArgumentException { if (text == null) { setValue(null); } else { String value = text; if (escapeHTML) { value = HtmlUtils.htmlEscape(value); } if (escapeJavaScript) { value = JavaScriptUtils.javaScriptEscape(value); } setValue(value); } }
controller代码:
@RequestMapping("/test2/{user}/{password}") @ResponseBody public String getUser(@PathVariable("user")String user,@PathVariable("password")String password){ System.out.println(user); System.out.println(password); return "成功调用资源!"; }
1.对html的转码
请求链接为
http://localhost:8080/publish/api/test2//er
如果不配置这个,返回结果为:
er
如果配置,返回结果为:
<input>er
这样就完成了对html特殊标签的转码