【问题标题】:Throwing Custom Exception and Display Error Message from Custom AuthenticationProvider从自定义 AuthenticationProvider 抛出自定义异常并显示错误消息
【发布时间】:2011-09-18 21:29:37
【问题描述】:

这是对this question的跟进。

我有一个扩展 AbstractUserDetailsAuthenticationProvider 的自定义 AuthenticationProvider。在 AdditionalAuthenticationChecks 中,我正在做一些自定义身份验证工作,此过程的一部分是在登录屏幕上向用户显示一些消息。目前,为了测试,我创建了一个 UserNotActivatedException:

class UserNotActivatedException extends AuthenticationException {

  public UserNotActivatedException(String message, Throwable t) {
    super(message, t)
  }

  public UserNotActivatedException(String message) {
    super(message)
  }

  public UserNotActivatedException(String message, Object extraInformation) {
    super(message, extraInformation)
  }

}

在额外的AuthenticationChecks 中,我只是立即将其扔给测试。现在,我需要知道我需要做什么才能让我自己的失败消息显示在登录屏幕上。在 spring-security-core 默认配置中,我们可以覆盖以下内容:

errors.login.disabled = "Sorry, your account is disabled."
errors.login.expired = "Sorry, your account has expired."
errors.login.passwordExpired = "Sorry, your password has expired."
errors.login.locked = "Sorry, your account is locked."
errors.login.fail = "Sorry, we were not able to find a user with that username and password."

但我不知道如何添加自己的附加消息。

【问题讨论】:

    标签: grails spring-security


    【解决方案1】:

    看起来这些消息只是被生成到grails-app/controllersLoginControllerauthfail 操作使用。这是模板中的代码(在插件中):

    /**
     * Callback after a failed login. Redirects to the auth page with a warning message.
     */
    def authfail = {
    
        def username = session[UsernamePasswordAuthenticationFilter.SPRING_SECURITY_LAST_USERNAME_KEY]
        String msg = ''
        def exception = session[WebAttributes.AUTHENTICATION_EXCEPTION]
        if (exception) {
            if (exception instanceof AccountExpiredException) {
                msg = SpringSecurityUtils.securityConfig.errors.login.expired
            }
            else if (exception instanceof CredentialsExpiredException) {
                msg = SpringSecurityUtils.securityConfig.errors.login.passwordExpired
            }
            else if (exception instanceof DisabledException) {
                msg = SpringSecurityUtils.securityConfig.errors.login.disabled
            }
            else if (exception instanceof LockedException) {
                msg = SpringSecurityUtils.securityConfig.errors.login.locked
            }
            else {
                msg = SpringSecurityUtils.securityConfig.errors.login.fail
            }
        }
    
        if (springSecurityService.isAjax(request)) {
            render([error: msg] as JSON)
        }
        else {
            flash.message = msg
            redirect action: auth, params: params
        }
    }
    

    (来自 ~/.grails/1.3.7/projects/project-name/plugins/spring-security-core-1.1.2/src/templates/LoginController.groovy.template)

    您可能只需将您的 UserNotActivatedException 类型添加到那里的条件中。

    【讨论】:

    • 啊,很好。我会去试一试,看看会发生什么。
    • 你这个人!工作。今天完成所有这些工作后,我觉得一篇博客文章即将发布。 :o)
    • 很高兴它起作用了 :) - 我自己没有更改 authfail 操作,但它看起来很简单。最近我自己也在做一些自定义的 AuthenticationProvider 东西;我想这对我自己的使用可能会派上用场。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 1970-01-01
    • 2017-02-28
    • 2013-08-02
    • 2020-03-15
    相关资源
    最近更新 更多