XSS攻击全称跨站脚本攻击,是为不和层叠样式表(Cascading Style Sheets, CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS,XSS是一种在web应用中的计算机安全漏洞,它允许恶意web用户将代码植入到提供给其它用户使用的页面中。
简而言之,就是作恶用户通过表单提交一些前端代码,如果不做处理的话,这些前端代码将会在展示的时候被浏览器执行。
解决XSS攻击,可以通过后端对输入的数据做过滤或者转义,使XSS攻击代码失效。
对于过滤XSS脚本的代码,通过搜索引擎可以搜索到很多,但似乎都不是那么全面。基本上都是只能过滤querystring(表单类型)类型的入参,而不能过滤json类型的入参。其实,在现在的开发中,更多的是使用json类型做数据交互。下面就直接贴代码了:
这里重写了两个方法:getParameter和getParameterValues,getParameter方法是直接通过request获得querystring类型的入参调用的方法。如果是通过springMVC注解类型来获得参数的话,走的是getParameterValues的方法。大家可以通过打印一个输出来验证一下。
StringEscapeUtilsescapeHtml4这个方法来自Apache的工具类,maven坐标如下:
过滤的代码写完了,下面就是在一个filter中应用该代码。
过滤表单类型的代码已经完成(xssObjectMapper这个是后面过滤json类型才用到的)。下面来实现过滤json类型的代码:
代码如下:
这里是通过修改SpringMVC的json序列化来达到过滤xss的目的的。其实也可以通过第一种方法,重写getInputStream方法来实现,这里我就不做演示了(通过json类型传参会走getInputStream方法,通过重写该方法打印输出可以证明)。
TestControllerjava
下面通过postman测试下效果:
可以看到,js代码已经经过转义。转义过后的代码,即使前端读取过去了,也不会被浏览器执行的。
1以前的struts等框架URL映射都是写在XML当中的,通过URL查找对应的JAVA类比较方便,如registdo,直接查struts-configxml就可以了,但是使用注解方式的话,只能通过查找JAVA文件关键字来定位对应的controller类,这样极其不方便,难道只能通过约定来解决这个问题吗?那就削弱了spring mvc映射配置的灵活性了。
使用@RequestMapping注解时,配置的信息最后都设置到了RequestMappingInfo中
RequestMappingInfo封装了PatternsRequestCondition,RequestMethodsRequestCondition,ParamsRequestCondition等,所以自己不干活,所有的活都是委托给具体的condition处理
先看下封装的RequestCondition吧,之前的文章将的比较细了,不清楚各个类具体是做什么的,可以移步这里<SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系>
1 package orgspringframeworkwebservletmvcmethod;
2 public final class RequestMappingInfo implements RequestCondition<RequestMappingInfo> {
3
4 private final PatternsRequestCondition patternsCondition;
5
6 private final RequestMethodsRequestCondition methodsCondition;
7
8 private final ParamsRequestCondition paramsCondition;
9
10 private final HeadersRequestCondition headersCondition;
11
12 private final ConsumesRequestCondition consumesCondition;
13
14 private final ProducesRequestCondition producesCondition;
15
16 private final RequestConditionHolder customConditionHolder;
17 }
初始化没什么好看的,直接看接口的实现吧
貌似combine也没啥料,就是分别委托
1 /
2 Combines "this" request mapping info (ie the current instance) with another request mapping info instance
3 <p>Example: combine type- and method-level request mappings
4 @return a new request mapping info instance; never {@code null}
5 /
6 public RequestMappingInfo combine(RequestMappingInfo other) {
7 PatternsRequestCondition patterns = thispatternsConditioncombine(otherpatternsCondition);
8 RequestMethodsRequestCondition methods = thismethodsConditioncombine(othermethodsCondition);
9 ParamsRequestCondition params = thisparamsConditioncombine(otherparamsCondition);
10 HeadersRequestCondition headers = thisheadersConditioncombine(otherheadersCondition);
11 ConsumesRequestCondition consumes = thisconsumesConditioncombine(otherconsumesCondition);
12 ProducesRequestCondition produces = thisproducesConditioncombine(otherproducesCondition);
13 RequestConditionHolder custom = thiscustomConditionHoldercombine(othercustomConditionHolder);
14
15 return new RequestMappingInfo(patterns, methods, params, headers, consumes, produces, customgetCondition());
16 }
getMatchingCondition只是体现出可能基于性能消耗的考虑,把PatternsRequestCondition和RequestConditionHolder的比较放到后面单独处理了
1 /
2 Checks if all conditions in this request mapping info match the provided request and returns
3 a potentially new request mapping info with conditions tailored to the current request
4 <p>For example the returned instance may contain the subset of URL patterns that match to
5 the current request, sorted with best matching patterns on top
6 @return a new instance in case all conditions match; or {@code null} otherwise
7 /
8 public RequestMappingInfo getMatchingCondition(>
1通过>@RequestMapping(value="/index1")
public String helloaction1(>}
2通过参数名获得:
@RequestMapping(value="/index1")public String helloaction1(String nnn){ //这里名字要与前端元素名字一致才能获得
Systemoutprintln(nnn);
return "index";
}
3通过@RequestParam注解获得:
@RequestMapping(value="/index")public String helloaction(@RequestParam(value="nnn",required=false)String nnn1, Model model){ //nnn要与前端一致,在此处可以理解为参数nnn1的别名
Systemoutprintln(nnn1);
modeladdAttribute("hello", "这是用action传过来的值:"+nnn1);
return "index";
}
4SpringMvc还能通过将vo作为参数获得vo的各个属性:
@RequestMapping(value="/index2")public String helloaction2(User user){
Systemoutprintln(usergetAccount());
Systemoutprintln(usergetPassword());
return "index";
}
以上就是关于【快学springboot】15、SpringBoot过滤XSS脚本攻击全部的内容,包括:【快学springboot】15、SpringBoot过滤XSS脚本攻击、请教spring mvc 3如何通过URL快速定位使用注解的controller类、springmvc中的requestmapping注解怎么实现的等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)