詳解Struts2中對(duì)未登錄jsp頁(yè)面實(shí)現(xiàn)攔截功能
Struts2中攔截器大家都很經(jīng)常使用,但是攔截器只能攔截action不能攔截jsp頁(yè)面。這個(gè)時(shí)候就有點(diǎn)尷尬了,按道理來(lái)說(shuō)沒(méi)登錄的用戶(hù)只能看login界面不能夠通過(guò)輸入U(xiǎn)RL進(jìn)行界面跳轉(zhuǎn),這顯然是不合理的。這里介紹Struts2中Filter實(shí)現(xiàn)jsp頁(yè)面攔截的功能。(有興趣的人可以去研究Filter過(guò)濾器的其它用法,因?yàn)槔眠^(guò)濾器也可以實(shí)現(xiàn)action攔截的功能)
下面直接上代碼,邊看邊分析實(shí)現(xiàn)步驟和原理。
1.web.xml中的配置信息:
<filter> <filter-name>SessionInvalidate</filter-name> <filter-class>com.tp.action.SessionCheckFilter</filter-class> //過(guò)濾器核心類(lèi)的class地址 <init-param> <param-name>checkSessionKey</param-name> //session中需要檢查的key <param-value>users</param-value> </init-param> <init-param> <param-name>redirectURL</param-name> //過(guò)濾重定向的地址 <param-value>/login.jsp</param-value> </init-param> <init-param> <param-name>notCheckURLList</param-name> //不需要過(guò)濾的jsp <param-value>/login.jsp</param-value> </init-param> </filter> <filter-mapping> <filter-name>SessionInvalidate</filter-name> //需要過(guò)濾的文件 <url-pattern>*.jsp</url-pattern> </filter-mapping>
這里有幾點(diǎn)需要注意的是:
1.過(guò)濾器要盡量放在Struts2配置代碼的上面。
2.在SessionInvalidate中 <url-pattern>*.jsp</url-pattern> 配置非常重要。*.jsp表示只過(guò)濾jsp的界面不會(huì)把css,js,action一起給過(guò)濾了。如果寫(xiě)成/*就會(huì)把所有的東西一起過(guò)濾了。包括css,js,action等。所以這個(gè)地方一定要看仔細(xì)。
3.SessionCheckFilter過(guò)濾的核心類(lèi):
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; /** * 用于檢測(cè)用戶(hù)是否登陸的過(guò)濾器,如果未登錄,則重定向到指的登錄頁(yè)面 配置參數(shù) checkSessionKey 需檢查的在 Session 中保存的關(guān)鍵字 * redirectURL 如果用戶(hù)未登錄,則重定向到指定的頁(yè)面,URL不包括 ContextPath notCheckURLList * 不做檢查的URL列表,以分號(hào)分開(kāi),并且 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()); } } } }
到這里過(guò)濾器的功能就實(shí)現(xiàn)了。再重申一下web.xml中配置的信息,需要好好檢查檢查因?yàn)槟抢锸沁^(guò)濾器是否成功的關(guān)鍵。
總結(jié)
本文關(guān)于詳解Struts2中對(duì)未登錄jsp頁(yè)面實(shí)現(xiàn)攔截功能的介紹就到這里,希望對(duì)大家有所幫助。歡迎參閱:struts2開(kāi)發(fā)流程及詳細(xì)配置 Struts2修改上傳文件大小限制方法解析等。有什么問(wèn)題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家。感謝朋友們對(duì)的支持。
相關(guān)文章:
