在Spring MVC中设置会话超时
有没有办法在Spring中指定会话超时? 我无法在web.xml中指定它 。 因为我在控制器中使用会话范围bean如下
我通过spring xml文件配置了控制器。
class xyzController{ ABCSessionScopeClass objectWhichWillBeStoredInSession; }
我也不能用它
session.setMaxInactiveInterval(60*60);
有没有其他方法可以做到这一点。 我不介意在每个会话或同时为所有会话设置超时。
解决方案使用Pure Spring MVC,sevlet context.xml
处理程序适配器
@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
并为正常登录和remember-me指定authentication-success-handler。
@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); } }
我无法通过任何Spring配置文件找到任何方式来指定会话超时值。 我正在使用
bean,这样我就不必管理读/写值/对象到会话。 现在,我也希望在不使用servlet API的情况下设置会话超时值。 但看起来除了web.xml文件之外没有办法指定它。 因此最终使用servlet api request.getSession()
来设置超时时间。 我外化时间值,以便我可以轻松地更改它而无需重新编译代码。 如果有人找到更好的方法,请随时发布。 如果发现更好,我可以接受这个作为答案。