当前位置:   article > 正文

存储型Xss跨站式脚本攻击解决方案_存储型xss解决方案

存储型xss解决方案
  1. 一 新建一个XssFilter类
  2. package com.walk.common.xssnew;
  3. import java.io.IOException;
  4. import javax.servlet.Filter;
  5. import javax.servlet.FilterChain;
  6. import javax.servlet.FilterConfig;
  7. import javax.servlet.ServletException;
  8. import javax.servlet.ServletRequest;
  9. import javax.servlet.ServletResponse;
  10. import javax.servlet.http.HttpServletRequest;
  11. import javax.servlet.http.HttpServletResponse;
  12. /**
  13. * <p>ClassName: XssFilter</p>
  14. * Description:Filter过滤器实现对Request的过滤<br/>
  15. * @date 2018年11月1日 下午3:02:37
  16. * @author jingzhenying
  17. * @version 1.0
  18. * @since JDK 1.7
  19. */
  20. public class XssFilter implements Filter {
  21. @Override
  22. public void destroy() {
  23. }
  24. /**
  25. * 过滤器用来过滤的方法
  26. */
  27. @Override
  28. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
  29. throws IOException, ServletException {
  30. // 包装request
  31. XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper((HttpServletRequest) request);
  32. //实际设置
  33. HttpServletResponse xssResponse = (HttpServletResponse) response;
  34. xssResponse.setHeader("X-XSS-Protection", "1; mode=block");
  35. xssResponse.setHeader("X-Frame-Options", "SAMEORIGIN");
  36. xssResponse.setHeader("Strict-Transport-Security", "max-age=31536; includeSubDomains");
  37. // xssResponse.setHeader("Content-Security-Policy", "default-src 'self'");
  38. xssResponse.setHeader("X-Content-Type-Options", "nosniff");
  39. chain.doFilter(xssRequest, xssResponse);
  40. }
  41. @Override
  42. public void init(FilterConfig filterConfig) throws ServletException {
  43. }
  44. }

 

二  再新建一个 XssHttpServletRequestWrapper类

  1. package com.walk.common.xssnew;
  2. import java.util.Map;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletRequestWrapper;
  5. /**
  6. * <p>ClassName: XssHttpServletRequestWrapper</p>
  7. * Description:HttpServletRequestWapper的包装类<br/>
  8. * @date 2019年10月14日 下午3:03:01
  9. * @author jingzhenying
  10. * @version 1.0
  11. * @since JDK 1.7
  12. */
  13. public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
  14. HttpServletRequest orgRequest = null;
  15. public XssHttpServletRequestWrapper(HttpServletRequest request) {
  16. super(request);
  17. }
  18. /**
  19. * 覆盖getParameter方法,将参数名和参数值都做xss过滤。
  20. * 如果需要获得原始的值,则通过super.getParameterValues(name)来获取
  21. * getParameterNames,getParameterValues和getParameterMap也可能需要覆盖
  22. */
  23. @Override
  24. public String getParameter(String name) {
  25. String value = super.getParameter(xssEncode(name));
  26. if (value != null) {
  27. value = xssEncode(value);
  28. }
  29. return value;
  30. }
  31. @Override
  32. public String[] getParameterValues(String name) {
  33. String[] value = super.getParameterValues(name);
  34. if (value != null) {
  35. for (int i = 0; i < value.length; i++) {
  36. value[i] = xssEncode(value[i]);
  37. }
  38. }
  39. return value;
  40. }
  41. @SuppressWarnings("rawtypes")
  42. @Override
  43. public Map getParameterMap() {
  44. return super.getParameterMap();
  45. }
  46. /**
  47. * 将容易引起xss漏洞的半角字符直接替换成全角字符 在保证不删除数据的情况下保存
  48. *
  49. * @param s
  50. * @return 过滤后的值
  51. */
  52. private static String xssEncode(String value) {
  53. if (value == null || value.isEmpty()) {
  54. return value;
  55. }
  56. value = value.replaceAll("eval\\((.*)\\)", "");
  57. value = value.replaceAll("<","&lt;");
  58. value = value.replaceAll(">","&gt;");
  59. value = value.replaceAll("'","&apos;");
  60. value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
  61. value = value.replaceAll("(?i)<script.*?>.*?<script.*?>", "");
  62. value = value.replaceAll("(?i)<script.*?>.*?</script.*?>", "");
  63. value = value.replaceAll("(?i)<.*?javascript:.*?>.*?</.*?>", "");
  64. value = value.replaceAll("(?i)<.*?\\s+on.*?>.*?</.*?>", "");
  65. // value = value.replaceAll("[<>{}\\[\\];\\&]","");
  66. return value;
  67. }
  68. /**
  69. * 覆盖getHeader方法,将参数名和参数值都做xss过滤。 如果需要获得原始的值,则通过super.getHeaders(name)来获取
  70. * getHeaderNames 也可能需要覆盖 这一段代码在一开始没有注释掉导致出现406错误,原因是406错误是HTTP协议状态码的一种,
  71. * 表示无法使用请求的内容特性来响应请求的网页。一般是指客户端浏览器不接受所请求页面的 MIME 类型。
  72. **/
  73. @Override
  74. public String getHeader(String name) {
  75. String value = super.getHeader(xssEncode(name));
  76. if (value != null) {
  77. value = xssEncode(value);
  78. }
  79. return value;
  80. }
  81. }

 

三   配置Web.xml


<!--解决xss漏洞-->
  <filter>
    <!--类名-->
    <filter-name>XssFilter</filter-name>
    <!--该类的路径-->
    <filter-class>xxx.xxx.common.xssnew.XssFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>XssFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/150338
推荐阅读
  

闽ICP备14008679号