
后端防范xss攻击的示例:
java后端防止xss攻击可添加Filter过滤器,例如:
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;
}
}
再写一个实际过滤类:
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {//白名单数组
private static final String[] WHITE_LIST = {"content"};
// 定义script的正则表达式
private static final String REGEX_SCRIPT = " jindouyun.cn. All Rights Reserved. 筋斗云 版权所有 | 粤ICP备13013545号 | 增值电信业务经营许可证: 粤B1-20215235 |
粤公网安备 44070302000974号
违法和不良信息举报中心 24 小时违法和不良信息举报热线:4006783389,举报邮箱:jubao@jindouyun.cn
![]()