grails - SpringSecurity,如何记录由于启用==假而失败的尝试,grails 2.5

标签 grails logging spring-security

我们的身份验证域对象 Operator,在其上启用了 bool 值。
当我们将此设置为 false 时,SpringSecurity + grails 会神奇地使登录尝试失败,并显示消息“对不起,您的帐户已禁用”。
我们想记录这样的尝试。据推测,有某种监听器或处理程序不需要我们实现逻辑(即 grails 只是通知我们用户被拒绝,而不是我们现在必须决定是否应该拒绝用户)。
我们已经记录了失败的密码检查,如果它们因为登录尝试次数过多而失败 - 我们不希望它捕获这些事件,只是由于 enabled == false 而被拒绝.

最佳答案

是的,您可以轻松实现。首先修改你的Config.groovy以便它可以重定向到登录失败时的特定操作:

grails.plugin.springsecurity.failureHandler.defaultFailureUrl = "/login/authfail"
// For AJAX based authentication
grails.plugin.springsecurity.failureHandler.ajaxAuthFailUrl = "/login/authfail"

现在定义一个 LoginController有行动authfail如下:
def authfail() {
    String msg = ""
    Exception exception = session[WebAttributes.AUTHENTICATION_EXCEPTION]

    // print the kind of message you want either due to account locked, enabled = false, password expired = true and others
    log.debug "User login failed due to [${exception?.message}]"
    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")
        }
    }

    // redirect or respond acccordingly
}

关于grails - SpringSecurity,如何记录由于启用==假而失败的尝试,grails 2.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32561627/

相关文章:

javascript - 如何让这段代码在现代nodejs中运行?

tomcat - 在 Tomcat 上运行时如何从存储在 httpsession 上的 map 中获取值

grails - 如何减少 Grails 中 PermGen 空间的使用

hibernate - 对旧数据库使用Grails/Hibernate。如何创建缺少的功能?

java - 奇怪的 "18 chars"LOGBack 行为

spring - 创建 session 无状态使用

spring-mvc - 如何为 Spring AbstractPreAuthenticatedProcessingFilter 连接 AuthenticationManager

java - 使用 RequestHeaderAuthenticationFilter 时如何测试 Internet Explorer?

java - 为什么IntelliJ IDEA无法确定Java版本?

grails - grails gorm级联保存最佳实践