복붙노트

[SPRING] 스프링 보안 : BadCredentialsException 대신 LockedException이 발생합니다. 이유는 무엇입니까?

SPRING

스프링 보안 : BadCredentialsException 대신 LockedException이 발생합니다. 이유는 무엇입니까?

스프링 보안 프레임 워크를 사용하는 기본 사용자 인증의 경우 스프링 보안 DaoAuthenticationProvider

사용자가 올바른 사용자 이름으로 로그인하려고하면 잘못된 비밀번호와 사용자 계정이 이미 잠겨 있습니다. 그러면 스프링 보안 인증 모듈이 BadCredentialsException을 던질 것으로 예상됩니다. 대신 LockedException을 발생시킵니다.

내 질문은

어떤 도움을 주시면 감사하겠습니다. 인증 공급자 구현 코드

@Component("authenticationProvider")
public class LoginAuthenticationProvider extends DaoAuthenticationProvider {

    @Autowired
    UserDAO userDAO;

    @Autowired
    @Qualifier("userDetailsService")
    @Override
    public void setUserDetailsService(UserDetailsService userDetailsService) {
        super.setUserDetailsService(userDetailsService);
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        try {
            Authentication auth = super.authenticate(authentication);
            // if reach here, means login success, else exception will be thrown

            // reset the user attempts
            userDAO.resetPasswordRetryAttempts(authentication.getName());

            return auth;
        } catch (BadCredentialsException ex) {
            // invalid login, update user attempts
            userDAO.updatePasswordRetryAttempts(authentication.getName(), PropertyUtils.getLoginAttemptsLimit());
            throw ex;
        } catch (LockedException ex) {
            // this user is locked
            throw ex;
        } catch (AccountExpiredException ex) {
            // this user is expired
            throw ex;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw ex;
        }
    }

}

해결법

  1. ==============================

    1.너는 물었다 :

    너는 물었다 :

    스프링 보안은 먼저 계정이 존재하고 유효한지 확인한 후 암호를 확인하기 때문입니다.

    보다 구체적 : AbstractUserDetailsAuthenticationProvider.authenticate에서 수행됩니다. 아주 간단한 설명에서이 방법은 다음과 같이 작동합니다.

    user = retrieveUser(username, (UsernamePasswordAuthenticationToken) authentication);
    ...
    preAuthenticationChecks.check(user);
    additionalAuthenticationChecks(user, (UsernamePasswordAuthenticationToken) authentication);
    ...
    postAuthenticationChecks.check(user);
    

    좋은 점은 preAuthenticationChecks 및 postAuthenticationChecks가 인터페이스 UserDetailsChecker에 대한 참조이므로 변경할 수 있다는 것입니다. 그냥 자신 만의 두 UserDetailsChecker, pre에 대한 하나의 Null 구현과 모든 것을 확인하는 게시물 용으로 구현하십시오.

  2. from https://stackoverflow.com/questions/33969890/spring-security-lockedexception-is-thrown-instead-of-badcredentialsexception by cc-by-sa and MIT license