Spring Security 설정 후 로그인을 해보면 아이디나 비밀번호가 틀려도 모두 BadCredentialException만 발생해서 두 가지 에러상황을 따로 처리할 수 없다. 해당 이슈를 구분할 수 있는 setHideUserNotFoundException설정이 기본 true로 설정되어있어 그렇다고한다. 더 강한 보안을 위해서 그렇다고 하는데 잘 이해가 안간다... 

 

암튼 이 설정값은 AuthenticationProvider 생성 시에 false로 수정할 수 있다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
    DaoAuthenticationProvider bean = new DaoAuthenticationProvider();
    bean.setHideUserNotFoundExceptions(false);
    bean.setUserDetailsService(userDetailsService);
    bean.setPasswordEncoder(passwordEncoder());
    
    return bean;
}
 
@Override 
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.authenticationProvider(this.daoAuthenticationProvider());
}
cs

Security Config 설정파일에서 AuthenticationProvider 생성 시 해당 값을 false로 지정한 뒤 적용한다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
public void onAuthenticationFailure(
    HttpServletRequest request, 
    HttpServletResponse response, 
    AuthenticationException e) throws IOException, ServletException {
 
    String sErrMsg = "";
    if (e instanceof UsernameNotFoundException) {
        sErrMsg = "존재하지 않는 사용자입니다.";
    } else if (e instanceof BadCredentialsException) {
        sErrMsg = "비밀번호가 틀립니다.";
    } else {
        sErrMsg = "기타 인증오류입니다.";
    }
    
    ...
}
cs

AuthenticationFailureHandler를 구현한 핸들러에서 UsernameNotFoundException을 처리할 수 있다.

 

참고)

cyr9210.github.io/2019/09/30/Security/usernamenotfound/

 

인증 시, UsernameNotfoundException 발생 안함 문제(BadCredentials Exception만 발생)

SpringSecurityDebug인증 시, UsernameNotfoundException 발생 안함 문제(BadCredentials Exception만 발생)

cyr9210.github.io

www.programmersought.com/article/8509702354/

 

2019-01-10 springboot + spring-security The error message when logging in is "Bad Credential" instead of specific information...

In the background, if the username is incorrectly logged in, there is a specific error exception message returned. But in the front-end interface, I only saw "Bad Credential" instead of specific error messages. Keep track of the code and find the problem h

www.programmersought.com

codevang.tistory.com/268

 

스프링 Security_로그인_로그인 실패 대응 로직 [3/9]

- Develop OS : Windows10 Ent, 64bit - WEB/WAS Server : Tomcat v9.0 - DBMS : MySQL 5.7.29 for Linux (Docker) - Language : JAVA 1.8 (JDK 1.8) - Framwork : Spring 3.1.1 Release - Build Tool : Maven 3.6..

codevang.tistory.com

 

Posted by 셋부터넷
,