今天在工作中发现自己开发的系统存在XSS漏洞,特写此文章记录解决方案,话不多说截止开车。
防止XSS攻击我们需要做的除了在硬件层面(防火墙、IP白名单)验证外,后端代码也需要做相应的处理,最简单直接的方式添加Filter过滤器。
/**
* @Project : codeyi-web
* @Package Name : com.codeyi.common.xss
* @Company
* @Author Codeyi on 2019-9-11.
* @Creation Date: 2019年09月11日 13:32
* @Description :描述
*/
public class RequestXssFilter implements Filter {
FilterConfig filterConfig = null;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
filterChain.doFilter(new XssHttpServletRequestWrapper(
(HttpServletRequest) servletRequest), servletResponse);
}
@Override
public void destroy () {
this.filterConfig = null;
}
}
实际过滤类
/**
* @Project : codeyi-web
* @Package Name : com.codeyi.common.xss
* @Company
* @Author Codeyi on 2019-9-11.
* @Creation Date: 2019年09月11日 13:34
* @Description :描述
*/
public class XssHttpServletReq