檢測用戶是否登錄的過濾器:
——情景:系統中某些頁面只有在正常登錄后才可以使用,用戶請求這些頁面時要檢查session中有無該用戶信息,但在所有必要的頁面加上session的判斷相當麻煩的事情
——解決方案:編寫一個用於檢測用戶是否登錄的過濾器,如果用戶未登錄,則重定向到指定的登錄頁面
——要求:需檢查的在Session中保存的關鍵字;如果用戶未登錄,需重定向到指定的頁面(URL不包括ContextPath);不做檢查的URL列表(以分號分開,並且URL中不包括ContextPath)都要采取可配置的方式。
list.jsp
設置b,c,d,e需要用戶登錄權限,點擊跳轉login.jsp
登錄完成,輸入有效的名字可以進入其他頁面
源代碼:
list.jsp
<a href="a.jsp">AAA</a> <br><br> <a href="b.jsp">BBB</a> <br><br> <a href="c.jsp">CCC</a> <br><br> <a href="d.jsp">DDD</a> <br><br> <a href="e.jsp">EEE</a> <br><br>
a,b,c,d,e.jsp
<h4>AAA PAGE</h4> <a href="list.jsp">Return...</a>
login.jsp
<form action="doLogin.jsp" method="post"> username: <input type="text" name="username"> <input type="submit" value="Submit"> </form>
doLogin.jsp
<% //1.獲取用戶的登錄信息 String username = request.getParameter("username"); //2.若登錄信息完整,則把登錄信息方法HttpSession if (username!=null&&!username.trim().equals("")){ session.setAttribute(application.getInitParameter("userSessionKey"),username); //3.重定向到list.jsp response.sendRedirect("list.jsp"); }else { response.sendRedirect("login.jsp"); } %>
web.xml的相關設置
<!--用戶信息放入到session中鍵的名字--> <context-param> <param-name>userSessionKey</param-name> <param-value>USERSESSIONKEY</param-value> </context-param> <!--若未登陸,需重定向的頁面--> <context-param> <param-name>rediretPage</param-name> <param-value>/login/login.jsp</param-value> </context-param> <!--不需要攔截(或檢查)的URL列表--> <context-param> <param-name>uncheckedUrls</param-name> <param-value>/login/a.jsp,/login/list.jsp,/login/login.jsp,/login/doLogin.jsp,</param-value> </context-param>
LoginFilter.java
package com.demo.filter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.Arrays; import java.util.List; public class LoginFilter implements Filter { private String sessionKey; private String redirectUrl; private String uncheckedUrls; private FilterConfig filterConfig; @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; ServletContext servletContext = this.filterConfig.getServletContext(); sessionKey = servletContext.getInitParameter("userSessionKey"); redirectUrl = servletContext.getInitParameter("rediretPage"); uncheckedUrls = servletContext.getInitParameter("uncheckedUrls"); System.out.println(sessionKey); System.out.println(redirectUrl); System.out.println(uncheckedUrls); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; //1.從web.xml文件中獲取sessionKey,redirectUrl,uncheckedUrls //1.獲取請求的servletPath String requestUrl = request.getRequestURL().toString(); String requestUri = request.getRequestURI(); String servletPath = request.getServletPath(); // http://localhost:8081/javaweb/login/list.jsp System.out.println(requestUrl); // /javaweb/login/list.jsp System.out.println(requestUri); // /login/list.jsp System.out.println(servletPath); //2.檢查1獲取的servletPath是否不需要檢查的URL中的一個,若是,則直接放行,方法結束 List<String> urls = Arrays.asList(uncheckedUrls.split(",")); if (urls.contains(servletPath)){ filterChain.doFilter(request,response); return; } //3.從session中獲取sessionKey對應的值,若值不存在,則重定向到redirectUrl Object user = request.getSession().getAttribute(sessionKey); if (user == null){ response.sendRedirect(request.getContextPath()+ redirectUrl); return; } //4.若存在,則放行,允許訪問 filterChain.doFilter(request,response); } @Override public void destroy() { } }