Struts2中攔截器大家都很經(jīng)常使用,但是攔截器只能攔截action不能攔截jsp頁面。這個時候就有點尷尬了,按道理來說沒登錄的用戶只能看login界面不能夠通過輸入URL進行界面跳轉(zhuǎn),這顯然是不合理的。這里介紹Struts2中Filter實現(xiàn)jsp頁面攔截的功能。(有興趣的人可以去研究Filter過濾器的其它用法,因為利用過濾器也可以實現(xiàn)action攔截的功能)
下面直接上代碼,邊看邊分析實現(xiàn)步驟和原理。
1.web.xml中的配置信息:
<filter> <filter-name>SessionInvalidate</filter-name> <filter-class>com.tp.action.SessionCheckFilter</filter-class> //過濾器核心類的class地址 <init-param> <param-name>checkSessionKey</param-name> //session中需要檢查的key <param-value>users</param-value> </init-param> <init-param> <param-name>redirectURL</param-name> //過濾重定向的地址 <param-value>/login.jsp</param-value> </init-param> <init-param> <param-name>notCheckURLList</param-name> //不需要過濾的jsp <param-value>/login.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>SessionInvalidate</filter-name> //需要過濾的文件 <url-pattern>*.jsp</url-pattern> </filter-mapping>
這里有幾點需要注意的是:
1.過濾器要盡量放在Struts2配置代碼的上面。
2.在SessionInvalidate中 <url-pattern>*.jsp</url-pattern> 配置非常重要。*.jsp表示只過濾jsp的界面不會把css,js,action一起給過濾了。如果寫成/*就會把所有的東西一起過濾了。包括css,js,action等。所以這個地方一定要看仔細。
3.SessionCheckFilter過濾的核心類:
package com.tp.action; import java.io.IOException; import java.util.HashSet; import java.util.Set; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * 用于檢測用戶是否登陸的過濾器,如果未登錄,則重定向到指的登錄頁面 配置參數(shù) checkSessionKey 需檢查的在 Session 中保存的關(guān)鍵字 * redirectURL 如果用戶未登錄,則重定向到指定的頁面,URL不包括 ContextPath notCheckURLList * 不做檢查的URL列表,以分號分開,并且 URL 中不包括 ContextPath */ public class SessionCheckFilter implements Filter { protected FilterConfig filterConfig = null; private String redirectURL = null; private Set<String> notCheckURLList = new HashSet<String>(); private String sessionKey = null; @Override public void destroy() { notCheckURLList.clear(); } @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) servletRequest; HttpServletResponse response = (HttpServletResponse) servletResponse; HttpSession session = request.getSession(); if (sessionKey == null) { filterChain.doFilter(request, response); return; } if ((!checkRequestURIIntNotFilterList(request)) && session.getAttribute("users") == null) { response.sendRedirect(request.getContextPath() + redirectURL); return; } filterChain.doFilter(servletRequest, servletResponse); } private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) { String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo()); String temp = request.getRequestURI(); temp = temp.substring(request.getContextPath().length() + 1); // System.out.println("是否包括:"+uri+";"+notCheckURLList+"=="+notCheckURLList.contains(uri)); return notCheckURLList.contains(uri); } @Override public void init(FilterConfig filterConfig) throws ServletException { this.filterConfig = filterConfig; redirectURL = filterConfig.getInitParameter("redirectURL"); sessionKey = filterConfig.getInitParameter("checkSessionKey"); String notCheckURLListStr = filterConfig .getInitParameter("notCheckURLList"); if (notCheckURLListStr != null) { System.out.println(notCheckURLListStr); String[] params = notCheckURLListStr.split(","); for (int i = 0; i < params.length; i++) { notCheckURLList.add(params[i].trim()); } } } }
到這里過濾器的功能就實現(xiàn)了。再重申一下web.xml中配置的信息,需要好好檢查檢查因為那里是過濾器是否成功的關(guān)鍵。
總結(jié)
本文關(guān)于詳解Struts2中對未登錄jsp頁面實現(xiàn)攔截功能的介紹就到這里,有什么問題可以隨時留言,小編會及時回復(fù)大家。感謝朋友們對VeVb武林網(wǎng)的支持。
新聞熱點
疑難解答