您现在的位置是:首页 > 文章详情

spring-boot-plus 1.3.1 发布,XSS-CORS-CodeGenerator 优化

日期:2019-10-15点击:462

[V1.3.1-RELEASE] 2019.10.15

⭐️ New Features

  • Xss跨站脚本工具处理
  • CORS跨域配置

⚡️ Optimization

  • 代码生成器可自定义配置生成哪些文件
  • 请求路径filter配置,配置文件属性名称调整
  • Aop切点优化,Aop JSON参数输出优化
  • 可配置是否生成Validation验证代码
  • 优化controller,entity模版生成
  • 优化代码生成器 CodeGenerator
  • 调整 aopfilter,interceptor,controller,param,vo代码目录结构

📝 Added/Modified

  • Add XssFilter,XssHttpServletRequestWrapper,XssJacksonDeserializer,XssJacksonSerializer
  • Add SpringBootPlusCorsProperties
  • Update JacksonConfig
  • Update LogAop,RequestPathFilter,ShiroConfig

🐞 Bug Fixes

  • fix druid控制面板无法访问问题

📔 Documentation

🔨 Dependency Upgrades

  • Upgrade to spring-boot 2.1.9.RELEASE
  • Upgrade to Fastjson 1.2.62
  • Upgrade to hutool 4.6.10
  • Add commons-text 1.8

 

CORS跨域处理

CORS:Cross-Origin Resource Sharing

  • CORS是一种允许当前域(domain)的资源(比如html/js/web service)被其他域(domain)的脚本请求访问的机制,通常由于同域安全策略(the same-origin security policy)浏览器会禁止这种跨域请求。

处理方法

  • 后台设置允许的请求源/请求头等信息

后台配置

CorsFilter Bean配置

使用 Spring 提供的 CorsFilter 过滤器实现跨域配置

  • io.geekidea.springbootplus.core.config.SpringBootPlusConfig
 /** * CORS跨域设置 * * @return */ @Bean public FilterRegistrationBean corsFilter(SpringBootPlusCorsProperties corsProperties) { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration corsConfiguration = new CorsConfiguration(); // 跨域配置 corsConfiguration.setAllowedOrigins(corsProperties.getAllowedOrigins()); corsConfiguration.setAllowedHeaders(corsProperties.getAllowedHeaders()); corsConfiguration.setAllowedMethods(corsProperties.getAllowedMethods()); corsConfiguration.setAllowCredentials(corsProperties.isAllowCredentials()); corsConfiguration.setExposedHeaders(corsProperties.getExposedHeaders()); source.registerCorsConfiguration(corsProperties.getPath(), corsConfiguration); FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source)); bean.setOrder(Ordered.HIGHEST_PRECEDENCE); bean.setEnabled(corsProperties.isEnable()); return bean; } 

配置文件

配置文件类:io.geekidea.springbootplus.core.properties.SpringBootPlusCorsProperties

  • application.yml
 spring-boot-plus: ############################ CORS start ############################ # CORS跨域配置,默认允许跨域 cors: # 是否启用跨域,默认启用 enable: true # CORS过滤的路径,默认:/** path: /** # 允许访问的源 allowed-origins: '*' # 允许访问的请求头 allowed-headers: x-requested-with,content-type,token # 是否允许发送cookie allow-credentials: true # 允许访问的请求方式 allowed-methods: OPTION,GET,POST # 允许响应的头 exposed-headers: token # 该响应的有效时间默认为30分钟,在有效时间内,浏览器无须为同一请求再次发起预检请求 max-age: 1800 ############################ CORS end ############################## 

参考

 

XSS跨站脚本攻击处理

XSS:Cross Site Scripting

  • 跨站脚本攻击(XSS),是目前最普遍的Web应用安全漏洞。这类漏洞能够使得攻击者嵌入恶意脚本代码到正常用户会访问到的页面中,当正常用户访问该页面时,则可导致嵌入的恶意脚本代码的执行,从而达到恶意攻击用户的目的。

处理方法

将参数中的特殊字符进行转换

  • 例如 input参数值,用户输入为:
 <script>alert(1);</script> 
  • 处理后为:
 &lt;script&gt;alert(1);&lt;/script&gt; 

后台处理

pom.xml依赖

使用 commons-text包中的StringEscapeUtils.escapeHtml4();方法

 <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-text</artifactId> <version>1.8</version> </dependency> 

XssHttpServletRequestWrapper

HttpServletRequest 对象的请求参数进行处理

 public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper { public XssHttpServletRequestWrapper(HttpServletRequest request) { super(request); } @Override public String getQueryString() { String value = super.getQueryString(); return StringEscapeUtils.escapeHtml4(value); } @Override public String getParameter(String name) { String value = super.getParameter(name); return StringEscapeUtils.escapeHtml4(value); } @Override public String[] getParameterValues(String name) { String[] values = super.getParameterValues(name); if (ArrayUtils.isEmpty(values)) { return values; } int length = values.length; String[] escapeValues = new String[length]; for (int i = 0; i < length; i++) { String value = values[i]; escapeValues[i] = StringEscapeUtils.escapeHtml4(value); } return escapeValues; } } 

XssFilter

使用WebFilter注解,拦截所有请求,过滤请求参数

 @Slf4j @WebFilter(filterName = "xssFilter", urlPatterns = "/*", asyncSupported = true) public class XssFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; XssHttpServletRequestWrapper xssHttpServletRequestWrapper = new XssHttpServletRequestWrapper(request); filterChain.doFilter(xssHttpServletRequestWrapper, servletResponse); } } 

启动类添加@ServletComponentScan注解

扫描使用servlet注解的类,启用 XssFilter

 @ServletComponentScan 

JSON字符串请求参数处理

实现Jackson反序列化方法,将参数值转义处理

 public class XssJacksonDeserializer extends JsonDeserializer<String> { @Override public String deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { return StringEscapeUtils.escapeHtml4(jsonParser.getText()); } } 

JSON字符串响应结果处理

实现Jackson序列化方法,将参数值转义处理

 @Slf4j public class XssJacksonSerializer extends JsonSerializer<String> { @Override public void serialize(String s, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(StringEscapeUtils.escapeHtml4(s)); } } 

重点,Jackson配置Xss

 @Configuration public class JacksonConfig implements WebMvcConfigurer { @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { // code... // XSS序列化 simpleModule.addSerializer(String.class, new XssJacksonSerializer()); simpleModule.addDeserializer(String.class, new XssJacksonDeserializer()); // code... } } 

总结

实现字符串转义的核心方法:

  • org.apache.commons.text.StringEscapeUtils
 StringEscapeUtils.escapeHtml4();
原文链接:https://www.oschina.net/news/110574/spring-boot-plus-1-3-1-released
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章