【问题标题】:Spring MVC redirect to page with custom parameter when session expired会话过期时,Spring MVC 重定向到带有自定义参数的页面
【发布时间】:2016-05-15 16:12:59
【问题描述】:

当会话超时时,我找不到任何解决方案让 Spring 连同参数一起重定向到页面。我需要重定向到登录页面,出现“会话已过期” 之类的错误。我已经尝试过过滤器和方法session.isNew()。但它不起作用,因为当请求到达登录页面时,它总是已经有会话。 HttpSessionEvent 处理程序也不起作用,因为不允许访问请求属性并重定向到页面。

【问题讨论】:

    标签: spring session redirect session-timeout


    【解决方案1】:

    最简单的方法是创建一个拦截器,将 Refresh 标头添加到每个响应中,并在会话到期后的某个时间。

    public class RefreshInterceptor extends HandlerInterceptorAdapter {
    
        @Override
        public void postHandle (
            HttpServletRequest request, 
            HttpServletResponse response, 
            Object handler, 
            ModelAndView modelAndView
        ) throws Exception {
    
          //if session != null and user is authenticated then...
          response.setIntHeader("Refresh", figureOutWhenSessionExpires() + A_SMALL_DELAY );
    
          super.postHandle(request, response, handler, modelAndView);
    
        }
    }
    

    【讨论】:

    • 但这不会将我重定向到任何地方,但会延长会话。这不是我想要的。
    • @ovod 它告诉浏览器在会话到期后重新加载当前页面。如果页面是安全的,服务器会将浏览器重定向到登录页面
    猜你喜欢
    • 2014-06-05
    • 2011-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-19
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多