4

我已经继承了 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 标志,我会更喜欢它。这样,只有在他们获得正确的凭据时,才会通知用户他们的帐户已被锁定。

有没有办法强制弹簧这样做?

4

1 回答 1

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.NullUserDetailsChecker空对象模式的实现UserDetailsChecker(有 void check 方法,它什么都不做)。

于 2012-10-19T12:42:55.750 回答