微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring Security&ExtJS-在会话超时时重定向到登录页面

我在Spring MVC / Security中使用ExtJS.我希望在会话过期后将用户重定向登录页面,并在Spring安全应用程序上下文中提供了该密码-

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

但是由于对服务器的调用全部基于AJAX,因此不会发生重定向.
请提出实现此目标的最佳方法.
我有一个用于AJAX登录自定义UserNamePasswordAuthenticationFilter:

@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();
    }
最佳答案
您也许可以塑造以下内容以覆盖所有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);
}   

});

原文地址:https://www.jb51.cc/spring/531706.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐