2011-04-19 96 views
3

我在Spring MVC/Security中使用ExtJS。我希望用户被重定向到登录页面时,会议已过期,我给这个在Spring安全应用程序上下文 -Spring Security&ExtJS - 在会话超时时重定向到登录页面

<session-management invalid-session-url="/login.jsp"></session-management> 

但由于呼叫服务器都基于AJAX的一切,重定向不不会发生。 请建议实施此的最佳方式。 我有一个自定义UserNamePasswordAuthenticationFilter实施了AJAX登录:

@Override 
    protected void successfulAuthentication(HttpServletRequest request, 
     HttpServletResponse response, Authentication authResult) throws IOException, 
     ServletException { 
     SavedRequestAwareAuthenticationSuccessHandler srh = new SavedRequestAwareAuthenticationSuccessHandler(); 
     this.setAuthenticationSuccessHandler(srh); 
     srh.setRedirectStrategy(new RedirectStrategy() { 
      @Override 
      public void sendRedirect(HttpServletRequest httpServletRequest, 
       HttpServletResponse httpServletResponse, String s) throws IOException { 
       // do nothing, no redirect 
      } 
     }); 
     super.successfulAuthentication(request, response, authResult); 

     HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(
     response); 
     Writer out = responseWrapper.getWriter(); 
     out.write("{success:true}"); 
     out.close(); 
    } 
+0

也许与http://stackoverflow.com/questions/3930646/session-timeout-response-in-ajax有关 – Raghuram 2011-04-20 04:40:45

回答

3

您可能能够塑造下面覆盖所有Ajax请求来测试超时会话响应,并相应地处理它:

var origHandleResponse = Ext.data.Connection.prototype.handleResponse; 
Ext.override(Ext.data.Connection, { 
handleResponse : function(response){ 
    var text = Ext.decode(response.responseText); 
    if (<test for response that means the session timed out>) 
    { 
      var login = new Ext.Window({ 
       plain: true, 
       closeAction: 'hide', 
       modal: true, 
       title: "Login timed out, please log in.", 
       width: 400, 
       autoHeight: true, 
       items: [ 
       { 
        xtype: 'form', 
        id: 'login-form', 
        items: [ 
        { 
         xtype: 'textfield', 
         fieldLabel: 'Username', 
         name: 'username' 
        }, 
        { 
         xtype: 'textfield', 
         inputType: 'password', 
         fieldLabel: 'Password', 
         name: 'password' 
        }] 
       }], 
       buttons: [ 
       { 
        text: 'Submit', 
        handler: function() { 
         Ext.getCmp('login-form').getForm().submit({url: '<login url>'}); 
         login.hide(); 
        } 
       }] 
      }); 
      login.show(); 
    } 
    //else (optional?) 
    origHandleResponse.apply(this, arguments); 
} 

});

相关问题