2013-06-24 65 views
5

我已經啓用REST支持我的春天MVC應用程序與作爲Spring Security認證入口點

<http auto-config="false" use-expressions="true" 
      disable-url-rewriting="true" entry-point-ref="restAuthenticationEntryPoint"> 

我的安全context.xml的設置AuthenticationEntryPoint的RestAuthenticationEntryPoint.java

@Component 
public final class RestAuthenticationEntryPoint implements AuthenticationEntryPoint { 

    @Override 
    public void commence(final HttpServletRequest request, final HttpServletResponse response, final AuthenticationException authException) throws IOException { 
     response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized"); 
    } 

} 

每當任何用戶試圖訪問資源而無需驗證它會給出以下錯誤:

HTTP Status 401 - Unauthorized 

以上行爲僅適用於Rest服務。但是,我想有一個默認的行爲,如果用戶沒有通過身份驗證,將用戶重定向到正常web請求的登錄頁面。如何實現這一目標?

+0

我覺得這個鏈接可以幫助 [擅自處理錯誤消息在Spring Security基本身份驗證(http://stackoverflow.com/questions/4397062/handle-unauthorized-基於身份驗證的彈簧安全性錯誤消息) –

+0

另請參閱:http://stackoverflow.com/questions/23739746/howto-configure-spring-security-to-return-403-for-rest-網址和重定向到LOGI – yankee

回答