2015-10-13 97 views
3

我爲Grails應用程序使用Spring Security插件和Spring Security REST插件並嘗試實施密碼更改策略。在前端,當您未登錄時,我們的應用程序正在捕獲401錯誤,但密碼過期的情況給出了完全相同的HTTP響應。我期待看看是否有辦法改變它。如何定製Grails Spring Security REST登錄響應HTTP代碼

我可以捕獲AbstractAuthenticationFailureEvent並確定是否由於AuthenticationFailureCredentialsExpired事件,但我不知道如何讓該事件發送任何有意義的API,表明這不僅僅是失敗密碼登錄。

另外,我可以過期密碼並執行該策略,所以這不是問題。主要問題是該API的使用者如何區分失敗的用戶名/密碼挑戰和「您需要更改密碼」方案。

+0

這已經被問及在這裏回答:http://stackoverflow.com/a/33692352/2923710 –

回答

2

找到LoginController.groovy

下面是相關代碼authfail方法

 Exception exception = session[WebAttributes.AUTHENTICATION_EXCEPTION] 
     if (exception) { 
      if (exception instanceof AccountExpiredException) { 
       msg = g.message(code: "springSecurity.errors.login.expired") 
      } else if (exception instanceof CredentialsExpiredException) { 
       msg = g.message(code: "springSecurity.errors.login.passwordExpired") 
      } else if (exception instanceof DisabledException) { 
       msg = g.message(code: "springSecurity.errors.login.disabled") 
      } else if (exception instanceof LockedException) { 
       msg = g.message(code: "springSecurity.errors.login.locked") 
      } else { 
       msg = g.message(code: "springSecurity.errors.login.fail") 
      } 
     } 
1

我發現基於伯特貝克威思的這個職位響應的解決方案:Grails Spring Security Login/Logout Controllers not generated

Spring Security的REST插件有一個名爲RestAuthenticationFailureHandler的類,我將其複製到我的項目中並進行了重寫。調用onAuthenticationFailure方法時,我檢查錯誤的類型是否爲org.springframework.security.authentication.CredentialsExpiredException,如果是,則發出比預先配置的HTTP狀態碼更合適的HTTP狀態碼。