Skip to content

Commit

Permalink
-fix: SecureRandom을 사용한 난수생성으로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jieun5119 committed Oct 10, 2024
1 parent a8e2a3d commit 6a4c3c2
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/main/java/com/sscanner/team/user/service/SmsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.sscanner.team.user.requestDto.SmsVerifyRequestDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.security.SecureRandom;

@RequiredArgsConstructor
@Service
Expand All @@ -18,16 +19,24 @@ public class SmsService {
private final SmsRepository smsRepository;
private final UserRepository userRepository;

private static final SecureRandom secureRandom = new SecureRandom();

public void SendSms(SmsRequestDto smsRequestDto) {
String phoneNum = smsRequestDto.phoneNum();

if (userRepository.findByPhone(phoneNum).isPresent()) {
throw new BadRequestException(ExceptionCode.DUPLICATED_PHONE);
}

String certificationCode = Integer.toString((int)(Math.random() * (999999 - 100000 + 1)) + 100000); // 인증 코드(6자리랜덤)
smsCertificationUtil.sendSMS(phoneNum, certificationCode);
smsRepository.createSmsCertification(phoneNum, certificationCode);
int certificationCode = secureRandom.nextInt(900000) + 100000; // 100000 ~ 999999 범위의 난수
String codeAsString = Integer.toString(certificationCode);

// SMS 전송
smsCertificationUtil.sendSMS(phoneNum, codeAsString);

// 인증 코드 저장
smsRepository.createSmsCertification(phoneNum, codeAsString);

}

public boolean verifyCode(SmsVerifyRequestDto smsVerifyDto) {
Expand All @@ -39,9 +48,9 @@ public boolean verifyCode(SmsVerifyRequestDto smsVerifyDto) {
}
}

public boolean isVerify(String phoneNum, String code) {
return smsRepository.hasKey(phoneNum) && // 전화번호에 대한 키가 존재하고
smsRepository.getSmsCertification(phoneNum).equals(code); // 저장된 인증 코드와 입력된 인증 코드가 일치하는지 확인
public boolean isVerify(String phoneNum, String code) { // 전화번호에 대한 키 존재 + 인증코드 일치 검증
return smsRepository.hasKey(phoneNum) &&
smsRepository.getSmsCertification(phoneNum).equals(code);
}

}

0 comments on commit 6a4c3c2

Please sign in to comment.