簡體   English   中英

如何自定義Grails Spring Security REST登錄響應HTTP代碼

[英]How to customize Grails Spring Security REST login response HTTP code

我正在為Grails應用程序使用Spring Security插件和Spring Security REST插件,並嘗試實現密碼更改策略。 在前端,我們的應用程序在您未登錄時捕獲401錯誤,但是您的密碼已過期的情況會給出完全相同的HTTP響應。 我正在尋找是否有辦法改變它。

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

順便說一句,我可以使密碼到期並執行該策略,因此這不是問題。 主要問題是此API的使用者如何區分失敗的用戶名/密碼質詢和“您需要更改密碼”方案。

找到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")
            }
        }

我找到了一個基於Burt Beckwith對此帖子的回復的解決方案: Grails Spring安全登錄/注銷控制器未生成

Spring安全REST插件有一個名為RestAuthenticationFailureHandler的類,我將其復制到我的項目中並重寫。 當調用onAuthenticationFailure方法時,我檢查錯誤的類型是否為org.springframework.security.authentication.CredentialsExpiredException,如果是,則發出比預配置的更合適的HTTP狀態代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM