-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…ation SMS 본인인증 기능 구현 - 본인 인증 코드 문자 발송 API 구현
- Loading branch information
Showing
27 changed files
with
709 additions
and
4 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
21 changes: 21 additions & 0 deletions
21
src/main/java/com/ajou/hertz/common/auth/dto/request/SendUserAuthCodeRequest.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,21 @@ | ||
package com.ajou.hertz.common.auth.dto.request; | ||
|
||
import com.ajou.hertz.common.validator.PhoneNumber; | ||
|
||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor(access = AccessLevel.PRIVATE) | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
public class SendUserAuthCodeRequest { | ||
|
||
@Schema(description = "본인 인증 코드를 발송할 전화번호", example = "01012345678") | ||
@NotBlank | ||
@PhoneNumber | ||
private String phoneNumber; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/com/ajou/hertz/common/auth/entity/UserAuthCode.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,22 @@ | ||
package com.ajou.hertz.common.auth.entity; | ||
|
||
import java.io.Serializable; | ||
import java.time.LocalDateTime; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
public class UserAuthCode implements Serializable { | ||
|
||
// Redis 뿐만 아니라, user auth code 자체의 유효 기간을 의미. | ||
public static final int TTL_SECONDS = 300; | ||
|
||
private String code; | ||
private String phoneNumber; | ||
private LocalDateTime createdAt; | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/ajou/hertz/common/auth/repository/UserAuthCodeRedisRepository.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,35 @@ | ||
package com.ajou.hertz.common.auth.repository; | ||
|
||
import java.util.Optional; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import com.ajou.hertz.common.auth.entity.UserAuthCode; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Repository | ||
public class UserAuthCodeRedisRepository implements UserAuthCodeRepository { | ||
|
||
private final RedisTemplate<String, UserAuthCode> redisTemplate; | ||
|
||
@Override | ||
public UserAuthCode save(UserAuthCode userAuthCode) { | ||
redisTemplate.opsForValue().set( | ||
userAuthCode.getCode(), | ||
userAuthCode, | ||
UserAuthCode.TTL_SECONDS, | ||
TimeUnit.SECONDS | ||
); | ||
return userAuthCode; | ||
} | ||
|
||
@Override | ||
public Optional<UserAuthCode> findByCode(String code) { | ||
UserAuthCode userAuthCode = redisTemplate.opsForValue().get(code); | ||
return Optional.ofNullable(userAuthCode); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/com/ajou/hertz/common/auth/repository/UserAuthCodeRepository.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.ajou.hertz.common.auth.repository; | ||
|
||
import java.util.Optional; | ||
|
||
import com.ajou.hertz.common.auth.entity.UserAuthCode; | ||
|
||
public interface UserAuthCodeRepository { | ||
|
||
UserAuthCode save(UserAuthCode userAuthCode); | ||
|
||
Optional<UserAuthCode> findByCode(String code); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/ajou/hertz/common/auth/service/UserAuthCodeService.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,37 @@ | ||
package com.ajou.hertz.common.auth.service; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
import org.springframework.lang.NonNull; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.ajou.hertz.common.auth.entity.UserAuthCode; | ||
import com.ajou.hertz.common.auth.repository.UserAuthCodeRepository; | ||
import com.ajou.hertz.common.message.service.MessageService; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class UserAuthCodeService { | ||
|
||
private static final int USER_AUTH_CODE_LEN = 8; | ||
|
||
private final MessageService messageService; | ||
private final UserAuthCodeRepository userAuthCodeRepository; | ||
|
||
public void sendUserAuthCodeViaSms(@NonNull String targetPhoneNumber) { | ||
String authCode = generateRandomAuthCode(); | ||
UserAuthCode userAuthCode = new UserAuthCode(authCode, targetPhoneNumber, LocalDateTime.now()); | ||
userAuthCodeRepository.save(userAuthCode); | ||
messageService.sendShortMessage( | ||
String.format("[헤르츠] 본인 확인을 위해 인증코드 %s 를 입력해주세요.", authCode), | ||
targetPhoneNumber | ||
); | ||
} | ||
|
||
private String generateRandomAuthCode() { | ||
return UUID.randomUUID().toString().substring(0, USER_AUTH_CODE_LEN); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/ajou/hertz/common/config/ObjectMapperConfig.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,20 @@ | ||
package com.ajou.hertz.common.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | ||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | ||
|
||
@Configuration | ||
public class ObjectMapperConfig { | ||
|
||
@Bean | ||
public ObjectMapper objectMapper() { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JavaTimeModule javaTimeModule = new JavaTimeModule(); | ||
objectMapper.registerModules(javaTimeModule, new Jdk8Module()); | ||
return objectMapper; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/ajou/hertz/common/config/PathVariableEncodingExclusionInterceptor.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,19 @@ | ||
package com.ajou.hertz.common.config; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
import feign.RequestInterceptor; | ||
import feign.RequestTemplate; | ||
|
||
@Component | ||
public class PathVariableEncodingExclusionInterceptor implements RequestInterceptor { | ||
private static final String COLON = "%3A"; | ||
|
||
@Override | ||
public void apply(final RequestTemplate template) { | ||
final String path = template.path(); | ||
if (path.contains(COLON)) { | ||
template.uri(path.replaceAll(COLON, ":")); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/ajou/hertz/common/config/RedisTemplateConfig.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,27 @@ | ||
package com.ajou.hertz.common.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
import com.ajou.hertz.common.auth.entity.UserAuthCode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@Configuration | ||
public class RedisTemplateConfig { | ||
|
||
@Bean | ||
public RedisTemplate<String, UserAuthCode> redisTemplate( | ||
RedisConnectionFactory connectionFactory, | ||
ObjectMapper objectMapper | ||
) { | ||
RedisTemplate<String, UserAuthCode> redisTemplate = new RedisTemplate<>(); | ||
redisTemplate.setConnectionFactory(connectionFactory); | ||
redisTemplate.setKeySerializer(new StringRedisSerializer()); | ||
redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(objectMapper, UserAuthCode.class)); | ||
return redisTemplate; | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/ajou/hertz/common/message/constant/MessageType.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,7 @@ | ||
package com.ajou.hertz.common.message.constant; | ||
|
||
public enum MessageType { | ||
SMS, | ||
LMS, | ||
MMS | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/ajou/hertz/common/message/exception/SendMessageException.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,14 @@ | ||
package com.ajou.hertz.common.message.exception; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.lang.NonNull; | ||
|
||
import com.ajou.hertz.common.exception.CustomException; | ||
import com.ajou.hertz.common.exception.constant.CustomExceptionType; | ||
|
||
public class SendMessageException extends CustomException { | ||
|
||
public SendMessageException(@NonNull HttpStatus httpStatus, String optionalMessage) { | ||
super(httpStatus, CustomExceptionType.SEND_MESSAGE, optionalMessage); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/com/ajou/hertz/common/message/service/MessageService.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,6 @@ | ||
package com.ajou.hertz.common.message.service; | ||
|
||
public interface MessageService { | ||
|
||
void sendShortMessage(String message, String targetPhoneNumber); | ||
} |
Oops, something went wrong.