/src/main/java/cn/springboot/common/authority/filter/XSSSecurityFilter.java

https://bitbucket.org/guoj5477/lotter_customers · Java · 112 lines · 68 code · 17 blank · 27 comment · 7 complexity · 2c78ad9e7791be619f8a1c6594be6f78 MD5 · raw file

  1. package cn.springboot.common.authority.filter;
  2. import java.io.IOException;
  3. import java.util.Map;
  4. import java.util.Set;
  5. import javax.servlet.Filter;
  6. import javax.servlet.FilterChain;
  7. import javax.servlet.FilterConfig;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.ServletRequest;
  10. import javax.servlet.ServletResponse;
  11. import javax.servlet.annotation.WebFilter;
  12. import javax.servlet.annotation.WebInitParam;
  13. import javax.servlet.http.HttpServletRequest;
  14. import javax.servlet.http.HttpServletResponse;
  15. import org.slf4j.Logger;
  16. import org.slf4j.LoggerFactory;
  17. import com.alibaba.fastjson.JSON;
  18. import cn.springboot.common.authority.service.xss.XSSHttpRequestWrapper;
  19. import cn.springboot.common.authority.service.xss.XSSSecurityConfig;
  20. import cn.springboot.common.authority.service.xss.XSSSecurityConstants;
  21. import cn.springboot.common.authority.service.xss.XSSSecurityManager;
  22. /**
  23. * @Description xss攻击脚本过滤器
  24. * @author 王鑫
  25. * @date Mar 24, 2017 7:43:01 PM
  26. */
  27. @WebFilter(urlPatterns = "/*", filterName = "XSSCheck", initParams = { @WebInitParam(name = "securityconfig", value = "classpath:conf/xss_security_config.xml") })
  28. public class XSSSecurityFilter implements Filter {
  29. private static final Logger log = LoggerFactory.getLogger(XSSSecurityFilter.class);
  30. /**
  31. * 初始化操作
  32. */
  33. public void init(FilterConfig filterConfig) throws ServletException {
  34. XSSSecurityManager.init(filterConfig);
  35. }
  36. /**
  37. * 销毁操作
  38. */
  39. public void destroy() {
  40. log.debug("XSSSecurityFilter destroy() begin");
  41. XSSSecurityManager.destroy();
  42. log.debug("XSSSecurityFilter destroy() end");
  43. }
  44. /**
  45. * 安全审核 读取配置信息
  46. */
  47. public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
  48. // 判断是否使用HTTP
  49. checkRequestResponse(request, response);
  50. HttpServletRequest httpRequest = (HttpServletRequest) request;
  51. HttpServletResponse httpResponse = (HttpServletResponse) response;
  52. // http信息封装类
  53. XSSHttpRequestWrapper xssRequest = new XSSHttpRequestWrapper(httpRequest);
  54. // 对request信息进行封装并进行校验工作,若校验失败(含非法字符),根据配置信息进行日志记录和请求中断处理
  55. if (xssRequest.validateParameter(httpResponse)) {
  56. if (XSSSecurityConfig.IS_LOG) {
  57. String paramStr = "";
  58. Map<String, String[]> submitParams = httpRequest.getParameterMap();
  59. Set<String> submitNames = submitParams.keySet();
  60. String[] submitValues = null;
  61. for (String submitName : submitNames) {
  62. submitValues = submitParams.get(submitName);
  63. for (String submitValue : (String[]) submitValues)
  64. paramStr = paramStr + submitValue;
  65. }
  66. log.debug("XSS Security Filter RequestURL:" + httpRequest.getRequestURL().toString());
  67. log.debug("param:" + paramStr);
  68. log.debug("XSS Security Filter RequestParameter:{}", JSON.toJSONString(httpRequest.getParameterMap()));
  69. }
  70. // 是否中断操作
  71. if (XSSSecurityConfig.IS_CHAIN) {
  72. request.setAttribute("err", "您输入的参数有非法字符,请输入正确的参数!");
  73. request.setAttribute("pageUrl",httpRequest.getRequestURI());
  74. request.getRequestDispatcher(request.getServletContext().getContextPath() + XSSSecurityConstants.FILTER_ERROR_PAGE).forward(request, response);
  75. return;
  76. }
  77. }
  78. chain.doFilter(request, response);
  79. }
  80. /**
  81. * 判断Request ,Response 类型
  82. *
  83. * @param request
  84. * ServletRequest
  85. * @param response
  86. * ServletResponse
  87. * @throws javax.servlet.ServletException
  88. */
  89. private void checkRequestResponse(ServletRequest request, ServletResponse response) throws ServletException {
  90. if (!(request instanceof HttpServletRequest))
  91. throw new ServletException("Can only process HttpServletRequest");
  92. if (!(response instanceof HttpServletResponse))
  93. throw new ServletException("Can only process HttpServletResponse");
  94. }
  95. }