Skip to content

Commit

Permalink
fix: #167-Redis가 Lua 스크립트를 읽지 못하는 현상 해결
Browse files Browse the repository at this point in the history
  • Loading branch information
mclub4 committed May 7, 2024
1 parent c82f620 commit ad1f0a4
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
3 changes: 1 addition & 2 deletions back-gateway/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ ext {
}

dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand All @@ -36,7 +35,7 @@ dependencies {
implementation 'io.jsonwebtoken:jjwt-jackson:0.11.5'

implementation 'org.springframework.boot:spring-boot-starter-data-redis-reactive'
implementation 'io.lettuce.core:lettuce-core:6.3.2'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import com.gateway.backgateway.filter.AuthorizationHeaderFilter;
import com.gateway.backgateway.filter.RequestRateLimitFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Configuration
public class GatewayConfig {
Expand Down Expand Up @@ -49,18 +45,17 @@ public RouteLocator gatewayRoutes(RouteLocatorBuilder builder,
.route("spring", r -> r.path("/api/**")
.filters(f->f
.filter(authFilter.apply(config -> {config.setRequiredRole("role_user");}))
.filter(limitFilter.apply(config -> {
config.setRateLimiter(redisRateLimiter());
config.setRouteId("22");
}))
.requestRateLimiter(c -> {
c.setRateLimiter(redisRateLimiter());
})
)
.uri("http://localhost:8080"))
.uri("http://spring:8080"))
.build();
}

@Bean
public RedisRateLimiter redisRateLimiter() {
return new RedisRateLimiter(0, 1, 1); // 기본 replenishRate 및 burstCapacity 값을 지정합니다
return new RedisRateLimiter(1, 1, 1); // 기본 replenishRate 및 burstCapacity 값을 지정합니다
}

public UserIdKeyResolver userKeyResolver() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.gateway.backgateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {

@Bean
@Primary
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() {
return new LettuceConnectionFactory("redis", 6379);
}

@Bean
@Primary
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) {
RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext
.<String, Object>newSerializationContext(new StringRedisSerializer())
.hashKey(new StringRedisSerializer())
.hashValue(new StringRedisSerializer())
.build();

return new ReactiveRedisTemplate<>(factory, serializationContext);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package com.gateway.backgateway.exception;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gateway.backgateway.dto.ErrorResponse;
import io.jsonwebtoken.JwtException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.annotation.Order;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -40,15 +37,15 @@ public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
response.getHeaders().setContentType(MediaType.APPLICATION_JSON);
if (ex instanceof JwtTokenInvalidException) {
errorCode = INVALID_JWT_TOKEN;
response.setStatusCode(HttpStatusCode.valueOf(errorCode.getStatus()));
response.setStatusCode(HttpStatus.valueOf(errorCode.getStatus()));
}
else if (ex instanceof TooManyRequestException) {
errorCode = TOO_MANY_REQUESTS;
response.setStatusCode(HttpStatusCode.valueOf(errorCode.getStatus()));
response.setStatusCode(HttpStatus.valueOf(errorCode.getStatus()));
}
else{
errorCode = INTERNAL_SERVER_ERROR;
response.setStatusCode(HttpStatusCode.valueOf(errorCode.getStatus()));
response.setStatusCode(HttpStatus.valueOf(errorCode.getStatus()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public GatewayFilter apply(Config config) {
String token = request.getHeaders()
.getFirst(HttpHeaders.AUTHORIZATION).replace("Bearer ", "");

System.out.println(token);

if (!validateToken(token)) {
throw JwtTokenInvalidException.INSTANCE;
}
Expand Down
2 changes: 1 addition & 1 deletion back-gateway/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jwt:
key: ${JWT_SECRET}

server:
port: 8082
port: 8081
chatbot-url: ${CHATBOT_URL}

logging:
Expand Down

0 comments on commit ad1f0a4

Please sign in to comment.