오늘은 NewsFeedProject User정보 수정기능을 만들었다.
패스워드를 변경할 때, 비밀번호를 입력받아서 맞는지 확인하고 비밀번호를 변경해준다.
그리고 패스워드를 인코딩 해서 비교하고 등록해줘야한다. ★패스워드 암호화★
@Configuration
public class PasswordConfig {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
패스워드 암호화를 위해선 Encoder가 필요한데 Encoder는 위와같이 먼저 config를 이용해 설정해둔다.
private final PasswordEncoder passwordEncoder;
//Bean으로 동록된 passwordEncoder를 생성자에서 가져오기
String password = passwordEncoder.encode(pwdRequestDto.getPassword());//패스워드 인코딩
위와같이 String타입으로 받아온 password를 인코딩하여 암호화해서 저장해주어야한다.
@Transactional
public UserResponseDto updatePassword(PwdRequestDto pwdRequestDto, UserDetailsImpl userDetails) { //확일할 패스워드와 유저정보 같이 받아옴
//로그인 되어있는지 확인하는 if문 추가 JWT확인
String password = passwordEncoder.encode(pwdRequestDto.getPassword());//패스워드 인코딩
String modifyPassword = passwordEncoder.encode(pwdRequestDto.getModifyPassword());//패스워드 인코딩
if(password.equals(userDetails.getPassword())){ //입력한 패스워드와 유저의 패스워드가 맞는지 확인
UserResponseDto userResponseDto = new UserResponseDto(userDetails.getUsername(),modifyPassword,
userDetails.getIntroduce(),userDetails.getProfileName()); //Dto파일 새로 입력한 패스워드로 생성
User user = findUser(userDetails.getId()); //id를 이용해 user찾기
user.update(userResponseDto); //해당Dto대로 user 업데이트
//쿠키 제거 해주기 Logout메서드를 부르면 해결될 듯 하다.
return userResponseDto; //Dto 리턴
}
throw new IllegalArgumentException("수정확인을 위해 필요한 비밀번호가 틀립니다.");
//여기까지 로그인 확인
}
오늘 작성한 Service 코드이다.
패스워드를 받아와서 패스워드를 현재 로그인한 사용자 패스워드와 비교해서 확인하고 바꿀 패스워드로 사용자 패스워드를 변경해준다.