强制Spring安全性在帐户状态标志之前检查凭据

时间:2012-10-19 11:48:01

标签: java spring-security

我已经将org.springframework.security.core.userdetails.User子类化,并在我的构造函数中调用:

Super (String username,
        String password,
        boolean enabled,
        boolean accountNonExpired,
        boolean credentialsNonExpired,
        boolean accountNonLocked,
        GrantedAuthority[] authorities)

然后我使用${SPRING_SECURITY_LAST_EXCEPTION.message}在登录失败时显示结果。

我遇到的问题是,如果我将accountNotLocked设置为false,则会显示帐户锁定错误消息,无论密码是否正确,都会发生这种情况。如果首先启用spring验证凭据然后启用,accountNonExpired,credentialsNonExpired和accountNonLocked标志,我会更喜欢它。这样,只有在用户获得凭证时,才会通知用户他们的帐户已被锁定。

有没有办法强迫春天这样做?

1 个答案:

答案 0 :(得分:5)

我假设您使用的是最受欢迎的DaoAuthenticationProvider,问题出现在此类的UserDetailsChecker默认实现中。也就是说,在DaoAuthenticationProvider.additionalAuthenticationChecks之后移动所有支票应足以解决您的问题。请尝试以下配置:

<authentication-manager alias="authenticationManager">
  <authentication-provider ref="daoAuthenticationProvider" />
</authentication-manager>

<bean id="daoAuthenticationProvider"
      class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
  <property name="userDetailsService" ref="userDetailsService"/>
  <property name="passwordEncoder" ref="passwordEncoder"/>
  <property name="preAuthenticationChecks"
      class="com.example.NullUserDetailsChecker"/>
  <property name="postAuthenticationChecks"
      class="org.springframework.security.authentication.AccountStatusUserDetailsChecker"/>
</bean>

其中com.example.NullUserDetailsCheckerUserDetailsChecker的{​​{3}}实现(具有无效检查方法,无效)。