From ff46845bd5b535c98cf78cf2503874856799343f Mon Sep 17 00:00:00 2001 From: JaeUk Jeong Date: Thu, 28 Mar 2024 17:11:25 +0900 Subject: [PATCH] ewq --- build.gradle | 3 ++ .../common/config/ObjectMapperConfig.java | 20 +++++++++++++ .../common/config/RedisTemplateConfig.java | 27 ++++++++++++++++++ .../domain/user_auth_code/UserAuthCode.java | 26 +++++++++++++++++ .../repository/UserAuthCodeRepository.java | 8 ++++++ .../UserAuthCodeRepositoryImpl.java | 28 +++++++++++++++++++ src/main/resources/application.properties | 3 ++ 7 files changed, 115 insertions(+) create mode 100644 src/main/java/com/ajou/hertz/common/config/ObjectMapperConfig.java create mode 100644 src/main/java/com/ajou/hertz/common/config/RedisTemplateConfig.java create mode 100644 src/main/java/com/ajou/hertz/domain/user_auth_code/UserAuthCode.java create mode 100644 src/main/java/com/ajou/hertz/domain/user_auth_code/repository/UserAuthCodeRepository.java create mode 100644 src/main/java/com/ajou/hertz/domain/user_auth_code/repository/UserAuthCodeRepositoryImpl.java diff --git a/build.gradle b/build.gradle index 4a79f03..ce7a94c 100644 --- a/build.gradle +++ b/build.gradle @@ -77,6 +77,9 @@ dependencies { runtimeOnly 'com.h2database:h2' runtimeOnly 'com.mysql:mysql-connector-j' + // Redis + implementation 'org.springframework.boot:spring-boot-starter-data-redis' + // Lombok compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' diff --git a/src/main/java/com/ajou/hertz/common/config/ObjectMapperConfig.java b/src/main/java/com/ajou/hertz/common/config/ObjectMapperConfig.java new file mode 100644 index 0000000..e340ce6 --- /dev/null +++ b/src/main/java/com/ajou/hertz/common/config/ObjectMapperConfig.java @@ -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; + } +} diff --git a/src/main/java/com/ajou/hertz/common/config/RedisTemplateConfig.java b/src/main/java/com/ajou/hertz/common/config/RedisTemplateConfig.java new file mode 100644 index 0000000..dba9f03 --- /dev/null +++ b/src/main/java/com/ajou/hertz/common/config/RedisTemplateConfig.java @@ -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.domain.user_auth_code.UserAuthCode; +import com.fasterxml.jackson.databind.ObjectMapper; + +@Configuration +public class RedisTemplateConfig { + + @Bean + public RedisTemplate redisTemplate( + RedisConnectionFactory connectionFactory, + ObjectMapper objectMapper + ) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(connectionFactory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<>(objectMapper, UserAuthCode.class)); + return redisTemplate; + } +} diff --git a/src/main/java/com/ajou/hertz/domain/user_auth_code/UserAuthCode.java b/src/main/java/com/ajou/hertz/domain/user_auth_code/UserAuthCode.java new file mode 100644 index 0000000..48ba698 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/user_auth_code/UserAuthCode.java @@ -0,0 +1,26 @@ +package com.ajou.hertz.domain.user_auth_code; + +import java.io.Serializable; +import java.time.LocalDateTime; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +public class UserAuthCode implements Serializable { + + public static final int TTL_MINUTE = 5; + + private String code; + private Long userId; + + private LocalDateTime createdAt; + + public UserAuthCode(String code, Long userId) { + this(code, userId, LocalDateTime.now()); + } +} diff --git a/src/main/java/com/ajou/hertz/domain/user_auth_code/repository/UserAuthCodeRepository.java b/src/main/java/com/ajou/hertz/domain/user_auth_code/repository/UserAuthCodeRepository.java new file mode 100644 index 0000000..24335d4 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/user_auth_code/repository/UserAuthCodeRepository.java @@ -0,0 +1,8 @@ +package com.ajou.hertz.domain.user_auth_code.repository; + +import com.ajou.hertz.domain.user_auth_code.UserAuthCode; + +public interface UserAuthCodeRepository { + + UserAuthCode save(UserAuthCode userAuthCode); +} diff --git a/src/main/java/com/ajou/hertz/domain/user_auth_code/repository/UserAuthCodeRepositoryImpl.java b/src/main/java/com/ajou/hertz/domain/user_auth_code/repository/UserAuthCodeRepositoryImpl.java new file mode 100644 index 0000000..19e8439 --- /dev/null +++ b/src/main/java/com/ajou/hertz/domain/user_auth_code/repository/UserAuthCodeRepositoryImpl.java @@ -0,0 +1,28 @@ +package com.ajou.hertz.domain.user_auth_code.repository; + +import java.util.concurrent.TimeUnit; + +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +import com.ajou.hertz.domain.user_auth_code.UserAuthCode; + +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@Repository +public class UserAuthCodeRepositoryImpl implements UserAuthCodeRepository { + + private final RedisTemplate template; + + @Override + public UserAuthCode save(UserAuthCode userAuthCode) { + template.opsForValue().set( + userAuthCode.getCode(), + userAuthCode, + UserAuthCode.TTL_MINUTE, + TimeUnit.MINUTES + ); + return userAuthCode; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 142e73d..668671b 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -15,6 +15,9 @@ spring.datasource.username=${DB_USERNAME} spring.datasource.password=${DB_PASSWORD} spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.data.redis.host=${REDIS_HOST} +spring.data.redis.port=${REDIS_PORT} + spring.servlet.multipart.max-request-size=70MB spring.servlet.multipart.max-file-size=10MB