3

如何在 spring mvc 3.2 中处理会话超时,例如 30 分钟后它应该重定向到 index.html。

尝试使用拦截器,但 web.xml 中指定的会话超时值被忽略。

spring-servlet.xml

 <mvc:interceptors>   
   <bean class="com.server.utils.AuthenticationTokenInterceptor" />   
   </mvc:interceptors>

web.xml

<session-config>
    <session-timeout>30</session-timeout>
  </session-config>

 @Override  
    public boolean preHandle(HttpServletRequest request,HttpServletResponse response, Object handler) throws Exception {   
     try  
        {System.out.println("Inside Interceptor");   
            HttpSession session = request.getSession();   
            String authToken = (String) session.getAttribute("userId");   
               System.out.println("Interceptor invoked For Auth token");   
                if(authToken==null || authToken.equals(""))   
                {   
                    System.out.println("Auth Token time Out");   
                 response.sendRedirect(servletContext.getContextPath()+"/login");   
                    return false;   
                }   
                else  
                {   
                 return true;   
                }   
       }catch(Exception ex)   
           {   
           ex.getMessage();   
          response.sendRedirect(servletContext.getContextPath()+"/login");   
              return false;   
           }   
        }   


    @Override  
    public void postHandle(HttpServletRequest request,   
          HttpServletResponse response, Object handler,   
        ModelAndView modelAndView) throws Exception {   
   }   

   @Override  
 public void afterCompletion(HttpServletRequest request,   
         HttpServletResponse response, Object handler, Exception ex)   
            throws Exception {   
    }
4

2 回答 2

2
<system.web>
    <sessionState allowCustomSqlDatabase="true" mode="SQLServer"
    sqlConnectionString="SQLServerConnection" cookieless="false" timeout="60">
   </sessionState>
   <authentication mode="None" />
   <compilation debug="true" targetFramework="4.5" />
   <httpRuntime targetFramework="4.5" maxRequestLength="52428800" />
</system.web>

- 这段代码放在 web.config

  $.timeoutDialog
    ({
        timeout: 60 * 60,
        countdown: 20,
        logout_url: '@Url.Action("Logout", "Login")', restart_on_yes: true
    });
  • 此代码放入您的设置页面并使用“timeout_dialog.js”it.and 其他详细信息在 .js 文件中设置。

       public override void OnActionExecuting(ActionExecutingContext 
      filterContext)
    {
    
        if (filterContext.HttpContext.Session["UserID"] == null)
        {
            if (filterContext.HttpContext.Request.IsAjaxRequest())
            {
                filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                filterContext.Result = new JsonResult
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = new
                    {
                        Exception="error"
                    }
                };
            }
            else
            {
                filterContext.Result = new RedirectToRouteResult(
                new RouteValueDictionary
                {
                    { "controller", "Login" },
                    { "action", "Login" }
                });
                    //return;
            }
            return;
    
        }
        base.OnActionExecuting(filterContext);            
    }
    

    此代码放在通用类文件夹文件中的 filter.cs 中。

于 2017-06-14T13:41:37.397 回答
1

也许使用纯 Java EE 处理它比使用 Spring MVC 更好:javax.servlet.http.HttpSessionListener通知类型当前用户会话发生的所有更改,包括超时。要使用javax.servlet.http.HttpSessionListener,您需要在 中注册web.xml

<web-app ...>
        <listener>
        <listener-class>stuff.MySessionListener</listener-class>
    </listener>
</web-app>

并在您的班级中执行您的自定义逻辑。处理超时的方法是sessionDestroyed

package stuff;

import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MySessionListener implements HttpSessionListener {     

  @Override
  public void sessionCreated(HttpSessionEvent arg0) {
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent arg0) {
      //Your logic goes here
  } 
}
于 2013-08-18T00:13:43.390 回答