Contents

[Java]스프링부트 회원 기존 비밀번호 체크하기

Contents

회원 비밀번호 변경 로직을 작성중에 현재 비밀번호와 새 비밀번호를 입력받아, 기존 비밀번호가 맞는지 체크하는 로직을 넣고 싶었습니다.

String currentPassword = new BCryptPasswordEncoder().encode(request.getCurrentPassword());	

final User persistUser = userRepository.findUserByIdAndPassword(userId, currentPassword)
        .orElseThrow(() -> new EntityNotFoundException("회원정보를 찾을 수 없습니다."));
        
if(!currentPassword.equals(persistUser.getPassword())) {	
    logger.info("changePassword is Not Equal Current Password");	
    return new ResponseEntity<>(UserRegisterResult.ERROR.getResponseBody(),	
            HttpStatus.FORBIDDEN);	
}

new BCryptPasswordEncoder().encode(password);로 암호화 한 패스워드를 저장했기 때문에, 회원을 찾을때도 이렇게 하면 되겠다고 생각해서 위와 같은 코드를 작성하였는데, 테스트중 계속하여 EntityNotFoundException이 발생하였습니다.

디버깅으로 체크하였더니 String currentPassword = new BCryptPasswordEncoder().encode(request.getCurrentPassword()); 부분에서 매번 다른 비밀번호가 currentPassword에 들어갔습니다.

왜 다른 값이 나오는지 알고 싶어 BCryptPasswordEncoder 클래스 파일을 열어보았는데,

encode 부분에서 rawPasswordsalt값을 생성하여 두개의 값으로 패스워드를 해싱하고 있었습니다.

해당 클래스 파일안에 matches(CharSequence rawPassword, String encodedPassword)라는 함수가 있었고,

클래스의 인터페이스를 확인하였더니,

/**
 * Verify the encoded password obtained from storage matches the submitted raw
 * password after it too is encoded. Returns true if the passwords match, false if
 * they do not. The stored password itself is never decoded.
 *
 * @param rawPassword the raw password to encode and match
 * @param encodedPassword the encoded password from storage to compare with
 * @return true if the raw password, after encoding, matches the encoded password from
 * storage
 */
boolean matches(CharSequence rawPassword, String encodedPassword);

라는 주석을 확인하였고, 구현체가 아닌 인터페이스를 사용하고자 org.springframework.security.crypto.password.PasswordEncoder 를 의존성 주입하여,

final User persistUser = userRepository.findById(userId)
                .orElseThrow(() -> new EntityNotFoundException("회원정보를 찾을 수 없습니다."));
if(!passwordEncoder
                .matches(request.getCurrentPassword(), persistUser.getPassword())) {
    logger.info("changePassword is Not Equal Current Password");
    return new ResponseEntity<>(UserRegisterResult.ERROR.getResponseBody(),
            HttpStatus.FORBIDDEN);
}

이와 같이 수정하였고, 기대했던 결과대로 수행되었습니다.

아직 자바와 스프링이 많이 서툴러서 코드를 작성하는 시간보다 검색해보는 시간이 많아 더 어려운거 같습니다.

잘못된 부분이 있다면 코멘트 부탁드리겠습니다.