使用 Grails Spring Security Rest 插件时如何获取客户端登录失败的原因?

Posted

技术标签:

【中文标题】使用 Grails Spring Security Rest 插件时如何获取客户端登录失败的原因?【英文标题】:How to get the reason of login failure in clients while using Grails Spring Security Rest plugin? 【发布时间】:2016-02-14 22:50:06 【问题描述】:

我们将 Grails Spring Security Rest 插件 1.5.2 与 Grails 2.5.2 一起使用。

不幸的是,它似乎没有报告任何登录失败的原因。我们从登录请求中得到的唯一信息是HTTP 401

我们有一种机制,在多次登录失败后,我们设置UseraccountLocked 属性,因此帐户被禁用/锁定。

我们希望至少获得有关登录失败原因的 JSON 响应,例如“用户帐户已锁定”、“凭据错误”、“没有这样的用户”等。

有没有办法用我们使用的当前版本来做到这一点?

【问题讨论】:

【参考方案1】:

插件不支持开箱即用。但是,您可以通过将 RestAuthenticationFailureHandler 替换为您自己的实现并将其连接到 resources.groovyrestAuthenticationFailureHandler 来轻松解决此问题。

【讨论】:

感谢您的回复。所以“异常”包含失败的详细信息,我需要将我的 JSON 消息添加到自定义 RestAuthenticationFailureHandler 中的“响应”对象中。对吗? 可以是任何org.springframework.security.core.AuthenticationException,例如org.springframework.security.authentication.LockedException 我做到了。它可以工作,但我无法在响应中添加任何正文...当我添加标头时它工作正常,但是当我尝试 response.getWriter().write("\"error\" 时,我从未在 response.body 中收到任何内容: $e.message");在我的自定义 restAuthenticationFailureHandler 任何想法? 从这里很难说。您是否还设置了响应的 Content-Type 标头?否则某些客户端可能无法读取正文

以上是关于使用 Grails Spring Security Rest 插件时如何获取客户端登录失败的原因?的主要内容,如果未能解决你的问题,请参考以下文章

Grails - grails-spring-security-rest - 无法从 application.yml 加载 jwt 机密

使用 Spring Security Grails 插件编码密码

Grails + spring-security-core:用户登录后如何分配角色?

在 Grails 中使用 Pre/Post Spring-Security Annotations

Grails 3.0 和 Spring Security

Grails - 卸载 Spring Security Core