使用纯弹簧 MVC 的解决方案,sevlet 上下文.xml
<mvc:interceptors>
<bean class="com.xxx.SessionHandler" />
</mvc:interceptors>
处理程序适配器
@Component
public class SessionHandler extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
request.getSession().setMaxInactiveInterval(60*60);
return true;
}
}
假设您正在使用弹簧安全性,
对于每个成功的登录,我认为最好的方法是创建和指定身份验证 - 成功 - 处理程序用于正常登录以及记住我。LoginSuccessHandler
@Service
public class LoginSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
@Override
public void onAuthenticationSuccess(
HttpServletRequest request,
HttpServletResponse response,
Authentication authentication) throws ServletException, IOException {
request.getSession().setMaxInactiveInterval(60*60);
super.onAuthenticationSuccess(request, response, authentication);
}
}
<http auto-config="true" use-expressions="true">
<form-login login-page="/login"
authentication-failure-url="/login.hst?error=true"
**authentication-success-handler-ref="loginSucessHandler"** />
<logout invalidate-session="true" logout-success-url="/home" logout-url="/logout" />
<remember-me key="jbcp" **authentication-success-handler-ref="loginSucessHandler"**/>
<session-management>
<concurrency-control max-sessions="1" />
</session-management>
</http>