0

我需要在登录尝试失败时捕获用户名,以进行分析以检测可能在我的应用程序登录时遇到问题的攻击和用户。

在所有控制器和操作的过滤器上,我使用域类 ActivityLog 创建了一个日志,其中存储了每个请求的所有请求/参数数据。当我尝试获取来自登录控制器的操作的用户名时,用户名不在参数上,也不在 j_username 上。

如何从过滤器获取登录失败时使用的用户名?

参数看起来像:[login_error:1, action:auth, controller:login]

我试图从 GrailsAnonymousAuthenticationToken 获取信息,但用户名是grails.anonymous.user

4

1 回答 1

0

简短的回答是您无法在过滤器中获取该信息。

长答案是使用 Grails 的 spring 安全核心插件中内置的事件来对失败的身份验证事件做出反应。

这是 Grails 2.4.x 应用程序的示例

// Config.groovy
grails.plugin.springsecurity.apf.storeLastUsername = true // this is now needed with updated version of spring security
grails.plugin.springsecurity.useSecurityEventListener = true
grails.plugin.springsecurity.onInteractiveAuthenticationSuccessEvent = { e, appCtx ->
    // Successful login stuff in here

}
grails.plugin.springsecurity.onAbstractAuthenticationFailureEvent = { e, appCtx ->
  // Failed login stuff in here
  // e.getAuthentication().getPrincipal() contains the principal object
}
于 2017-06-12T01:07:19.873 回答