update 优化 xss 处理器 从gateway获取转发路径

dev
疯狂的狮子Li 1 year ago
parent 5aa96c5b02
commit 96245cf82d

@ -3,6 +3,7 @@ package org.dromara.common.web.filter;
import jakarta.servlet.*; import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.dromara.common.core.utils.ServletUtils;
import org.dromara.common.core.utils.SpringUtils; 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.dromara.common.web.config.properties.XssProperties;
@ -26,12 +27,13 @@ public class XssFilter implements Filter {
@Override @Override
public void init(FilterConfig filterConfig) throws ServletException { public void init(FilterConfig filterConfig) throws ServletException {
XssProperties properties = SpringUtils.getBean(XssProperties.class); XssProperties properties = SpringUtils.getBean(XssProperties.class);
String appName = SpringUtils.getApplicationName(); HttpServletRequest request = ServletUtils.getRequest();
String appPath = "/" + StringUtils.substring(appName, appName.indexOf("-") + 1); // 从请求头获取gateway转发的服务前缀
String prefix = StringUtils.blankToDefault(request.getHeader("X-Forwarded-Prefix"), "");
List<String> excludeUrls = properties.getExcludeUrls() List<String> excludeUrls = properties.getExcludeUrls()
.stream() .stream()
.filter(x -> StringUtils.startsWith(x, appPath)) .filter(x -> StringUtils.startsWith(x, prefix))
.map(x -> x.replaceFirst(appPath, StringUtils.EMPTY)) .map(x -> x.replaceFirst(prefix, StringUtils.EMPTY))
.toList(); .toList();
excludes.addAll(excludeUrls); excludes.addAll(excludeUrls);
} }

Loading…
Cancel
Save