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

栏目: 软件资讯 · 发布时间: 5年前

内容简介:[V1.3.1-RELEASE] 2019.10.15 :star:️ New Features Xss跨站脚本工具处理 CORS跨域配置 :zap:️ Optimization 代码生成器可自定义配置生成哪些文件 请求路径filter配置,配置文件属性名称调整 Aop切点优化,Aop...

[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();

以上所述就是小编给大家介绍的《spring-boot-plus 1.3.1 发布,XSS-CORS-CodeGenerator 优化》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

A Philosophy of Software Design

A Philosophy of Software Design

John Ousterhout / Yaknyam Press / 2018-4-6 / GBP 14.21

This book addresses the topic of software design: how to decompose complex software systems into modules (such as classes and methods) that can be implemented relatively independently. The book first ......一起来看看 《A Philosophy of Software Design》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具