This commit is contained in:
2022-03-07 17:41:24 +09:00
parent 6c5815afc7
commit f34c8ef2da
5 changed files with 76 additions and 19 deletions

View File

@@ -1,6 +1,5 @@
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
}

View File

@@ -1,16 +0,0 @@
package bit.space.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowedMethods("OPTIONS", "GET", "POST", "PUT", "DELETE");
}
}

View File

@@ -0,0 +1,50 @@
package bit.space.config.filter;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import reactor.core.publisher.Mono;
@Slf4j
@Component
public class GlobalFilter extends AbstractGatewayFilterFactory<GlobalFilter.Config> {
public GlobalFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return ((exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
log.info("GlobalFilter Message : {}", config.getBaseMessage());
if(config.isPreLogger()) {
log.info("GlobalFilter start : 요청 id -> {}", request.getId());
}
return chain.filter(exchange).then(Mono.fromRunnable(()-> {
if(config.isPostLogger()) {
log.info("GlobalFilter end : 응답 code -> {}", response.getStatusCode());
}
}));
});
}
@Data
public static class Config {
private String baseMessage;
private boolean preLogger;
private boolean postLogger;
}
}

View File

@@ -13,6 +13,30 @@ spring:
name: gateway-server
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOriginPatterns: "*"
allow-credentials: true
allowedHeaders:
- x-requested-with
- authorization
- content-type
- credential
- X-AUTH-TOKEN
- X-CSRF-TOKEN
allowedMethods:
- POST
- GET
- PUT
- OPTIONS
- DELETE
default-filters:
- name: GlobalFilter
args:
baseMessage: GLOBAL FILTER
preLogger: true
postLogger: true
routes:
- id: member-service
uri: lb://MEMBER-SERVICE

View File

@@ -8,14 +8,14 @@ const App: React.FC = () => {
useEffect(() => {
mainAxios.get("/member-service/welcome")
.then(res => console.log(res))
.then(res => setMessage(res.data))
},[])
return (
<div>
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.tsx</code> and save to reload.
{message}
</p>
</div>
);