Spring mvc 处理会话超时?
如何处理SpringMVC3.2中的会话超时,例如30分钟后,它应该重定向到index.html 尝试使用侦听器,但忽略了web.xml中指定的会话超时值 spring-servlet.xmlSpring mvc 处理会话超时?,spring-mvc,Spring Mvc,如何处理SpringMVC3.2中的会话超时,例如30分钟后,它应该重定向到index.html 尝试使用侦听器,但忽略了web.xml中指定的会话超时值 spring-servlet.xml <mvc:interceptors> <bean class="com.server.utils.AuthenticationTokenInterceptor" /> </mvc:interceptors> web.xml <ses
<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 {
}
30
@凌驾
公共布尔预处理(HttpServletRequest请求、HttpServletResponse响应、对象处理程序)引发异常{
尝试
{System.out.println(“内部拦截器”);
HttpSession session=request.getSession();
String authToken=(String)session.getAttribute(“userId”);
System.out.println(“为身份验证令牌调用的拦截器”);
if(authToken==null | | authToken.equals(“”)
{
System.out.println(“身份验证令牌超时”);
sendRedirect(servletContext.getContextPath()+“/login”);
返回false;
}
其他的
{
返回true;
}
}捕获(例外情况除外)
{
例如getMessage();
sendRedirect(servletContext.getContextPath()+“/login”);
返回false;
}
}
@凌驾
public void postHandle(HttpServletRequest请求,
HttpServletResponse,对象处理程序,
ModelAndView(ModelAndView)引发异常{
}
@凌驾
完成后公共无效(HttpServletRequest请求,
HttpServletResponse,对象处理程序,异常示例)
抛出异常{
}
也许使用普通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>
- 这段代码放在您的设置页面中,并使用“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中
那么到底发生了什么?用户发出请求,但未调用拦截器?或者服务器没有发出请求,但您希望页面自动重定向到/login?什么?描述你的场景。问题是拦截器被调用了。但是服务器没有发出请求。请描述您的场景。服务器接收请求,但不发出请求。我还是不明白。谢谢,我添加了sessionEvent.getSession()==null | | sessionEvent.getSession().equals(“”),但我无法重定向ti index.jsp。我们需要手动使会话无效吗?我需要检查会话的每个页面,然后重定向到索引页面?
<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>
$.timeoutDialog
({
timeout: 60 * 60,
countdown: 20,
logout_url: '@Url.Action("Logout", "Login")', restart_on_yes: true
});
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);
}