XSS过滤

<filter>
   <filter-name>NewXssFilter</filter-name>
   <filter-class>com.sbr.fort.filter.NewXssFilter</filter-class>
   <init-param>
      <!-- 不需要顾虑的连接 -->
      <param-name>excludeUrls</param-name>
      <param-value>/back_up@</param-value>
   </init-param>
   <init-param>
      <!--  分隔符 -->
      <param-name>SplitChar</param-name>
      <param-value>@</param-value>
   </init-param>
   <init-param>
      <!--   需要过滤的字符串 -->
      <param-name>FilterChar</param-name>
      <param-value>AND@OR@create@drop@delete@update@union</param-value>
   </init-param>
   <init-param>
      <!-- 全角替换字符串,与过滤字符串以SplitChar为分隔符一一对应 -->
      <param-name>ReplaceChar</param-name>
      <param-value>sqlinjection@sqlinjection@sqlinjection@sqlinjection@sqlinjection@sqlinjection@sqlinjection</param-value>
   </init-param>
</filter> 

public class NewXssFilter implements Filter {
   private String filterChar;//需要过滤的字符串
   private String replaceChar;//替代需要过滤的字符串对应的字符串
   private String splitChar;//分隔符
   private String excludeUrls;//不需要过滤的连接
   FilterConfig filterConfig = null;
   public void init(FilterConfig filterConfig) throws ServletException {
      this.filterChar=filterConfig.getInitParameter("FilterChar");
      this.replaceChar=filterConfig.getInitParameter("ReplaceChar");
      this.splitChar=filterConfig.getInitParameter("SplitChar");
      this.excludeUrls=filterConfig.getInitParameter("excludeUrls");
      this.filterConfig = filterConfig;
   }
   public void destroy() {
      this.filterConfig = null;
   }
       
   public void doFilter(ServletRequest request, ServletResponse response,
           FilterChain chain) throws IOException, ServletException {
      //如果是不需要的过滤的url,直接到下一个filter
      if(isExcludeUrl(request)){
         chain.doFilter(request, response);
      }else{
      //否则包装HttpServletRequest,替换参数
         chain.doFilter(new XssHttpServletRequestWrapper((HttpServletRequest) request,filterChar,replaceChar,splitChar), response);
      }
   }
   private boolean isExcludeUrl(ServletRequest request){
      boolean exclude=false;
      if(StringUtils.isNotBlank(excludeUrls)){
          String[]excludeUrl=excludeUrls.split(splitChar);
          if(excludeUrl!=null&&excludeUrl.length>0){
             for(String url:excludeUrl){
                if(URLHelper.getURI((HttpServletRequest)request).startsWith(url)){
                   exclude=true;
                }
             }
          }
      }
      return exclude;
   }

}

public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {
   private String[]filterChars;
   private String[]replaceChars;
   public XssHttpServletRequestWrapper(HttpServletRequest request,String filterChar,String replaceChar,String splitChar) {
      super(request);
      if(filterChar!=null&&filterChar.length()>0){
         filterChars=filterChar.split(splitChar);
      }
      if(replaceChar!=null&&replaceChar.length()>0){
         replaceChars=replaceChar.split(splitChar);
      }
   }
   public String getQueryString() {
      String value = super.getQueryString();
      if (value != null) {
         value = xssEncode(value);
         value = value.replaceAll("&", "&").replaceAll("=", "=").replaceAll(".([';]+|(–)+)."," ");;
      }
      return value;
   }
   
   /**
    * 覆盖getParameter方法,将参数名和参数值都做xss过滤。<br/>
    * 如果需要获得原始的值,则通过super.getParameterValues(name)来获取<br/>
    * getParameterNames,getParameterValues和getParameterMap也可能需要覆盖
    */
   public String getParameter(String name) {
      String value = super.getParameter(xssEncode(name));
      if (value != null) {
               //关键处理
         value = xssEncode(value);
      }
      return value;
   }
   
   public String[] getParameterValues(String name) {
      String[]parameters=super.getParameterValues(name);
      if (parameters==null||parameters.length == 0) {
         return null;
      }
      for (int i = 0; i < parameters.length; i++) {
               //关键处理
         parameters[i] = xssEncode(parameters[i]);
      }
      return parameters;
   }
   
   /**
    * 覆盖getHeader方法,将参数名和参数值都做xss过滤。<br/>
    * 如果需要获得原始的值,则通过super.getHeaders(name)来获取<br/> getHeaderNames 也可能需要覆盖
    */
   public String getHeader(String name) {

      String value = super.getHeader(xssEncode(name));
      if (value != null) {
              //关键处理
         value = xssEncode(value);
      }
      return value;
   }
   
   /**
    * 将容易引起xss漏洞的半角字符直接替换成全角字符
    * 
    * @param s
    * @return
    */
   private  String xssEncode(String s) {
      if (s == null || s.equals("")) {
         return s;
      }
      for (int i = 0; i < filterChars.length; i++) {
         if(s.contains(filterChars[i])){
            s=s.replace(filterChars[i], replaceChars[i]);
         }
      }
      return s;
   }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值