迫使Spring Security在帐户状态标志之前检查凭据



我已经sublassed org.springframework.security.core.userdetails.user,在我的构造函数中,我称呼:

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

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

我遇到的问题是,如果我将帐户notlock设置为false,则会显示帐户锁定的错误消息,并且无论密码是否正确,都会发生这种情况。如果首先验证了凭证,然后启用,accountnonexpppeed,cordersnonexpppped和accountnonlocked标志,我将更喜欢它。这样,只有用户正确获得凭证,才能通知他们的帐户。

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

我假设您正在使用最流行的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是null对象模式的实现UserDetailsChecker(具有无用的void检查方法)。

最新更新