|
|
|
|
@ -2,27 +2,18 @@ package cn.iocoder.yudao.framework.web.config;
|
|
|
|
|
|
|
|
|
|
import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
|
|
|
|
|
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.clean.JsoupXssCleaner;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.clean.XssCleaner;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.filter.CacheRequestBodyFilter;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.filter.DemoFilter;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.filter.XssFilter;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.handler.GlobalResponseBodyHandler;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.json.XssStringJsonDeserializer;
|
|
|
|
|
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
|
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
|
|
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
|
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
|
import org.springframework.util.AntPathMatcher;
|
|
|
|
|
import org.springframework.util.PathMatcher;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
import org.springframework.web.cors.CorsConfiguration;
|
|
|
|
|
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
|
|
@ -34,7 +25,7 @@ import javax.annotation.Resource;
|
|
|
|
|
import javax.servlet.Filter;
|
|
|
|
|
|
|
|
|
|
@AutoConfiguration
|
|
|
|
|
@EnableConfigurationProperties({WebProperties.class, XssProperties.class})
|
|
|
|
|
@EnableConfigurationProperties(WebProperties.class)
|
|
|
|
|
public class YudaoWebAutoConfiguration implements WebMvcConfigurer {
|
|
|
|
|
|
|
|
|
|
@Resource
|
|
|
|
|
@ -107,15 +98,6 @@ public class YudaoWebAutoConfiguration implements WebMvcConfigurer {
|
|
|
|
|
return createFilterBean(new CacheRequestBodyFilter(), WebFilterOrderEnum.REQUEST_BODY_CACHE_FILTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建 XssFilter Bean,解决 Xss 安全问题
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnBean(XssCleaner.class)
|
|
|
|
|
public FilterRegistrationBean<XssFilter> xssFilter(XssProperties properties, PathMatcher pathMatcher, XssCleaner xssCleaner) {
|
|
|
|
|
return createFilterBean(new XssFilter(properties, pathMatcher, xssCleaner), WebFilterOrderEnum.XSS_FILTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建 DemoFilter Bean,演示模式
|
|
|
|
|
*/
|
|
|
|
|
@ -125,33 +107,7 @@ public class YudaoWebAutoConfiguration implements WebMvcConfigurer {
|
|
|
|
|
return createFilterBean(new DemoFilter(), WebFilterOrderEnum.DEMO_FILTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Xss 清理者
|
|
|
|
|
*
|
|
|
|
|
* @return XssCleaner
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean(XssCleaner.class)
|
|
|
|
|
public XssCleaner xssCleaner() {
|
|
|
|
|
return new JsoupXssCleaner();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注册 Jackson 的序列化器,用于处理 json 类型参数的 xss 过滤
|
|
|
|
|
*
|
|
|
|
|
* @return Jackson2ObjectMapperBuilderCustomizer
|
|
|
|
|
*/
|
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean(name = "xssJacksonCustomizer")
|
|
|
|
|
@ConditionalOnBean(ObjectMapper.class)
|
|
|
|
|
@ConditionalOnProperty(value = "yudao.xss.enable", havingValue = "true")
|
|
|
|
|
public Jackson2ObjectMapperBuilderCustomizer xssJacksonCustomizer(XssCleaner xssCleaner) {
|
|
|
|
|
// 在反序列化时进行 xss 过滤,可以替换使用 XssStringJsonSerializer,在序列化时进行处理
|
|
|
|
|
return builder -> builder.deserializerByType(String.class, new XssStringJsonDeserializer(xssCleaner));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
|
|
|
|
|
public static <T extends Filter> FilterRegistrationBean<T> createFilterBean(T filter, Integer order) {
|
|
|
|
|
FilterRegistrationBean<T> bean = new FilterRegistrationBean<>(filter);
|
|
|
|
|
bean.setOrder(order);
|
|
|
|
|
return bean;
|
|
|
|
|
|