-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 회원가입시 사용자 번호로 문자 전송 기능 - 인증번호 입력시 검증 후 가입 - 전화번호 중복여부 검사 - 인증번호 만료 시간 설정
- Loading branch information
Showing
12 changed files
with
213 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/com/sscanner/team/user/SmsCertificationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.sscanner.team.user; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import net.nurigo.sdk.NurigoApp; | ||
import net.nurigo.sdk.message.model.Message; | ||
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.Component; | ||
|
||
@Component | ||
public class SmsCertificationUtil { | ||
@Value("${coolsms.api.key}") | ||
private String apiKey; | ||
|
||
@Value("${coolsms.api.secret}") | ||
private String apiSecret; | ||
|
||
@Value("${coolsms.api.number}") | ||
private String fromNumber; | ||
|
||
DefaultMessageService messageService; | ||
|
||
@PostConstruct // 의존성 주입이 완료된 후 초기화 수행 | ||
public void init(){ | ||
this.messageService = NurigoApp.INSTANCE.initialize(apiKey, apiSecret, "https://api.coolsms.co.kr"); | ||
} | ||
|
||
// 단일 메시지 발송 | ||
public void sendSMS(String to, String certificationCode){ | ||
Message message = new Message(); | ||
message.setFrom(fromNumber); | ||
message.setTo(to); | ||
message.setText("본인확인 인증번호는 " + certificationCode + "입니다."); | ||
|
||
this.messageService.sendOne(new SingleMessageSendingRequest(message)); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/sscanner/team/user/controller/SmsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.sscanner.team.user.controller; | ||
|
||
import com.sscanner.team.global.common.response.ApiResponse; | ||
import com.sscanner.team.global.exception.BadRequestException; | ||
import com.sscanner.team.global.exception.ExceptionCode; | ||
import com.sscanner.team.user.requestDto.SmsRequestDto; | ||
import com.sscanner.team.user.requestDto.SmsVerifyRequestDto; | ||
import com.sscanner.team.user.service.SmsService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/sms") | ||
public class SmsController { | ||
|
||
private final SmsService smsService; | ||
|
||
@PostMapping("/send") | ||
public ApiResponse<?> SendSMS(@RequestBody @Valid SmsRequestDto smsRequestDto){ | ||
smsService.SendSms(smsRequestDto); | ||
return new ApiResponse<>(200,"문자를 전송했습니다",null); | ||
} | ||
|
||
@PostMapping("/verify") | ||
public ApiResponse<?> verifyCode(@RequestBody @Valid SmsVerifyRequestDto req) { | ||
boolean verify = smsService.verifyCode(req); | ||
if (verify) { | ||
return new ApiResponse<>(200,"인증이 완료되었습니다.",null); | ||
} else { | ||
throw new BadRequestException(ExceptionCode.UNAUTHORIZED); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
39 changes: 39 additions & 0 deletions
39
src/main/java/com/sscanner/team/user/repository/SmsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.sscanner.team.user.repository; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.StringRedisTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.time.Duration; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class SmsRepository { | ||
|
||
private final String PREFIX = "sms:"; // 키 | ||
|
||
private final StringRedisTemplate stringRedisTemplate; | ||
|
||
// 인증 정보 저장 | ||
public void createSmsCertification(String phone, String code) { | ||
int LIMIT_TIME = 60 * 120; // 유효시간 (2분) | ||
stringRedisTemplate.opsForValue() | ||
.set(PREFIX + phone, code, Duration.ofSeconds(LIMIT_TIME)); | ||
} | ||
|
||
// 인증 정보 조회 | ||
public String getSmsCertification(String phone) { | ||
return stringRedisTemplate.opsForValue().get(PREFIX + phone); | ||
} | ||
|
||
//인증 정보 삭제 | ||
public void deleteSmsCertification(String phone) { | ||
stringRedisTemplate.delete(PREFIX + phone); | ||
} | ||
|
||
// 인증 정보 Redis에 존재 확인 | ||
public boolean hasKey(String phone) { | ||
return Boolean.TRUE.equals(stringRedisTemplate.hasKey(PREFIX + phone)); // Redis에서 해당 키의 존재 여부 확인 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/sscanner/team/user/requestDto/SmsRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.sscanner.team.user.requestDto; | ||
|
||
import jakarta.validation.constraints.NotEmpty; | ||
|
||
public record SmsRequestDto( | ||
@NotEmpty(message = "휴대폰 번호를 입력해주세요") | ||
String phoneNum | ||
) { | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/sscanner/team/user/requestDto/SmsVerifyRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.sscanner.team.user.requestDto; | ||
|
||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record SmsVerifyRequestDto ( | ||
|
||
@NotNull(message = "휴대폰 번호를 입력해주세요.") | ||
String phoneNum, | ||
@NotNull(message = "인증번호를 입력해주세요.") | ||
String code | ||
){ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/sscanner/team/user/service/SmsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.sscanner.team.user.service; | ||
|
||
import com.sscanner.team.global.exception.BadRequestException; | ||
import com.sscanner.team.global.exception.ExceptionCode; | ||
import com.sscanner.team.user.SmsCertificationUtil; | ||
import com.sscanner.team.user.repository.SmsRepository; | ||
import com.sscanner.team.user.repository.UserRepository; | ||
import com.sscanner.team.user.requestDto.SmsRequestDto; | ||
import com.sscanner.team.user.requestDto.SmsVerifyRequestDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class SmsService { | ||
|
||
private final SmsCertificationUtil smsCertificationUtil; | ||
private final SmsRepository smsRepository; | ||
private final UserRepository userRepository; | ||
|
||
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); | ||
} | ||
|
||
public boolean verifyCode(SmsVerifyRequestDto smsVerifyDto) { | ||
if (isVerify(smsVerifyDto.phoneNum(), smsVerifyDto.code())) { | ||
smsRepository.deleteSmsCertification(smsVerifyDto.phoneNum()); | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
public boolean isVerify(String phoneNum, String code) { | ||
return smsRepository.hasKey(phoneNum) && // 전화번호에 대한 키가 존재하고 | ||
smsRepository.getSmsCertification(phoneNum).equals(code); // 저장된 인증 코드와 입력된 인증 코드가 일치하는지 확인 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters