`

在过滤器中判断用户登录(session过期跳转到登陆页面)

 
阅读更多

今天一直想重写DispatchAction的excute方法来判断用户session是否过期,但是由于项目中所有的Action从一开始都是extends DispatchAction,全盘替换总怕出什么问题,所以想换一种方式,考虑在进入具体action前先走过滤器,那么是否可以在过滤器中试一下呢?上网一搜果然可以,先将代码备份一下作将来参考用:

public class SetCharacterEncodingFilter implements Filter {

 // ----------------------------------------------------- Instance Variables

 /**
  * The default character encoding to set for requests that pass through this
  * filter.
  */
 protected String encoding = null;

 /**
  * The filter configuration object we are associated with. If this value is
  * null, this filter instance is not currently configured.
  */
 protected FilterConfig filterConfig = null;

 /**
  * Should a character encoding specified by the client be ignored?
  */
 protected boolean ignore = true;

 // --------------------------------------------------------- Public Methods

 /**
  * Take this filter out of service.
  */
 public void destroy() {

  this.encoding = null;
  this.filterConfig = null;

 }

 private final static String[] ignore_url={"/index.do"};//进入各子平台时不用坐登录check
 /**
  * Select and set (if specified) the character encoding to be used to
  * interpret request parameters for this request.
  *
  * @param request
  *            The servlet request we are processing
  * @param result
  *            The servlet response we are creating
  * @param chain
  *            The filter chain we are processing
  *
  * @exception IOException
  *                if an input/output error occurs
  * @exception ServletException
  *                if a servlet error occurs
  */
 public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {

  System.out.println("&&&&&&&&&&&&&&&&doFilter&&&&&&&&&&&&&&");
  // Conditionally select and set the character encoding to be used
  
  boolean haveFind = true;
  if (ignore || (request.getCharacterEncoding() == null)) {
   String encoding = selectEncoding(request);
   if (encoding != null)
   {
    request.setCharacterEncoding(encoding);
    response.setCharacterEncoding(encoding);
   }
  }
  RequestDispatcher dispatcher = request.getRequestDispatcher("logout.jsp");//这里设置如果没有登陆将要转发到的页面  
    HttpServletRequest req = (HttpServletRequest) request;  
    HttpServletResponse res = (HttpServletResponse) response;  
    HttpSession session = req.getSession(true);  
   
    if (req.getRequestURI().indexOf(".jsp") >= 0 || req.getRequestURI().indexOf(".do") >= 0)  
             haveFind = false;
    //System.out.println(haveFind);
    for (int i = 0; i < ignore_url.length; i++) {  
             if (req.getRequestURI().indexOf(ignore_url[i]) >= 0) {  
                 haveFind = true;  
                 break;  
             } 
    }

    //System.out.println(req.getRequestURI()+haveFind);
   
    // 从session里取的用户名信息  
    String username = (String) session.getAttribute("Hthm_RealName");//这里获取session,为了检查session里有没有保存用户信息,没有的话回转发到登陆页面  
   
    // 判断如果没有取到用户信息,就跳转到登陆页面  
    if (!haveFind&&(username == null || "".equals(username)))  
    {  
     // 跳转到登陆页面  
     dispatcher.forward(request,response);  
     System.out.println("用户没有登陆,不允许操作");  
       
     res.setHeader("Cache-Control","no-store");     
     res.setDateHeader("Expires",0);  
     res.setHeader("Pragma","no-cache");  
    }  
    else 
    {  
     // 已经登陆,继续此次请求  
     chain.doFilter(request,response);  
//     System.out.println("用户已经登陆,允许操作");  
    }  

 

    
  // Pass control on to the next filter
  //chain.doFilter(request, response);
  
     Map map = request.getParameterMap();
     Set set = map.entrySet();
     if(map!= null)
     {
      for(Iterator it = set.iterator();it.hasNext();)
      {
       Map.Entry entry = (Entry) it.next();
       if(entry.getValue() instanceof String[])
       {
        String[] values = (String[]) entry.getValue();
       //TransHtmlTag是spring中类文件.
        for(int i = 0 ; i < values.length ; i++)
         values[i] = TransHtmlTag.TransHtmlTag(values[i]);
         entry.setValue(values);
       }

      }
     }

 }

 /**
  * Place this filter into service.
  *
  * @param filterConfig
  *            The filter configuration object
  */
 public void init(FilterConfig filterConfig) throws ServletException {

  this.filterConfig = filterConfig;
  this.encoding = filterConfig.getInitParameter("encoding");
  String value = filterConfig.getInitParameter("ignore");
  if (value == null)
   this.ignore = true;
  else if (value.equalsIgnoreCase("true"))
   this.ignore = true;
  else if (value.equalsIgnoreCase("yes"))
   this.ignore = true;
  else
   this.ignore = false;

 }

 // ------------------------------------------------------ Protected Methods

 /**
  * Select an appropriate character encoding to be used, based on the
  * characteristics of the current request and/or filter initialization
  * parameters. If no character encoding should be set, return
  * <code>null</code>.
  * <p>
  * The default implementation unconditionally returns the value configured
  * by the <strong>encoding</strong> initialization parameter for this
  * filter.
  *
  * @param request
  *            The servlet request we are processing
  */
 protected String selectEncoding(ServletRequest request) {

  return (this.encoding);

 }

}// EOC

分享到:
评论

相关推荐

    Java Web实现session过期后自动跳转到登陆页功能【基于过滤器】

    本文主要介绍了Java Web实现session过期后自动跳转到登陆页功能,涉及java过滤器针对session的判断与跳转相关操作技巧。 一、建立基本过滤器 要实现session过期后自动跳转到登陆页功能,需要建立一个基本的过滤器...

    Session过期后自动跳转到登录页面的实例代码

    在本文中,我们将探讨如何在Session过期后自动将用户重定向到登录页面。 首先,开发者可能尝试使用Session监听器(HttpSessionListener)来检测Session的过期。监听器是Java Servlet规范的一部分,允许我们注册监听...

    Jsp中解决session过期跳转到登陆页面并跳出iframe框架的方法

    在JSP开发中,管理会话(session)的安全性是非常重要的,特别是在用户登录后的管理后台系统中。通常,我们需要在用户的会话超时后自动将用户重定向到登录页面,从而保证系统的安全。同时,在使用iframe框架布局的...

    项目访问时间过长session过期提醒

    "项目访问时间过长session过期提醒"这个话题涉及到的是如何配置Web应用程序,以便在用户的会话过期时自动跳转到特定的过期页面,提醒用户重新登录。下面我们将详细讲解实现这一功能的关键步骤和技术。 首先,我们...

    session过期处理

    - 自动跳转到登录页:服务器可以设置拦截器或过滤器,当检测到Session过期,直接重定向到登录页面。 - 提示信息:告知用户Session已过期,需要重新登录。 4. **Session与Cookie的关系**:Session依赖于Cookie来...

    ajax重写方法搭配后台filter返回status实现ajax请求跳转登录页面

    但在需要用户登录的场景下,服务器可能会返回一个非200的状态码,比如401(未经授权)或403(禁止访问)。这时,我们可以监听这些特定的状态码并做出相应的响应,例如跳转到登录页面。 ```javascript $.ajax({ url...

    重写 ajax 实现 session 超时跳转到登录页面实例代码

    在Web开发中,Session是服务器用来跟踪用户状态的一种机制,当用户登录后,服务器会创建一个Session对象并关联到客户端的浏览器。然而,如果用户长时间无操作,Session可能会因为超时而失效。在这种情况下,通常需要...

    Javaweb 登录页面自动跳转

    2. **会话管理**:在用户登录后,为了保持用户的登录状态,我们需要创建一个会话(session)。在JavaWeb中,这可以通过`HttpSession`接口实现。创建会话时,可以存储用户的登录信息,如用户ID,以便在后续请求中识别...

    shrio cas 集成,多登录页面的配置,session过期校验的定制

    4. **多登录页面配置**:如果项目中有多个不同的登录入口,需要配置多个CasFilter或者自定义过滤器,根据请求的不同路径跳转到相应的登录页面。 5. **Session过期校验的定制**:Shiro默认有自己的会话管理机制,但...

    J简单的springmvc包括拦截、session设置,超时跳转

    例如,在拦截器的`preHandle()`方法中,检查当前请求的Session是否有效,如果发现Session已经过期,就通过`response.sendRedirect(String url)`重定向到登录页面。 在这个项目中,开发者可能已经实现了这些功能,但...

    springboot 项目,基于springsession整合redis实现登录拦截功能

    在 Sticky Session 模式下,客户端的请求会尽可能地路由到创建该Session的服务器,只有在Session过期或服务器故障时才需要使用Redis中的Session数据。 综上所述,本项目展示了如何在Spring Boot应用中集成Spring ...

    webix+springmvc session超时跳转登录页面

    在用户会话超时时,通常需要将用户重定向至登录页面以重新验证身份。然而,对于使用Webix库进行AJAX请求的应用来说,直接在服务器端重定向到登录页面可能不会生效,因为异步请求不会更新整个页面。为了解决这个问题...

    登录超时给出提示跳到登录页面(ajax、导入、导出)

    在Web应用开发中,用户登录状态的管理是至关重要的。当用户长时间未操作或者系统检测到异常情况时,通常会设置登录超时机制,以保护用户账号的安全。本文将详细介绍如何实现登录超时后给出提示并跳转到登录页面,...

    Asp.net 中mvc 实现超时弹窗后跳转功能

    在Asp.net MVC框架中,实现用户登录超时后的弹窗提醒及自动跳转功能是一项常见的需求,这有助于提供更好的用户体验。以下是如何利用Cookie、过滤器(Filter)以及响应(Response)来实现这一功能的详细步骤。 首先...

    ajax提交session超时跳转页面使用全局的方法来处理

    如果检测到这是一个AJAX请求,过滤器会在响应头中添加一个自定义字段`sessionstatus`,并将值设为"timeout"。对于非AJAX请求,直接重定向到指定的登录页(例如`/test/index.jsp`)。 接下来,在客户端,我们需要...

    j2ee项目使用filter和memcached实现session服务器

    在J2EE应用程序中,处理用户会话是至关重要的,特别是在多服务器环境下,为了确保用户在不同服务器之间切换时能够保持其登录状态和购物车等信息。这就是session管理的用武之地。本项目通过结合使用Filter(过滤器)...

    实现用户自动登录

    本话题主要围绕"实现用户自动登录"展开,将详细介绍如何通过过滤器(Filter)实现这一功能,同时涉及自动登录的有效期设置、禁止黑名单用户登录以及JSTL(JavaServer Pages Standard Tag Library)技术的应用。...

Global site tag (gtag.js) - Google Analytics