提问者:小点点

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


我已经子类化了org.springframework.security. core.user详细信息。用户,在我的构造函数中,我调用:

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

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

我有的问题是,如果我设置为false帐户锁定然后帐户锁定错误消息显示,无论密码是否正确都会发生这种情况。我更喜欢它,如果Spring验证凭据先,然后启用,帐户非过期,凭证非过期和帐户非锁定标志。这样,只有当用户获得正确的凭据时,才会通知他们的帐户被锁定。

有没有办法逼Spring这么做?


共1个答案

匿名用户

我假设您使用的是最流行的DaoAuthenticationProvider,并且问题出在此类的UserDetailsChecker默认实现中。也就是说,在DaoAuthenticationProvider. addtionalAuthenticationChecks之后移动所有检查应该足以解决您的问题。尝试以下配置:

<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的空对象模式实现(具有什么都不做的void检查方法)。