用过滤器Filter判断用户是否登陆

时间:2025-01-04 16:36:20

用过滤器Filter判断用户是否登陆

WEB.XML

  1. <!-- 用户session的 键   sessionKEY -->
  2. <context-param>
  3. <param-name>userSessionKey</param-name>
  4. <param-value>USERSESSIONKEY</param-value>
  5. </context-param>
  6. <!-- 重定向的页面 -->
  7. <context-param>
  8. <param-name>rediretPage</param-name>
  9. <param-value>/u_member/Login.jsp</param-value>
  10. </context-param>
  11. <!-- 不需要过滤的页面 -->
  12. <context-param>
  13. <param-name>uncheckUrls</param-name>
  14. <param-value>/u_member/insert.jsp,/u_member/Login.jsp</param-value>
  15. </context-param>

Filter 过滤器代码

  1. package go.onlineShop.filter;
  2. import java.io.IOException;
  3. import java.util.Arrays;
  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.annotation.WebFilter;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14. /**
  15. * Servlet Filter implementation class IsLoginFilter
  16. * 判断是否登陆过滤器
  17. */
  18. @WebFilter("/u_member")
  19. public class IsLoginFilter implements Filter {
  20. //从xml中取值
  21. String uncheckUrls =null;
  22. String userSessionKey=null;
  23. String rediretPage=null;
  24. /**
  25. * Default constructor.
  26. */
  27. public IsLoginFilter() {
  28. // TODO Auto-generated constructor stub
  29. }
  30. /**
  31. * @see Filter#destroy()
  32. */
  33. public void destroy() {
  34. // TODO Auto-generated method stub
  35. }
  36. /**
  37. * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
  38. */
  39. public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  40. // TODO Auto-generated method stub
  41. //1.得到访问路径
  42. HttpServletRequest request = (HttpServletRequest)req;
  43. HttpServletResponse response = (HttpServletResponse)res;
  44. String reqUrl = request.getRequestURL().toString();
  45. String reqURI = request.getRequestURI();
  46. String servletPath = request.getServletPath();
  47. //  String[] uncheckUrl = uncheckUrls.split(",");
  48. List<String> urls = Arrays.asList(uncheckUrls.split(","));
  49. //2.如果是属于不需要过滤的地址就直接  放行  方法结束
  50. if(urls.contains(servletPath)){
  51. chain.doFilter(request, response);
  52. return;
  53. }
  54. //3.从session中获取sessionKey对应的值;若不存在  则跳转到登陆页面
  55. Object user = request.getSession().getAttribute(userSessionKey);
  56. if(user==null){
  57. request.getRequestDispatcher(rediretPage).forward(request, response);
  58. return;
  59. }
  60. //4若存在 则放行;
  61. chain.doFilter(request, response);
  62. }
  63. /**
  64. * @see Filter#init(FilterConfig)
  65. */
  66. public void init(FilterConfig fConfig) throws ServletException {
  67. rediretPage=fConfig.getServletContext().getInitParameter("rediretPage");
  68. uncheckUrls=fConfig.getServletContext().getInitParameter("uncheckUrls");
  69. userSessionKey=fConfig.getServletContext().getInitParameter("userSessionKey");
  70. }
  71. }

Login页面

用过滤器Filter判断用户是否登陆

参考:

用过滤器Filter判断用户是否登陆 - ****博客
http://blog.****.net/u010634066/article/details/38769577