前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >实现Struts2中对未登录的jsp页面进行拦截功能(采用的是Struts2中过滤器进行过滤拦截)

实现Struts2中对未登录的jsp页面进行拦截功能(采用的是Struts2中过滤器进行过滤拦截)

作者头像
林老师带你学编程
发布2022-11-30 10:29:02
7930
发布2022-11-30 10:29:02
举报
文章被收录于专栏:强仔仔强仔仔

Struts2中拦截器大家都很经常使用,但是拦截器只能拦截action不能拦截jsp页面。这个时候就有点尴尬了,按道理来说没登录的用户只能看login界面不能够通过输入URL进行界面跳转,这显然是不合理的。这里介绍Struts2中Filter实现jsp页面拦截的功能。(有兴趣的人可以去研究Filter过滤器的其它用法,因为利用过滤器也可以实现action拦截的功能)

下面直接上代码,边看边分析实现步骤和原理。

1.web.xml中的配置信息:

代码语言:javascript
复制
	<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等。所以这个地方一定要看仔细。

2。SessionCheckFilter过滤的核心类:

代码语言:javascript
复制
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<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());  
      }  
    }  
  }  
}  

到这里过滤器的功能就实现了。再重申一下web.xml中配置的信息,需要好好检查检查因为那里是过滤器是否成功的关键。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-05-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档