6177a972f1d361c406e81e8b7954baeeb61e2920.svn-base 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.system.filter;
  2. import java.io.IOException;
  3. import java.util.Enumeration;
  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. import org.apache.log4j.Logger;
  13. /**
  14. * 非法字符过滤器
  15. * 1.所有非法字符配置在web.xml中,如需添加新字符,请自行配置
  16. * 2.请注意请求与相应的编码格式设置
  17. * @author lee
  18. *
  19. */
  20. public class XSSFilter implements Filter{
  21. private static final Logger log = Logger.getLogger(XSSFilter.class);
  22. private String encoding;
  23. private String[] illegalChars;
  24. private final String str="<,>";
  25. @Override
  26. public void destroy() {
  27. // TODO Auto-generated method stub
  28. encoding = null;
  29. illegalChars = null;
  30. }
  31. @Override
  32. public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
  33. throws IOException, ServletException {
  34. // TODO Auto-generated method stub
  35. HttpServletRequest req = (HttpServletRequest)request;
  36. HttpServletResponse res = (HttpServletResponse) response;
  37. //必须手动指定编码格式
  38. req.setCharacterEncoding(encoding);
  39. String tempURL = req.getRequestURI();
  40. log.info(tempURL);
  41. Enumeration params = req.getParameterNames();
  42. //是否执行过滤 true:执行过滤 false:不执行过滤
  43. boolean executable = true;
  44. //非法状态 true:非法 false;不非法
  45. boolean illegalStatus = false;
  46. String illegalChar = "";
  47. //对参数名与参数进行判断
  48. w:while(params.hasMoreElements()){
  49. String paramName = (String) params.nextElement();
  50. executable = true;
  51. //密码不过滤
  52. if(paramName.toLowerCase().contains("password")){
  53. executable = false;
  54. }
  55. if(executable){
  56. String[] paramValues = req.getParameterValues(paramName);
  57. f1:for(int i=0;i<paramValues.length;i++){
  58. String paramValue = paramValues[i];
  59. f2:for(int j=0;j<illegalChars.length;j++){
  60. illegalChar = illegalChars[j];
  61. if(paramValue.indexOf(illegalChar) != -1){
  62. illegalStatus = true;//非法状态
  63. break f2;
  64. }
  65. }
  66. if(illegalStatus){
  67. break f1;
  68. }
  69. }
  70. }
  71. if(illegalStatus){
  72. break w;
  73. }
  74. }
  75. if(illegalStatus){
  76. //必须手动指定编码格式
  77. res.setContentType("text/html;charset="+encoding);
  78. res.setCharacterEncoding(encoding);
  79. res.getWriter().print("<script>window.alert('当前链接中存在非法字符');window.history.go(-1);</script>");
  80. }else{
  81. filterChain.doFilter(request, response);
  82. }
  83. }
  84. @Override
  85. public void init(FilterConfig filterConfig) throws ServletException {
  86. // TODO Auto-generated method stub
  87. encoding = filterConfig.getInitParameter("encoding");
  88. illegalChars = str.split(",");
  89. }
  90. }