详解Struts2中对未登录jsp页面实现拦截功能 Struts2中拦截器大家都很经常使用,但是拦截器只能拦截action不能拦截jsp页面。这个时候就有点尴尬了,按道理来说没登录的用户只能看login界面不能够通过输入URL进行界面跳转,这显然是不合理的。这里介绍Struts2中Filter实现jsp页面拦截的功能。(有兴趣的人可以去研究Filter过滤器的其它用法,因为利用过滤器也可以实现action拦截的功能) 下面直接上代码,边看边分析实现步骤和原理。 1.web.xml中的配置信息: SessionInvalidate com.tp.action.SessionCheckFilter //过滤器核心类的class地址 checkSessionKey //session中需要检查的key users redirectURL //过滤重定向的地址 /login.jsp notCheckURLList //不需要过滤的jsp /login.jsp SessionInvalidate //需要过滤的文件 *.jsp 这里有几点需要注意的是: 1.过滤器要尽量放在Struts2配置代码的上面。 2.在SessionInvalidate中 *.jsp 配置非常重要。*.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; /** * 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面 配置参数 checkSessionKey 需检查的在 Session 中保存的关键字 * redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath notCheckURLList * 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath */ public class SessionCheckFilter implements Filter { protected FilterConfig filterConfig = null; private String redirectURL = null; private Set notCheckURLList = new HashSet(); 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()); } } } } 到这里过滤器的功能就实现了。再重申一下web.xml中配置的信息,需要好好检查检查因为那里是过滤器是否成功的关键。 总结 本文关于详解Struts2中对未登录jsp页面实现拦截功能的介绍就到这里,希望对大家有所帮助。欢迎参阅:struts2开发流程及详细配置 Struts2修改上传文件大小限制方法解析等。有什么问题可以随时留言,小编会及时回复大家。感谢朋友们对中文源码网的支持。