防止SQL注入和XSS攻击Filter

使用IBM的安全漏洞扫描工具扫描出一堆漏洞,下面的filter主要是解决防止SQL注入和XSS攻击

一个是Filter负责将请求的request包装一下。

一个是request包装器,负责过滤掉非法的字符。

将这个过滤器配置上以后,世界总算清净多了。。

代码如下:

[java]
  1. import java.io.IOException; 
  2.  
  3. import javax.servlet.Filter; 
  4. import javax.servlet.FilterChain; 
  5. import javax.servlet.FilterConfig; 
  6. import javax.servlet.ServletException; 
  7. import javax.servlet.ServletRequest; 
  8. import javax.servlet.ServletResponse; 
  9. import javax.servlet.http.HttpServletRequest; 
  10.  
  11. /**
  12. * <code>{@link CharLimitFilter}</code>
  13. *
  14. * 拦截防止sql注入
  15. *
  16. * @author Administrator
  17. */ 
  18. publicclass XssFilter implements Filter { 
  19.  
  20.  
  21.     /* (non-Javadoc)
  22.      * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
  23.      */ 
  24.     publicvoid doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, 
  25.             ServletException { 
  26.          
  27.             XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper( 
  28.             (HttpServletRequest) request); 
  29.             filterChain.doFilter(xssRequest, response); 
  30.          
  31.     } 
  32.  
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * <code>{@link CharLimitFilter}</code>
 *
 * 拦截防止sql注入
 *
 * @author Administrator
 */
public class XssFilter implements Filter {


	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException,
			ServletException {
		
			XssHttpServletRequestWrapper xssRequest = new XssHttpServletRequestWrapper(
			(HttpServletRequest) request);
			filterChain.doFilter(xssRequest, response);
		
	}

}

包装器:

[java]
  1. /**
  2. * <code>{@link XssHttpServletRequestWrapper}</code>
  3. *
  4. * TODO : document me
  5. *
  6. * @author Administrator
  7. */ 
  8. publicclass XssHttpServletRequestWrapper extends HttpServletRequestWrapper { 
  9.     HttpServletRequest orgRequest = null
  10.  
  11.     public XssHttpServletRequestWrapper(HttpServletRequest request) { 
  12.         super(request); 
  13.         orgRequest = request; 
  14.     } 
  15.  
  16.     /**
  17.     * 覆盖getParameter方法,将参数名和参数值都做xss过滤。<br/>
  18.     * 如果需要获得原始的值,则通过super.getParameterValues(name)来获取<br/>
  19.     * getParameterNames,getParameterValues和getParameterMap也可能需要覆盖
  20.     */ 
  21.     @Override 
  22.     public String getParameter(String name) { 
  23.         String value = super.getParameter(xssEncode(name)); 
  24.         if (value != null) { 
  25.             value = xssEncode(value); 
  26.         } 
  27.         return value; 
  28.     } 
  29.  
  30.     /**
  31.     * 覆盖getHeader方法,将参数名和参数值都做xss过滤。<br/>
  32.     * 如果需要获得原始的值,则通过super.getHeaders(name)来获取<br/>
  33.     * getHeaderNames 也可能需要覆盖
  34.     */ 
  35.     @Override 
  36.     public String getHeader(String name) { 
  37.  
  38.         String value = super.getHeader(xssEncode(name)); 
  39.         if (value != null) { 
  40.             value = xssEncode(value); 
  41.         } 
  42.         return value; 
  43.     } 
  44.  
  45.     /**
  46.     * 将容易引起xss漏洞的半角字符直接替换成全角字符
  47.     *
  48.     * @param s
  49.     * @return
  50.     */ 
  51.     privatestatic String xssEncode(String s) { 
  52.         if (s == null || "".equals(s)) { 
  53.             return s; 
  54.         } 
  55.         StringBuilder sb = new StringBuilder(s.length() + 16); 
  56.         for (int i = 0; i < s.length(); i++) { 
  57.             char c = s.charAt(i); 
  58.             switch (c) { 
  59.             case'>'
  60.                 sb.append('>');//全角大于号 
  61.                 break
  62.             case'<'
  63.                 sb.append('<');//全角小于号 
  64.                 break
  65.             case'\''
  66.                 sb.append('‘');//全角单引号 
  67.                 break
  68.             case'\"'
  69.                 sb.append('“');//全角双引号 
  70.                 break
  71.             case'&'
  72.                 sb.append('&');//全角 
  73.                 break
  74.             case'\\'
  75.                 sb.append('\');//全角斜线 
  76.                 break
  77.             case'#'
  78.                 sb.append('#');//全角井号 
  79.                 break
  80.             default
  81.                 sb.append(c); 
  82.                 break
  83.             } 
  84.         } 
  85.         return sb.toString(); 
  86.     } 
  87.  
  88.     /**
  89.     * 获取最原始的request
  90.     *
  91.     * @return
  92.     */ 
  93.     public HttpServletRequest getOrgRequest() { 
  94.         return orgRequest; 
  95.     } 
  96.  
  97.     /**
  98.     * 获取最原始的request的静态方法
  99.     *
  100.     * @return
  101.     */ 
  102.     publicstatic HttpServletRequest getOrgRequest(HttpServletRequest req) { 
  103.         if (req instanceof XssHttpServletRequestWrapper) { 
  104.             return ((XssHttpServletRequestWrapper) req).getOrgRequest(); 
  105.         } 
  106.  
  107.         return req; 
  108.     } 
  109.  

web.xml文件

 

  1.   <!-- 解决xss漏洞 --> 
  2.   <filter> 
  3.     <filter-name>xssFilter</filter-name> 
  4.      <filter-class>com.baidu.rigel.sandbox.core.filter.XSSFilter</filter-class> 
  5.   </filter> 
  6.  
  7.   <!-- 解决xss漏洞 --> 
  8.   <filter-mapping> 
  9.     <filter-name>xssFilter</filter-name> 
  10.     <url-pattern>/*</url-pattern> 
  11.   </filter-mapping> 
posted @ 2013-10-12 10:39  tbyang  阅读(1504)  评论(0编辑  收藏  举报