grails - 抛出自定义异常并显示来自自定义 AuthenticationProvider 的错误消息

标签 grails spring-security

这是 this question 的后续内容.

我有一个扩展 AbstractUserDetailsAuthenticationProvider 的自定义 AuthenticationProvider。在 extraAuthenticationChecks 中,我正在执行一些自定义身份验证工作,此过程的一部分是在登录屏幕上向用户显示一些消息。目前,为了测试,我创建了一个 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)
  }

}

在additionalAuthenticationChecks 中,我立即将其投入测试。现在,我需要知道需要做什么才能让我自己的失败消息显示在登录屏幕上。在 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-app/controllers 中的 LoginControllerauthfail 操作使用。这是模板中的代码(在插件中):

/**
 * 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 类型添加到那里的条件中。

关于grails - 抛出自定义异常并显示来自自定义 AuthenticationProvider 的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6415092/

相关文章:

java - 在 grails Controller 中识别 ajax 请求或浏览器请求

spring-security - 是否有一种技术可以延迟 session 创建,直到用户成功登录?

java - Angular 4 + Spring Boot + Spring Security - 在 Angular 端存储 session

java - Spring Security用户角色及访问jsp

spring - 我如何在 Spring 中处理 Tomcat 的 MaxUploadSizeExceededException?

grails - 为什么Grails要求我在 Controller 中使用 `def`而不是 `void`?

grails - 如何使用 grails jasper 插件使用添加到 JasperReports 3.7.2 的表格组件?

spring-security - Spring Security SAML 使用多个子域访问同一个应用程序(同一个 IP)

grails - grails spring安全登录不起作用,尝试了以前的方法,不起作用

grails - 在每个测试用例之后是否可能进行Grails IntegrationSpec回滚?