3

我正在使用 springsecurity core 1.2 和 spring security ui 0.2。在使用 registercontroller 的默认设置中,用户可以将 accountLocked 设置为 true:

    def user = new User(username: username,
            enabled: true,
            password: password,
            accountExpired: false,
            accountLocked: true,
            passwordExpired: false)

在 RegisterController.register 中,将向新用户发送一封确认电子邮件。电子邮件到达,用户点击电子邮件中的链接,RegisterController.verifyRegistration 为用户设置 accountLocked = false。

这都是开箱即用的默认行为,因此我不会列出所有相关代码。默认行为是在 accountLocked = false 之前不允许用户登录。如果您注册然后稍后再回来登录,登录表单上会显示“帐户锁定”错误。

即使 accountLocked 为真,我也希望允许用户登录(这意味着他们没有点击确认电子邮件中的验证链接)。在不添加我自己的“myAccountLocked”字段的情况下如何做到这一点?

我希望能够基于 accountLocked 控制用户的某些帐户活动,但他们仍然应该能够在单击电子邮件中的验证链接之前登录以访问其帐户的某些部分。

我对默认安装所做的一项更改是我在注册过程中创建用户,而不是在验证过程中。我的验证过程只是将 accountLocked 标志设置为 false,仅此而已。

4

1 回答 1

4

Spring Security Core 会进行一些前置和后置身份验证检查。锁定验证在DefaultPreAuthenticationChecks类中完成,因此您需要替换它:

class MyPreAuthenticationChecks implements UserDetailsChecker {
    protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();
    protected final Logger log = LoggerFactory.getLogger(getClass());

    public void check(UserDetails user) {
        if (!user.isEnabled()) {
            log.debug("User account is disabled");

            throw new DisabledException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.disabled",
        "User is disabled"), user);
        }

        if (!user.isAccountNonExpired()) {
            log.debug("User account is expired");

            throw new AccountExpiredException(messages.getMessage("AbstractUserDetailsAuthenticationProvider.expired",
        "User account has expired"), user);
        }
    }
}

在 resources.groovy 中,你声明 bean 使用你的类而不是 Spring 的类:

beans = {
  preAuthenticationChecks(MyPreAuthenticationChecks)
}
于 2013-10-22T20:25:41.407 回答