feat(owner-apigateway-service): with credentials 및 exception handler 추가

- with credentials : true 옵션 추가
- 해당 옵션 추가로 인한
access-control-allow-origin : * 에서 vue 서버 아이피로 변경
- access token expired
일 경우 response body 추가
This commit is contained in:
bum12ark
2022-02-28 16:50:22 +09:00
parent 516c192211
commit b6b3e39380
4 changed files with 63 additions and 3 deletions

View File

@@ -1,8 +1,11 @@
package com.justpickup.ownerapigatewayservice;
import com.justpickup.ownerapigatewayservice.handler.GlobalExceptionHandler;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
@EnableEurekaClient
@@ -12,4 +15,9 @@ public class OwnerApigatewayServiceApplication {
SpringApplication.run(OwnerApigatewayServiceApplication.class, args);
}
@Bean
public ErrorWebExceptionHandler globalExceptionHandler() {
return new GlobalExceptionHandler();
}
}

View File

@@ -0,0 +1,46 @@
package com.justpickup.ownerapigatewayservice.handler;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.ExpiredJwtException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.util.HashMap;
import java.util.Map;
public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
@Autowired
private ObjectMapper objectMapper;
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
Class<? extends Throwable> exceptionClass = ex.getClass();
Map<String, Object> responseBody = new HashMap<>();
if (exceptionClass == ExpiredJwtException.class) {
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
exchange.getResponse().getHeaders().setContentType(MediaType.APPLICATION_JSON);
responseBody.put("code", "EXPIRED");
responseBody.put("message", "Access Token is Expired!");
}
DataBuffer wrap = null;
try {
byte[] bytes = objectMapper.writeValueAsBytes(responseBody);
wrap = exchange.getResponse().bufferFactory().wrap(bytes);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return exchange.getResponse().writeWith(Flux.just(wrap));
}
}

View File

@@ -85,7 +85,7 @@ public class JwtTokenProvider {
return false;
} catch (ExpiredJwtException e) {
log.error("JWT token is expired: {}", e.getMessage());
return false;
throw e;
} catch (UnsupportedJwtException e) {
log.error("JWT token is unsupported: {}", e.getMessage());
return false;

View File

@@ -17,7 +17,7 @@ spring:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins: "*"
allowedOrigins: "http://localhost:8080"
allowedMethods:
- GET
- POST
@@ -25,6 +25,7 @@ spring:
- PUT
- OPTIONS
allowedHeaders: '*'
allow-credentials: true
routes:
- id: owner-frontend-service
uri: lb://OWNER-FRONTEND-SERVICE
@@ -32,18 +33,22 @@ spring:
- Path=/owner-frontend-service/**
filters:
- RewritePath=/owner-frontend-service/(?<segment>.*),/$\{segment}
- id: order-service
uri: lb://ORDER-SERVCIE
predicates:
- Path=/order-service/**
filters:
- RewritePath=/order-service/(?<segment>.*),/$\{segment}
- AuthorizationHeaderFilter
- id: store-service
uri: lb://STORE-SERVCIE
predicates:
- Path=/store-service/**
filters:
- RewritePath=/store-service/(?<segment>.*),/$\{segment}
- id: user-service
uri: lb://USER-SERVICE
predicates:
@@ -54,7 +59,7 @@ spring:
- id: user-service
uri: lb://USER-SERVICE
predicates:
- Path=/user-service/refreshToken
- Path=/user-service/auth/reissue
- Method=GET
filters:
- RewritePath=/user-service/(?<segment>.*),/$\{segment}
@@ -78,6 +83,7 @@ spring:
- Path=/user-service/**
filters:
- RewritePath=/user-service/(?<segment>.*),/$\{segment}
- AuthorizationHeaderFilter
token:
access-expired-time: 3600000