当前位置:   article > 正文

XSS 存储漏洞解决_xss存储型漏洞解决代码

xss存储型漏洞解决代码

一、新建一个过滤器

  1. import javax.servlet.*;
  2. import javax.servlet.http.HttpServletRequest;
  3. import java.io.IOException;
  4. public class XssFilter implements Filter {
  5. @Override
  6. public void init(FilterConfig filterConfig) throws ServletException {
  7. }
  8. @Override
  9. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  10. MyXssHttpServletRequestWrapper req=new MyXssHttpServletRequestWrapper((HttpServletRequest)servletRequest);
  11. filterChain.doFilter(req,servletResponse);
  12. }
  13. @Override
  14. public void destroy() {
  15. }
  16. }

 二、继承HttpServletRequestWrapper,重写方法getParameterMap,替换非法字符

  1. package com.dawnpro.module.system.security.authentication.access;
  2. import com.alibaba.fastjson.JSONObject;
  3. import org.apache.commons.lang.StringUtils;
  4. import org.springframework.util.CollectionUtils;
  5. import org.springframework.web.servlet.HandlerMapping;
  6. import javax.servlet.ReadListener;
  7. import javax.servlet.ServletInputStream;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletRequestWrapper;
  10. import java.io.BufferedReader;
  11. import java.io.ByteArrayInputStream;
  12. import java.io.IOException;
  13. import java.io.InputStreamReader;
  14. import java.nio.charset.Charset;
  15. import java.util.*;
  16. import java.util.regex.Matcher;
  17. import java.util.regex.Pattern;
  18. public class MyXssHttpServletRequestWrapper extends HttpServletRequestWrapper {
  19. HttpServletRequest orgRequest= null;
  20. //不期待被过滤的的链接和字段(管理后台使用了富文本,希望有可编辑的内容)
  21. HashMap<String, String> doNotFilterURLAndParamMap = new HashMap<String, String>() {
  22. {
  23. put("/api/v2/group/manage", "description");
  24. put("/api/v1/sendNews", "content");
  25. }
  26. };
  27. boolean isUpData = false;//判断是否是上传 上传忽略
  28. public MyXssHttpServletRequestWrapper(HttpServletRequest servletRequest) {
  29. super(servletRequest);
  30. String contentType = servletRequest.getContentType ();
  31. orgRequest=servletRequest;
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/150322
推荐阅读
相关标签
  

闽ICP备14008679号