gpt4 book ai didi

grails - 如何自定义Grails Spring Security REST登录响应HTTP代码

转载 作者:行者123 更新时间:2023-12-01 13:49:21 26 4
gpt4 key购买 nike

我正在为 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")
}
}

关于grails - 如何自定义Grails Spring Security REST登录响应HTTP代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33112928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com