2012-06-28 57 views
2

我在應用程序中使用了Spring安全功能,但是我發現當會話過期時,所有請求ajax都返回頁面login.jsp(不是重定向,在http響應中,它把所有的html內容),這是我的web應用程序的登錄頁面。 我在我的應用程序中使用了很多ajax請求,目標是返回某些錯誤代碼,如510而不是登錄頁面。在基於Spring的Web應用程序中處理會話過期事件

<session-management session-authentication-strategy-ref="example" /> 

沒有無效會話的URL 我試圖使無效會話-URL = 「」,不能正常工作。 非常感謝

回答

3

使用自定義AuthenticationEntryPoint

package com.example.spring.security 
// imports here 

public class AjaxAwareAuthenticationEntryPoint 
    extends LoginUrlAuthenticationEntryPoint { 

    public AjaxAwareAuthenticationEntryPoint(final String loginFormUrl) { 
    super(loginFormUrl); 
    } 

    @Override 
    public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) 
     throws IOException, ServletException { 

    if ("XMLHttpRequest".equals(request.getHeader("X-Requested-With"))) { 
     response.sendError(403, "Forbidden"); 
    } else { 
     super.commence(request, response, authException); 
    } 
    } 
} 

bean定義和<http> element使用它作爲entry-point-ref

<http entry-point-ref="authenticationEntryPoint"> 
    <!-- more configuration here --> 
</http> 

<bean id="authenticationEntryPoint" 
    class="com.example.spring.security.AjaxAwareAuthenticationEntryPoint"> 
<constructor-arg value="/login.jsp"/> 
</bean> 
相關問題