From c1511038ea67f6a7c00dec3a4add8b46fd6f88bc Mon Sep 17 00:00:00 2001 From: JIUNG GU <60885635+JIUNG9@users.noreply.github.com> Date: Tue, 19 Dec 2023 19:43:49 +0900 Subject: [PATCH] add: add the redis config --- .../apigateway/common/config/RedisConfig.java | 35 +++++++++++++++---- .../common/util/RedisRefreshTokenUtil.java | 18 ++++++---- src/main/resources/application-dev.yml | 7 +--- src/main/resources/application-local.yml | 5 ++- src/main/resources/application-prod.yml | 7 ---- 5 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/main/java/kr/bb/apigateway/common/config/RedisConfig.java b/src/main/java/kr/bb/apigateway/common/config/RedisConfig.java index b1ee518..f2b5527 100644 --- a/src/main/java/kr/bb/apigateway/common/config/RedisConfig.java +++ b/src/main/java/kr/bb/apigateway/common/config/RedisConfig.java @@ -1,24 +1,45 @@ package kr.bb.apigateway.common.config; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.connection.RedisConnectionFactory; -import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; +import org.springframework.data.redis.connection.RedisStandaloneConfiguration; +import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.GenericToStringSerializer; +import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { + @Value("${spring.redis.host}") + private String host; + @Value("${spring.redis.port}") + private int port; + @Value("${spring.redis.password}") + private String password; + + + @Bean - public LettuceConnectionFactory connectionFactory() { - return new LettuceConnectionFactory(); + JedisConnectionFactory jedisConnectionFactory() { + RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration( + host, port); + redisStandaloneConfiguration.setHostName(host); + redisStandaloneConfiguration.setPort(port); + redisStandaloneConfiguration.setPassword(password); + return new JedisConnectionFactory(redisStandaloneConfiguration); } @Bean - public RedisTemplate deliveryRedisTemplate( - RedisConnectionFactory connectionFactory) { + public RedisTemplate redisTemplate( + JedisConnectionFactory jedisConnectionFactory) { RedisTemplate template = new RedisTemplate<>(); - template.setConnectionFactory(connectionFactory); + template.setConnectionFactory(jedisConnectionFactory); + template.setKeySerializer(new StringRedisSerializer()); + template.setValueSerializer(new StringRedisSerializer()); + template.setHashKeySerializer(new GenericToStringSerializer<>(String.class)); + template.setHashValueSerializer(new GenericToStringSerializer<>(String.class)); return template; } diff --git a/src/main/java/kr/bb/apigateway/common/util/RedisRefreshTokenUtil.java b/src/main/java/kr/bb/apigateway/common/util/RedisRefreshTokenUtil.java index 238d5d8..f9e076a 100644 --- a/src/main/java/kr/bb/apigateway/common/util/RedisRefreshTokenUtil.java +++ b/src/main/java/kr/bb/apigateway/common/util/RedisRefreshTokenUtil.java @@ -1,22 +1,28 @@ package kr.bb.apigateway.common.util; import java.util.concurrent.TimeUnit; -import lombok.RequiredArgsConstructor; +import lombok.NoArgsConstructor; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; -@RequiredArgsConstructor +@NoArgsConstructor @Component -public class -RedisRefreshTokenUtil { +public class RedisRefreshTokenUtil { + + private RedisTemplate redisTemplate; + + public RedisRefreshTokenUtil( + RedisTemplate redisTemplate) { + this.redisTemplate = redisTemplate; + } - private final RedisTemplate redisTemplate; public void saveRefreshToken(String userId, String refreshToken, long expirationTimeInSeconds) { String key = getRefreshTokenKey(userId); - redisTemplate.opsForValue().set(key, refreshToken, expirationTimeInSeconds, TimeUnit.SECONDS); + redisTemplate.opsForValue() + .set(key, refreshToken, expirationTimeInSeconds, TimeUnit.SECONDS); } public String getRefreshToken(String userId) { diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 05b5d36..2455566 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -17,11 +17,6 @@ management: - "refresh" - "bus-refresh" -cookie: - refresh: - http: - domain: http://localhost:8000 - token: - name: refresh-cookie + diff --git a/src/main/resources/application-local.yml b/src/main/resources/application-local.yml index 33e8de0..a561d55 100644 --- a/src/main/resources/application-local.yml +++ b/src/main/resources/application-local.yml @@ -1,6 +1,7 @@ server: port: 8000 spring: + cloud: gateway: httpclient: @@ -10,8 +11,10 @@ spring: main: web-application-type: reactive redis: - host: localhost + host: redis port: 6379 + password: 123456 + application: name: apigateway-service config: diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 2d1e4fb..859a00b 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -16,10 +16,3 @@ management: include: - "refresh" - "bus-refresh" - -cookie: - refresh: - http: - domain: http://localhost:8000 - token: - name: refresh-cookie