3

我正在为 Grails 应用程序使用 Spring Security 插件和 Spring Security REST 插件,并尝试实施密码更改策略。在前端,当您未登录时,我们的应用程序会捕获 401 错误,但您的密码已过期的情况会给出完全相同的 HTTP 响应。我正在寻找是否有办法改变它。

我可以捕获 AbstractAuthenticationFailureEvent 并确定它是否是由于 AuthenticationFailureCredentialsExpired 事件造成的,但我不知道如何让该事件从 API 中发送任何有意义的信息,表明这不仅仅是密码登录失败。

顺便说一句,我可以使密码过期并执行该策略,所以这不是问题。主要问题是此 API 的使用者如何区分失败的用户名/密码质询和“您需要更改密码”场景。

4

2 回答 2

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")
            }
        }
于 2015-10-14T03:19:06.967 回答
1

我根据 Burt Beckwith 对这篇文章的回复找到了一个解决方案: 未生成 Grails Spring Security Login/Logout Controllers

spring security REST 插件有一个名为 RestAuthenticationFailureHandler 的类,我将其复制到我的项目中并重写了。当调用 onAuthenticationFailure 方法时,我会检查错误的类型是否为 org.springframework.security.authentication.CredentialsExpiredException,如果是,我会发出比预配置更合适的 HTTP 状态代码。

于 2015-10-14T15:29:13.183 回答