gpt4 book ai didi

spring-mvc - 处理 session 超时?

转载 作者:行者123 更新时间:2023-12-04 02:21:57 24 4
gpt4 key购买 nike

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

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

spring-servlet.xml

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

网络.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 {
}

最佳答案

<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 中。

关于spring-mvc - 处理 session 超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18290301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com