반응형
Hash 기술을 이용한 암호화 처리
1. security에서 제공하는 암호화 클래스 사용
2. 회원가입 시 회원 비밀번호 암호화 처리
3. 로그인 시 암호화 된 비번 확인 기능 추가 및 수정
암호화 의존성 추가
implementation 'org.springframework.security:spring-security-crypto'
MySQL 오류 확인 - ControllerAdvice 확인
DataIntergrityViolationException
→ 잘못된 데이터가 바인딩 되었을 때 발생하는 에러
암호화 테스트
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class PasswordEncoderTest { public static void main(String[] args) { // 기능 확인 String password = "p1234"; BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); String hashedPassword = passowrdEncoder.encode(password); System.out.pritnln("원래 비밀번호 : " + password); System.out.pritnln("암호화된 비밀번호 : " + hashedPassword); // 사용자 요청 값 : p1234 // DB 기록된 값 : e2IBgb3EpCyvuVIczGLHyOsBk5bhP5bFKaQmO8CVix84M3JDuvrzy boolean isMatched = passwordEncoder.matches("aaaaa", hashedPassword); System.out.println("비밀번호 일치 여부 : " + isMatched); } } | cs |
WebMvcConfig 기능 추가
1 2 3 4 5 6 7 8 9 | @Configuration // IoC 등록 - 2개 이상 빈으로 등록 될 때 사용 public class WebMvcConfig implements WebMvcConfigurer { @Bean // IoC 관리 대상 - 싱글톤 public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } | cs |
UserService 암호화 기능 추가
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | @Service public class UserService { @Aurowired // DI 처리 private UserRepository userRepository; @Autowired // DI 처리 - WebMvcConfig에서 IoC 처리함 private PasswordEncoder passwordEncoder; // 회원가입 서비스 처리 @Transactional public void createUser(SignUpFormDto signUpFormDto) { String rawPw = signUpFormDto.getPassword(); String hashPw = passwordEncoder.encode(rawPw); signUpFormDto.setPassword(hashPwd); // 객체 상태 변경 int result = userRepository.insert(signUpFormDto); if(result != 1) { throw new CustomRestfullException("회원 가입 실패", HttpStatus.INTERNAL_SERVER_ERROR); } } // 로그인 서비스 처리 @Transcational public User signIn(SignInFormDto signInFormDto) { // User userEntity = userRepository.findByUsernameAndPassword(signInFormDto); // if(userEntity == null) { // throw new CustomRestfullException("아이디 혹은 비번이 틀렸습니다.", HttpStatus.INTERNAL_SERVER_ERROR); // } // signInFormDto.getPassword()는 원래 비밀번호 // userEntity.getPassword()는 암호화 된(해쉬 처리 된) 비밀번호 boolean isPwMatched = passwordEncoder.matches(signInFormDto.getPassword(), userEntity.getPassword()); if(isPwMatched == false) { throw new CustomRestfullException("비번이 틀렸습니다.", HttpStatus.INTERNAL_SERVER_ERROR); } return userEntity; } } | cs |
→ signInFormDto.getPassword()는 원래 비밀번호
userEntity.getPassword()는 암호화 된(해쉬 처리 된) 비밀번호
mysql 제약 수정
ALTER TABLE user_tb MODIFY COLUMN password VARCHAR(100) NOT NULL;
→ 암호화 된 비밀번호는 아래와 같이 입력되기 때문에 VARCHAR(100)으로 수정
UserRepository
1 2 3 4 5 6 7 8 | @Mapper public interface UserRepository { public User findByUsernameAndPassword(SingInFormDto signInFormDto); // 암호화 적용 기능 추가 public User findByUsername(SignInFormDto signInFormDto); } | cs |
→ 암호화를 사용하기 전에는 username과 password를 갖고 와서 사용하였고
암호화를 적용한 후에는 password가 암호화 처리 되었으니 username만 갖고 와서 로그인 처리함.
user.xml
<select id="findByUsername" resultType="com.tenco.bank.repository.model.User">
select * from user_tb where username = #{username}
</select>
반응형
'프로그래밍 > Spring Boot' 카테고리의 다른 글
Spring Boot_파일 업로드, 리소스 처리 ResourceHandler (0) | 2023.04.24 |
---|---|
Spring Boot_javax.el.PropertyNotFoundException (0) | 2023.04.24 |
Spring Boot_마이그레이션(migration) H2 데이터베이스 → MySQL 데이터베이스 (0) | 2023.04.21 |
Spring Boot_예외 처리 기술 (Exception) (0) | 2023.04.18 |
Spring Boot_MyBatis 설정 (0) | 2023.04.18 |