๋จผ์ build.gradle์ ๋ค์ด๊ฐ์
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.jasypt:jasypt:1.9.3'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
}
์ implementation 'org.jasypt:jasypt:1.9.3' ์ถ๊ฐ
๊ธฐ์กด์ ํผ๋ก๊ทธ์ธ์๋น์ค์ private final PasswordEncryptor passwordEncryptor; ์ถ๊ฐ
๋น๋ฐ๋ฒํธ ์ผ์น์ฌ๋ถ๋ฅผ
// ๋ฑ๋ก๋ ํ์์ด ์๋ ๊ฒฝ์ฐ ๋น๋ฐ๋ฒํธ ์ผ์น ์ฌ๋ถ ํ์ธ
Member member = optionalMember.get();
if (!member.getPassword().equals(password)) {
// ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ
throw new BusinessException(ErrorCode.INVALID_PASSWORD); }
์์
// ๋ฑ๋ก๋ ํ์์ด ์๋ ๊ฒฝ์ฐ ๋น๋ฐ๋ฒํธ ์ผ์น ์ฌ๋ถ ํ์ธ
Member member = optionalMember.get();
if (!passwordEncryptor.checkPassword(password, member.getPassword())) {
// ๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์๋ ๊ฒฝ์ฐ
throw new BusinessException(ErrorCode.INVALID_PASSWORD);
}
๋ก ๋ณ๊ฒฝํ๊ณ
ํ์๊ฐ์ ์ฒ๋ฆฌ๋ฅผ
// ํ์๊ฐ์
์ฒ๋ฆฌ
Member newMember = Member.builder()
.email(signupRequest.getEmail())
.password(signupRequest.getPassword())
.memberName(signupRequest.getMemberName())
.memberType(signupRequest.getMemberType())
.role(signupRequest.getRole())
์์
// ํ์๊ฐ์
์ฒ๋ฆฌ
String encryptedPassword = passwordEncryptor.encryptPassword(signupRequest.getPassword());
Member newMember = Member.builder()
.email(signupRequest.getEmail())
.password(encryptedPassword)
.memberName(signupRequest.getMemberName())
.memberType(signupRequest.getMemberType())
.role(signupRequest.getRole())
๋ก ๋ณ๊ฒฝํ๋ค.
์๋ ํด๋์ค๋ฅผ ์์ฑํด์ค๋ค.
import org.jasypt.util.password.StrongPasswordEncryptor;
import org.springframework.stereotype.Component;
@Component
public class PasswordEncryptor {
private static final StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
public static String encryptPassword(String password) {
return passwordEncryptor.encryptPassword(password);
}
public static boolean checkPassword(String plainPassword, String encryptedPassword) {
return passwordEncryptor.checkPassword(plainPassword, encryptedPassword);
}
'๐ผ๋ฐฑ์ค๋ > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์์์ฑ ๊ด๋ฆฌ (0) | 2023.12.22 |
---|---|
JPQL, JPA (1) | 2023.12.22 |
JPA๋? (0) | 2023.12.22 |
์๋ฐ ๋น๋ ํจํด์ด๋? (2) | 2023.12.20 |
@ControllerAdvicem, @RestControllerAdvice (0) | 2023.12.20 |