-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/#12 이메일 찾기 SMS 인증 구현 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
24c0eec
775a1d8
07e58a7
6f968ee
65fb560
bd3f08c
4afbd2b
1c614ba
478caf3
99d0766
7af6ece
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.application.dto.request; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Pattern; | ||
|
|
||
| public class SmsRequest { | ||
| public record SmsSendRequest( | ||
| @NotBlank(message = "전화번호는 필수입니다.") | ||
| @Pattern(regexp = "^\\d{10,11}$", message = "전화번호는 하이픈 없이 10~11자리 숫자만 입력해주세요.") String phoneNumber) { | ||
| } | ||
|
|
||
| public record SmsVerifyRequest( | ||
| @NotBlank(message = "전화번호는 필수입니다.") String phoneNumber, | ||
| @NotBlank(message = "인증코드는 필수입니다.") String verificationCode) { | ||
| } | ||
|
Comment on lines
+7
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 검증 요청에도 전화번호/인증코드 형식 검증을 맞춰주세요 발송 요청은 10~11자리만 허용하지만 검증 요청은 제약이 없어 ✅ 수정 제안 public record SmsVerifyRequest(
- `@NotBlank`(message = "전화번호는 필수입니다.") String phoneNumber,
- `@NotBlank`(message = "인증코드는 필수입니다.") String verificationCode) {
+ `@NotBlank`(message = "전화번호는 필수입니다.")
+ `@Pattern`(regexp = "^\\d{10,11}$", message = "전화번호는 하이픈 없이 10~11자리 숫자만 입력해주세요.") String phoneNumber,
+ `@NotBlank`(message = "인증코드는 필수입니다.")
+ `@Pattern`(regexp = "^\\d{6}$", message = "인증코드는 6자리 숫자만 입력해주세요.") String verificationCode) {
}🤖 Prompt for AI Agents |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.application.dto.response; | ||
|
|
||
| public class SmsResponse { | ||
|
|
||
| public record SmsSentResponse( | ||
| String message, | ||
| String phoneNumber, // 전송한 휴대폰 번호 | ||
| long expireIn) { | ||
| } | ||
|
|
||
| public record SmsVerifiedResponse( | ||
| boolean isVerified, | ||
| String verificationMessage, | ||
| String email) { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,93 @@ | ||||||||||||||||||||||||||||||||||||
| package com.whereyouad.WhereYouAd.domains.user.domain.service; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import com.whereyouad.WhereYouAd.domains.user.exception.code.UserErrorCode; | ||||||||||||||||||||||||||||||||||||
| import com.whereyouad.WhereYouAd.domains.user.exception.handler.UserHandler; | ||||||||||||||||||||||||||||||||||||
| import com.whereyouad.WhereYouAd.domains.user.persistence.entity.User; | ||||||||||||||||||||||||||||||||||||
| import com.whereyouad.WhereYouAd.domains.user.persistence.repository.UserRepository; | ||||||||||||||||||||||||||||||||||||
| import net.nurigo.sdk.NurigoApp; | ||||||||||||||||||||||||||||||||||||
| import net.nurigo.sdk.message.model.Message; | ||||||||||||||||||||||||||||||||||||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.response.SmsResponse; | ||||||||||||||||||||||||||||||||||||
| import com.whereyouad.WhereYouAd.global.utils.RedisUtil; | ||||||||||||||||||||||||||||||||||||
| import net.nurigo.sdk.message.request.SingleMessageSendingRequest; | ||||||||||||||||||||||||||||||||||||
| import net.nurigo.sdk.message.service.DefaultMessageService; | ||||||||||||||||||||||||||||||||||||
| import org.springframework.beans.factory.annotation.Value; | ||||||||||||||||||||||||||||||||||||
| import org.springframework.stereotype.Service; | ||||||||||||||||||||||||||||||||||||
| import org.springframework.transaction.annotation.Transactional; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| import java.util.Random; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| @Service | ||||||||||||||||||||||||||||||||||||
| @Transactional | ||||||||||||||||||||||||||||||||||||
| public class SmsService { | ||||||||||||||||||||||||||||||||||||
| private final RedisUtil redisUtil; | ||||||||||||||||||||||||||||||||||||
| private final UserRepository userRepository; | ||||||||||||||||||||||||||||||||||||
| private final DefaultMessageService defaultMessageService; | ||||||||||||||||||||||||||||||||||||
| private final String senderNumber; | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| public SmsService(RedisUtil redisUtil, UserRepository userRepository, | ||||||||||||||||||||||||||||||||||||
| @Value("${coolSms.apiKey}") String smsApiKey, | ||||||||||||||||||||||||||||||||||||
| @Value("${coolSms.secretKey}") String smsSecretKey, | ||||||||||||||||||||||||||||||||||||
| @Value("${coolSms.senderNumber}") String senderNumber) { | ||||||||||||||||||||||||||||||||||||
| this.redisUtil = redisUtil; | ||||||||||||||||||||||||||||||||||||
| this.userRepository = userRepository; | ||||||||||||||||||||||||||||||||||||
| this.senderNumber = senderNumber; | ||||||||||||||||||||||||||||||||||||
| this.defaultMessageService = NurigoApp.INSTANCE.initialize(smsApiKey, smsSecretKey, | ||||||||||||||||||||||||||||||||||||
| "https://api.coolsms.co.kr"); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // SMS 전송 | ||||||||||||||||||||||||||||||||||||
| public SmsResponse.SmsSentResponse sendSms(String phoneNumber) { | ||||||||||||||||||||||||||||||||||||
| // 이메일 찾기 기능: 가입된 유저인지 확인 | ||||||||||||||||||||||||||||||||||||
| if (!userRepository.existsByPhoneNumber(phoneNumber)) { | ||||||||||||||||||||||||||||||||||||
| throw new UserHandler(UserErrorCode.USER_NOT_FOUND_BY_PHONE); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| Message message = new Message(); | ||||||||||||||||||||||||||||||||||||
| String verificationCode = generateCode(); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| message.setFrom(senderNumber); // 발신 번호 | ||||||||||||||||||||||||||||||||||||
| message.setTo(phoneNumber); | ||||||||||||||||||||||||||||||||||||
| message.setText("[Where You Ad] 이메일 찾기\n 휴대폰 인증 번호는 [" + verificationCode + "] 입니다."); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // redis 저장 | ||||||||||||||||||||||||||||||||||||
| redisUtil.setDataExpire(phoneNumber, verificationCode, 180); // 유효 기간 3분 | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||
| this.defaultMessageService.sendOne(new SingleMessageSendingRequest(message)); | ||||||||||||||||||||||||||||||||||||
| } catch (Exception e) { | ||||||||||||||||||||||||||||||||||||
| e.printStackTrace(); | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
| throw new UserHandler(UserErrorCode.SMS_SEND_FAILED); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
Comment on lines
+54
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SMS 전송 실패 시 Redis 데이터가 남아있는 문제가 있습니다. 현재 코드는 Redis에 인증 코드를 저장한 후 SMS를 전송합니다. 만약 SMS 전송이 실패하면 Redis에 저장된 데이터는 그대로 남아있게 되어, 사용자가 받지 못한 인증 코드가 3분간 유효한 상태로 남습니다. 예를 들어:
🛡️ 수정 제안: SMS 전송 성공 후 Redis 저장 public SmsResponse.SmsSentResponse sendSms(String phoneNumber) {
Message message = new Message();
String verificationCode = generateCode();
message.setFrom(senderNumber); // 발신 번호
message.setTo(phoneNumber);
message.setText("[Where You Ad] 이메일 찾기\n 휴대폰 인증 번호는 [" + verificationCode + "] 입니다.");
- // redis 저장
- redisUtil.setDataExpire(phoneNumber, verificationCode, 180); // 유효 기간 3분
-
try {
this.defaultMessageService.sendOne(new SingleMessageSendingRequest(message));
+ // SMS 전송 성공 후 redis 저장
+ redisUtil.setDataExpire(phoneNumber, verificationCode, 180); // 유효 기간 3분
} catch (Exception e) {
- e.printStackTrace();
+ log.error("SMS 전송 실패 - phoneNumber: {}", phoneNumber, e);
throw new UserHandler(UserErrorCode.SMS_SEND_FAILED);
}
return new SmsResponse.SmsSentResponse("인증번호가 전송되었습니다.", phoneNumber, 180L);
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| return new SmsResponse.SmsSentResponse("인증번호가 전송되었습니다.", phoneNumber, 180L); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // 랜덤 인증 번호 생성 | ||||||||||||||||||||||||||||||||||||
| public String generateCode() { | ||||||||||||||||||||||||||||||||||||
| Random random = new Random(); | ||||||||||||||||||||||||||||||||||||
| int code = 100000 + random.nextInt(900000); // 6자리 인증 번호 | ||||||||||||||||||||||||||||||||||||
| return Integer.toString(code); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // 인증번호 확인 | ||||||||||||||||||||||||||||||||||||
| public boolean verifyCode(String phoneNumber, String insertedNum) { | ||||||||||||||||||||||||||||||||||||
| String savedCode = redisUtil.getData(phoneNumber); | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| if (savedCode != null && savedCode.equals(insertedNum)) { | ||||||||||||||||||||||||||||||||||||
| redisUtil.deleteData(phoneNumber); | ||||||||||||||||||||||||||||||||||||
| // 인증 완료 | ||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||
| // 인증번호 확인 후 유저 이메일 반환 | ||||||||||||||||||||||||||||||||||||
| public String isPhoneVerified(String phoneNumber, String insertedNum) { | ||||||||||||||||||||||||||||||||||||
| if (verifyCode(phoneNumber, insertedNum)) { | ||||||||||||||||||||||||||||||||||||
| User user = userRepository.findUserByPhoneNumber(phoneNumber) | ||||||||||||||||||||||||||||||||||||
| .orElseThrow(() -> new UserHandler(UserErrorCode.USER_NOT_FOUND_BY_PHONE)); | ||||||||||||||||||||||||||||||||||||
| return user.getEmail(); | ||||||||||||||||||||||||||||||||||||
| } else | ||||||||||||||||||||||||||||||||||||
| throw new UserHandler(UserErrorCode.USER_SMS_NOT_VERIFIED); | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.exception.handler; | ||
|
|
||
| import com.whereyouad.WhereYouAd.global.exception.AppException; | ||
| import com.whereyouad.WhereYouAd.global.exception.BaseErrorCode; | ||
|
|
||
| public class UserHandler extends AppException { | ||
| public UserHandler(BaseErrorCode errorCode) { | ||
| super(errorCode); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| package com.whereyouad.WhereYouAd.domains.user.presentation.docs; | ||
|
|
||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.request.EmailRequest; | ||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.request.SmsRequest; | ||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.request.SignUpRequest; | ||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.response.EmailSentResponse; | ||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.response.SmsResponse; | ||
| import com.whereyouad.WhereYouAd.domains.user.application.dto.response.SignUpResponse; | ||
| import com.whereyouad.WhereYouAd.global.response.DataResponse; | ||
| import io.swagger.v3.oas.annotations.Operation; | ||
|
|
@@ -13,34 +15,41 @@ | |
| import org.springframework.web.bind.annotation.RequestBody; | ||
|
|
||
| public interface UserControllerDocs { | ||
| @Operation( | ||
| summary = "단순 회원가입 API", | ||
| description = "이메일, 비밀번호, 이름, 전화번호를 받아 회원가입을 진행합니다(먼저 이메일 인증이 진행되어야 회원가입 가능)" | ||
| ) | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "성공"), | ||
| @ApiResponse(responseCode = "400_2", description = "이메일 중복 회원 존재") | ||
| }) | ||
| public ResponseEntity<DataResponse<SignUpResponse>> signUp(@RequestBody @Valid SignUpRequest request); | ||
| @Operation(summary = "단순 회원가입 API", description = "이메일, 비밀번호, 이름, 전화번호를 받아 회원가입을 진행합니다(먼저 이메일 인증이 진행되어야 회원가입 가능)") | ||
|
||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "성공"), | ||
| @ApiResponse(responseCode = "400_2", description = "이메일 중복 회원 존재") | ||
| }) | ||
| public ResponseEntity<DataResponse<SignUpResponse>> signUp(@RequestBody @Valid SignUpRequest request); | ||
|
|
||
| @Operation( | ||
| summary = "이메일 인증코드 전송 API", | ||
| description = "입력받은 이메일로 인증코드를 전송합니다. 인증코드 재전송도 해당 API 를 호출합니다.\n\n" + | ||
| "테스트용 이메일은 'test' 로 시작하거나 'example.com' 으로 끝나야합니다. 테스트용 이메일의 인증코드는 서버 로그로 확인 가능합니다." | ||
| ) | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "성공"), | ||
| @ApiResponse(responseCode = "400_3", description = "이메일 전송실패(이메일 오타 등)") | ||
| }) | ||
| public ResponseEntity<DataResponse<EmailSentResponse>> sendEmail(@RequestBody @Valid EmailRequest.Send request); | ||
| @Operation(summary = "이메일 인증코드 전송 API", description = "입력받은 이메일로 인증코드를 전송합니다. 인증코드 재전송도 해당 API 를 호출합니다.\n\n" + | ||
| "테스트용 이메일은 'test' 로 시작하거나 'example.com' 으로 끝나야합니다. 테스트용 이메일의 인증코드는 서버 로그로 확인 가능합니다.") | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "성공"), | ||
| @ApiResponse(responseCode = "400_3", description = "이메일 전송실패(이메일 오타 등)") | ||
| }) | ||
| public ResponseEntity<DataResponse<EmailSentResponse>> sendEmail(@RequestBody @Valid EmailRequest.Send request); | ||
|
|
||
| @Operation( | ||
| summary = "이메일 인증코드 인증 API", | ||
| description = "이메일과 인증코드를 받아 인증코드가 맞는지 검증합니다." | ||
| ) | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "성공"), | ||
| @ApiResponse(responseCode = "400_4", description = "실패(인증코드 불일치)") | ||
| }) | ||
| public ResponseEntity<DataResponse<String>> verifyEmail(@RequestBody @Valid EmailRequest.Verify request); | ||
| @Operation(summary = "이메일 인증코드 인증 API", description = "이메일과 인증코드를 받아 인증코드가 맞는지 검증합니다.") | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "성공"), | ||
| @ApiResponse(responseCode = "400_4", description = "실패(인증코드 불일치)") | ||
| }) | ||
| public ResponseEntity<DataResponse<String>> verifyEmail(@RequestBody @Valid EmailRequest.Verify request); | ||
|
|
||
| @Operation(summary = "이메일 찾기 - SMS 인증 번호 전송 API", description = "입력받은 전화번호로 인증 번호를 전송합니다.") | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "성공"), | ||
| @ApiResponse(responseCode = "400", description = "실패") | ||
| }) | ||
| public ResponseEntity<DataResponse<SmsResponse.SmsSentResponse>> sendSms( | ||
| @RequestBody @Valid SmsRequest.SmsSendRequest request); | ||
|
|
||
| @Operation(summary = "이메일 찾기 - SMS 인증 번호 확인 API", description = "전화번호와 인증 코드를 받아 맞는지 검증합니다.") | ||
| @ApiResponses({ | ||
| @ApiResponse(responseCode = "200", description = "성공"), | ||
| @ApiResponse(responseCode = "400", description = "실패") | ||
| }) | ||
| public ResponseEntity<DataResponse<SmsResponse.SmsVerifiedResponse>> verifySms( | ||
| @RequestBody @Valid SmsRequest.SmsVerifyRequest request); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.