update 优化 全局开启xss过滤 提高安全性 与cloud版本保持一致

This commit is contained in:
疯狂的狮子Li 2024-09-13 18:02:44 +08:00
parent e33f76d710
commit fc72b67090
4 changed files with 17 additions and 28 deletions

View File

@ -223,9 +223,10 @@ xss:
# 过滤开关 # 过滤开关
enabled: true enabled: true
# 排除链接(多个用逗号分隔) # 排除链接(多个用逗号分隔)
excludes: /system/notice excludeUrls:
# 匹配链接 - /system/notice
urlPatterns: /system/*,/monitor/*,/tool/* - /workflow/model/save
- /workflow/model/editModelXml
# 全局线程池相关配置 # 全局线程池相关配置
# 如使用JDK21请直接使用虚拟线程 不要开启此配置 # 如使用JDK21请直接使用虚拟线程 不要开启此配置

View File

@ -1,19 +1,15 @@
package org.dromara.common.web.config; package org.dromara.common.web.config;
import org.dromara.common.core.utils.StringUtils; import jakarta.servlet.DispatcherType;
import org.dromara.common.web.config.properties.XssProperties; import org.dromara.common.web.config.properties.XssProperties;
import org.dromara.common.web.filter.RepeatableFilter; import org.dromara.common.web.filter.RepeatableFilter;
import org.dromara.common.web.filter.XssFilter; import org.dromara.common.web.filter.XssFilter;
import jakarta.servlet.DispatcherType;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import java.util.HashMap;
import java.util.Map;
/** /**
* Filter配置 * Filter配置
* *
@ -30,12 +26,9 @@ public class FilterConfig {
FilterRegistrationBean registration = new FilterRegistrationBean(); FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setDispatcherTypes(DispatcherType.REQUEST); registration.setDispatcherTypes(DispatcherType.REQUEST);
registration.setFilter(new XssFilter()); registration.setFilter(new XssFilter());
registration.addUrlPatterns(StringUtils.split(xssProperties.getUrlPatterns(), StringUtils.SEPARATOR)); registration.addUrlPatterns("/*");
registration.setName("xssFilter"); registration.setName("xssFilter");
registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE); registration.setOrder(FilterRegistrationBean.HIGHEST_PRECEDENCE);
Map<String, String> initParameters = new HashMap<>();
initParameters.put("excludes", xssProperties.getExcludes());
registration.setInitParameters(initParameters);
return registration; return registration;
} }

View File

@ -3,6 +3,9 @@ package org.dromara.common.web.config.properties;
import lombok.Data; import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.ArrayList;
import java.util.List;
/** /**
* xss过滤 配置属性 * xss过滤 配置属性
* *
@ -13,18 +16,13 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
public class XssProperties { public class XssProperties {
/** /**
* 过滤开关 * Xss开关
*/ */
private String enabled; private Boolean enabled;
/** /**
* 排除链接多个用逗号分隔 * 排除路径
*/ */
private String excludes; private List<String> excludeUrls = new ArrayList<>();
/**
* 匹配链接
*/
private String urlPatterns;
} }

View File

@ -1,6 +1,8 @@
package org.dromara.common.web.filter; package org.dromara.common.web.filter;
import org.dromara.common.core.utils.SpringUtils;
import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.web.config.properties.XssProperties;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import jakarta.servlet.*; import jakarta.servlet.*;
@ -23,13 +25,8 @@ public class XssFilter implements Filter {
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
String tempExcludes = filterConfig.getInitParameter("excludes"); XssProperties properties = SpringUtils.getBean(XssProperties.class);
if (StringUtils.isNotEmpty(tempExcludes)) { excludes.addAll(properties.getExcludeUrls());
String[] url = tempExcludes.split(StringUtils.SEPARATOR);
for (int i = 0; url != null && i < url.length; i++) {
excludes.add(url[i]);
}
}
} }
@Override @Override