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

前端之家收集整理的这篇文章主要介绍了Spring Security&ExtJS-在会话超时时重定向到登录页面 前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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

  1. @Override
  2. protected void successfulAuthentication(HttpServletRequest request,HttpServletResponse response,Authentication authResult) throws IOException,ServletException {
  3. SavedRequestAwareAuthenticationSuccessHandler srh = new SavedRequestAwareAuthenticationSuccessHandler();
  4. this.setAuthenticationSuccessHandler(srh);
  5. srh.setRedirectStrategy(new RedirectStrategy() {
  6. @Override
  7. public void sendRedirect(HttpServletRequest httpServletRequest,HttpServletResponse httpServletResponse,String s) throws IOException {
  8. // do nothing,no redirect
  9. }
  10. });
  11. super.successfulAuthentication(request,response,authResult);
  12. HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(
  13. response);
  14. Writer out = responseWrapper.getWriter();
  15. out.write("{success:true}");
  16. out.close();
  17. }
最佳答案
您也许可以塑造以下内容以覆盖所有ajax请求,以测试超时的会话响应并相应地进行处理:

  1. var origHandleResponse = Ext.data.Connection.prototype.handleResponse;
  2. Ext.override(Ext.data.Connection,{
  3. handleResponse : function(response){
  4. var text = Ext.decode(response.responseText);
  5. if (<test for response that means the session timed out>)
  6. {
  7. var login = new Ext.Window({
  8. plain: true,closeAction: 'hide',modal: true,title: "Login timed out,please log in.",width: 400,autoHeight: true,items: [
  9. {
  10. xtype: 'form',id: 'login-form',items: [
  11. {
  12. xtype: 'textfield',fieldLabel: 'Username',name: 'username'
  13. },{
  14. xtype: 'textfield',inputType: 'password',fieldLabel: 'Password',name: 'password'
  15. }]
  16. }],buttons: [
  17. {
  18. text: 'Submit',handler: function() {
  19. Ext.getCmp('login-form').getForm().submit({url: '<login url>'});
  20. login.hide();
  21. }
  22. }]
  23. });
  24. login.show();
  25. }
  26. //else (optional?)
  27. origHandleResponse.apply(this,arguments);
  28. }

});

猜你在找的Spring相关文章