跨域资源共享漏洞怎么修复 跨域资源共享错误

主机教程 建站分享 2年前 (2022-12-07) 268次浏览

文章摘要:跨域资源共享漏洞怎么修复 跨域资源共享错误

修复跨域资源共享漏洞的方法 修复代码如下: public class RefererFilter implem […]

修复跨域资源共享漏洞的方法

修复代码如下:

public class RefererFilter implements Filter {

private static Logger logger = LoggerFactory.getLogger(RefererFilter.class);

private final String ORIGIN = "Origin";

private final String REFERER = "referer";

/** 允许访问的域名列表 */

private List allowDomainList = new ArrayList<>();

/** 过滤器忽略处理的url规则 */

private List excludes = new ArrayList<>();

@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

if (logger.isDebugEnabled()) {

logger.debug("referer filter is open");

}

// 判断该url是否需要过滤

HttpServletRequest req = (HttpServletRequest) request;

HttpServletResponse resp = (HttpServletResponse) response;

if (handleExcludeURL(req, resp)) {

chain.doFilter(request, response);

return;

}

if (null != allowDomainList && !allowDomainList.isEmpty()) {

logger.info("referer过滤");

String origin = req.getHeader(ORIGIN);

// 获取referer域

String referer = getRefererDomain(req.getHeader(REFERER));

logger.info("origin={}, referer={}", origin, referer);

if (origin == null || (allowDomainList.contains(origin) && (allowDomainList.contains(referer)))) {

// 有值,就继续执行下一个过滤链

chain.doFilter(request, response);

} else {

// 服务器拒绝

resp.setStatus(HttpStatus.FORBIDDEN.value());

}

} else {

chain.doFilter(request, response);

}

}

@Override

public void init(FilterConfig filterConfig) throws ServletException {

if (logger.isDebugEnabled()) {

logger.debug("referer filter init ====================");

}

String excludesTemp = filterConfig.getInitParameter("excludes");

if (excludesTemp != null) {

String[] url = excludesTemp.split(",");

for (int i = 0; url != null && i < url.length; i++) {

excludes.add(url[i]);

}

}

logger.info("excludes={}", excludes);

String allowDomainListTemp = filterConfig.getInitParameter("allowDomainList");

if (allowDomainListTemp != null) {

String[] url = allowDomainListTemp.split(",");

for (int i = 0; url != null && i < url.length; i++) {

allowDomainList.add(url[i]);

}

}

logger.info("allowDomainList={}", allowDomainList);

}

/**

* @param request

* @param response

* @return

*/

private boolean handleExcludeURL(HttpServletRequest request, HttpServletResponse response) {

if (excludes == null || excludes.isEmpty()) {

return false;

}

String url = request.getServletPath();

logger.info("校验{}是否需要referer过滤。", url);

for (String pattern : excludes) {

Pattern p = Pattern.compile("^" + pattern);

Matcher m = p.matcher(url);

if (m.find()) {

return true;

}

}

return false;

}

/**

* 获取referer域名

*

* @param refererUrl

* @return

*/

private static String getRefererDomain(String referer) {

String result = referer;

if (StringUtils.isNotBlank(referer)) {

if (referer.startsWith("https://")) {

int i = referer.substring(8).indexOf("/");

if (i > 0) {

result = referer.substring(0, 8 + i);

}

} else if (referer.startsWith("http://")) {

int i = referer.substring(7).indexOf("/");

if (i > 0) {

result = referer.substring(0, 7 + i);

}

}

}

return result;

}

}


声明:
若非注明,本站文章源于互联网收集整理和网友分享发布,如有侵权,请联系站长处理。
文章名称:跨域资源共享漏洞怎么修复 跨域资源共享错误
文章链接:http://www.7966.org/post/14923.html
转载请注明出处

喜欢 (0)