1ebac49092e3b1a425aa67e367afc623123d6be5.svn-base 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package com.yihu.base.filters;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  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.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import javax.servlet.http.HttpSession;
  14. import org.apache.commons.lang3.StringUtils;
  15. import com.yihu.base.SessionBean;
  16. /**
  17. * 权限过滤
  18. *
  19. * @author Administrator
  20. *
  21. */
  22. public class SecurityFilter implements Filter {
  23. private List<String> excludeList;//不做过滤的地址
  24. public void destroy() {
  25. }
  26. /**
  27. * 过滤器
  28. */
  29. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
  30. throws IOException, ServletException {
  31. HttpServletRequest request = (HttpServletRequest) req;
  32. HttpServletResponse response = (HttpServletResponse) res;
  33. HttpSession session = request.getSession(true);
  34. chain.doFilter(req, res);
  35. // String loginPath ="/ApmWeb/app/appLogin.html";
  36. // String path = request.getServletPath();
  37. // path = path.substring(1, path.length());
  38. // if(excludeList.contains(path)||( path.indexOf('?')<0&&(path.endsWith(".css")||path.endsWith(".js")||path.endsWith(".png")||path.endsWith(".jpg")||path.endsWith(".gif")))){
  39. // chain.doFilter(req, res);
  40. // return;
  41. // }
  42. // try {
  43. //
  44. //
  45. // if (request.getSession().getAttribute("apmUser") == null) {
  46. // String requestType = request.getHeader("X-Requested-With");
  47. // //response.sendRedirect("login.html");
  48. // if (!StringUtils.isEmpty(requestType) && requestType.equalsIgnoreCase("XMLHttpRequest")) {
  49. // response.setStatus(911);
  50. // response.setHeader("sessionstatus", "timeout");
  51. // response.addHeader("loginPath", loginPath);
  52. // return;
  53. // } else {
  54. // response.sendRedirect(loginPath);
  55. // return;
  56. // }
  57. // }else {
  58. // chain.doFilter(req, res);
  59. //
  60. // }
  61. // } catch (Exception e) {
  62. // e.printStackTrace();
  63. // }
  64. }
  65. public void init(FilterConfig config) throws ServletException {
  66. excludeList = new ArrayList<String>();
  67. String excludeStr = config.getInitParameter("exclude");
  68. String[] arr = excludeStr.split(",");
  69. for(String str:arr){
  70. excludeList.add(str.trim());
  71. }
  72. }
  73. public static void main(String[] args) {
  74. String str = "/abd/aa.jspss"; // /abc/abc2/abc.shtml
  75. List<String> list = new ArrayList();
  76. list.add("在在在");
  77. list.add("/abc/*.shtml");
  78. list.add("/*.jsp");
  79. list.add("/ab/*");
  80. list.add("/login.jsp");
  81. list.add("/dd/*.jsp");
  82. //System.out.println(isFilterUrl(str, list));
  83. }
  84. /**
  85. * 过滤URL地址 支持: /* : 所有目录 /*.jsp,/*.do : 根目录,以指定后辍名结尾的 /ab/*.do , /ab/* : 某个目录下有所有指定后辍名结尾的 *.jsp,*.do : 过滤指定后辍名结尾的
  86. *
  87. * @param servletPath
  88. * URL地址
  89. * @return 返回是否过滤
  90. */
  91. public static boolean isFilterUrl(String servletPath, List<String> list) {
  92. servletPath = servletPath.toLowerCase().trim(); // 转成小写的
  93. StringBuffer startStr; // * 号前面的字符串
  94. StringBuffer endStr; // * 号后面的字符串
  95. StringBuffer middleStr; // 对访问路径去除*号前后的字符串
  96. for (String filterUrl : list) {
  97. filterUrl = filterUrl.toLowerCase().trim();
  98. if (filterUrl.equals("/*")) { // 如果是有 /* 则所有都通过
  99. return true;
  100. }
  101. if (filterUrl.equalsIgnoreCase(servletPath)) { // 如果访问路径与列表中的一致,返回正确
  102. return true;
  103. }
  104. if (filterUrl.indexOf("*.") == 0) { // 如果是 "*." 号在前面,后面是后辍名的 例如:
  105. // *.js ,*.jsp,*.do
  106. if (servletPath.endsWith(filterUrl.substring(1)))
  107. return true;
  108. }
  109. if (filterUrl.indexOf("*") >= 1) { // 在有 "*" 号的情况下
  110. startStr = new StringBuffer(filterUrl.substring(0, filterUrl.indexOf("*")));
  111. endStr = new StringBuffer(filterUrl.substring(filterUrl.indexOf("*") + 1, filterUrl.length()));
  112. if (servletPath.startsWith(startStr.toString()) && servletPath.endsWith(endStr.toString())) {
  113. middleStr = new StringBuffer(servletPath.substring(startStr.length(),
  114. servletPath.length() - endStr.length()));
  115. if (middleStr.indexOf("/") == -1) {
  116. return true;
  117. }
  118. }
  119. }
  120. }
  121. return false;
  122. }
  123. }