4883d11bd0544463c5834f861d5e576a95c0dd86.svn-base 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package com.system.filter;
  2. import java.io.IOException;
  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. import javax.servlet.http.HttpServletResponse;
  11. import javax.servlet.http.HttpSession;
  12. public class AuthFilter implements Filter
  13. {
  14. public void destroy() {}
  15. public void doFilter(ServletRequest servletRequest, ServletResponse
  16. servletResponse,FilterChain filterChain) throws IOException, ServletException
  17. {
  18. HttpServletRequest request = (HttpServletRequest)servletRequest;
  19. HttpServletResponse response = (HttpServletResponse)servletResponse;
  20. String currentURL = request.getRequestURI();
  21. //取得根目录所对应的绝对路径:
  22. String targetURL = currentURL.substring(currentURL.indexOf("/", 1), currentURL.length());
  23. //截取到当前文件名用于比较
  24. HttpSession session = request.getSession(false);
  25. if (!"/login.html".equals(targetURL)&&!currentURL.contains("jsessionid"))
  26. {//判断当前页是否是重定向以后的登录页面页面,如果是就不做session的判断,防止出现死循环
  27. if (session == null || session.getAttribute("User_Name") == null) {
  28. //*用户登录以后需手动添加session
  29. System.out.println("request.getContextPath()=" + request.getContextPath());
  30. response.sendRedirect(request.getContextPath() + "/login.html");
  31. return; }}
  32. //加入filter链继续向下执行
  33. filterChain.doFilter(request, response);
  34. }
  35. public void init(FilterConfig filterConfig) throws ServletException {}
  36. }