fix 修复 经过加密的请求无法过滤xss问题 将xss实现从gateway移动到common-web解密后过滤
parent
f64b17b548
commit
503a0efc31
@ -0,0 +1,33 @@
|
|||||||
|
package org.dromara.common.web.config;
|
||||||
|
|
||||||
|
import jakarta.servlet.DispatcherType;
|
||||||
|
import org.dromara.common.web.config.properties.XssProperties;
|
||||||
|
import org.dromara.common.web.filter.XssFilter;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Filter配置
|
||||||
|
*
|
||||||
|
* @author Lion Li
|
||||||
|
*/
|
||||||
|
@AutoConfiguration
|
||||||
|
@EnableConfigurationProperties(XssProperties.class)
|
||||||
|
public class FilterConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnProperty(value = "xss.enabled", havingValue = "true")
|
||||||
|
public FilterRegistrationBean<XssFilter> xssFilterRegistration() {
|
||||||
|
FilterRegistrationBean<XssFilter> registration = new FilterRegistrationBean<>();
|
||||||
|
registration.setDispatcherTypes(DispatcherType.REQUEST);
|
||||||
|
registration.setFilter(new XssFilter());
|
||||||
|
registration.addUrlPatterns("/*");
|
||||||
|
registration.setName("xssFilter");
|
||||||
|
registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE + 1);
|
||||||
|
return registration;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
package org.dromara.common.web.filter;
|
||||||
|
|
||||||
|
import jakarta.servlet.*;
|
||||||
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
|
import org.dromara.common.core.utils.SpringUtils;
|
||||||
|
import org.dromara.common.core.utils.StringUtils;
|
||||||
|
import org.dromara.common.web.config.properties.XssProperties;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 防止XSS攻击的过滤器
|
||||||
|
*
|
||||||
|
* @author ruoyi
|
||||||
|
*/
|
||||||
|
public class XssFilter implements Filter {
|
||||||
|
/**
|
||||||
|
* 排除链接
|
||||||
|
*/
|
||||||
|
public List<String> excludes = new ArrayList<>();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
XssProperties properties = SpringUtils.getBean(XssProperties.class);
|
||||||
|
String appName = SpringUtils.getApplicationName();
|
||||||
|
String appPath = "/" + StringUtils.substring(appName, appName.indexOf("-") + 1);
|
||||||
|
List<String> excludeUrls = properties.getExcludeUrls()
|
||||||
|
.stream()
|
||||||
|
.filter(x -> StringUtils.startsWith(x, appPath))
|
||||||
|
.map(x -> x.replaceFirst(appPath, StringUtils.EMPTY))
|
||||||
|
.toList();
|
||||||
|
excludes.addAll(excludeUrls);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
HttpServletRequest req = (HttpServletRequest) request;
|
||||||
|
HttpServletResponse resp = (HttpServletResponse) response;
|
||||||
|
if (handleExcludeURL(req, resp)) {
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper((HttpServletRequest) request);
|
||||||
|
chain.doFilter(xssRequest, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean handleExcludeURL(HttpServletRequest request, HttpServletResponse response) {
|
||||||
|
String url = request.getServletPath();
|
||||||
|
String method = request.getMethod();
|
||||||
|
// GET DELETE 不过滤
|
||||||
|
if (method == null || HttpMethod.GET.matches(method) || HttpMethod.DELETE.matches(method)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return StringUtils.matches(url, excludes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
org.dromara.common.web.config.FilterConfig
|
||||||
org.dromara.common.web.config.I18nConfig
|
org.dromara.common.web.config.I18nConfig
|
||||||
org.dromara.common.web.config.UndertowConfig
|
org.dromara.common.web.config.UndertowConfig
|
||||||
org.dromara.common.web.config.ResourcesConfig
|
org.dromara.common.web.config.ResourcesConfig
|
||||||
Loading…
Reference in New Issue