2013-08-17 136 views
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 { 
    } 
+0

那麼究竟發生了什麼呢?用戶發出請求,但攔截器不被調用?或者服務器沒有發出請求,但你希望頁面自動被重定向到/ login?什麼?描述你的場景。 –

+0

問題是攔截器被調用。但服務器不提出請求。 – user2692100

+0

描述你的場景。服務器收到請求,但沒有發出請求。我還是不明白。 –

回答

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 
    } 
} 
+0

謝謝,我添加了sessionEvent.getSession()== null || sessionEvent.getSession()。equals(「」)但我無法重定向ti index.jsp。我們是否需要手動使會話失效? 每一頁我需要檢查會話,然後重定向到索引頁面? – user2692100

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」它和其他細節設置在.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放入常見的類文件夾文件中。