我使用的是Grails 4.0.9
和spring security core grails plugin 4.0.3
,我搜索了一种方法来捕捉用户失败和成功的登录尝试,但我什么都没找到,有办法做到吗?
您可以注册ApplicationListener
,它侦听AbstractAuthenticationFailureEvent
来处理失败的尝试,并在grails-app/conf/spring/resources.groovy
中注册相同的尝试。例如:
package com.objectcomputing.example
import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import org.springframework.context.ApplicationListener
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent
@Slf4j
@CompileStatic
class LoginFailedAuthEventListener implements ApplicationListener<AbstractAuthenticationFailureEvent> {
@Override
void onApplicationEvent(AbstractAuthenticationFailureEvent event) {
final Object username = event.authentication.principal
log.warn("Failed login attempt for username {}", username)
}
}
更多信息,请访问https://grails-plugins.github.io/grails-spring-security-core/4.0.x/index.html#registeringEventListener
请注意上面的监听器是非常基本的,它只记录信息,但你可以写任何你需要做的事情。
示例应用程序:https://github.com/puneetbehl/grails-spring-security-demo