MSA로 구성된 프로젝트에서 Swagger를 Gateway 통해서 요청할 수 있도록 Swagger 설정에서 Server 목록을 추가
server:
host: localhost
gateway:
port: 19091
@OpenAPIDefinition(
info = @Info(title = "Service API 명세서",
description = "Service API 명세서",
version = "v1"))
@Configuration
public class SwaggerConfig {
@Value("${server.host}")
String host;
@Value("${server.port}")
String serverPort;
@Value("${server.gateway.port}")
String gatewayPort;
@Bean
public GroupedOpenApi publicAPI(){
return GroupedOpenApi.builder()
.group("com.blue.service")
.pathsToMatch("/**")
.build();
}
@Bean
public OpenAPI customOpenAPI() {
List<Server> serverList = new ArrayList<>();
Server server = new Server()
.url("http://"+host+":"+serverPort+"/api")
.description("Server");
Server gatewayServer = new Server()
.url("http://"+host+":"+gatewayPort+"/api")
.description("Gateway Server");
serverList.add(gatewayServer);
serverList.add(server);
return new OpenAPI()
.servers(serverList)
.components(new Components()
.addSecuritySchemes("JWT-Token", new SecurityScheme()
.type(SecurityScheme.Type.HTTP)
.scheme("bearer")
.bearerFormat("JWT")
.in(SecurityScheme.In.HEADER)
.name("Authorization")))
.addSecurityItem(new SecurityRequirement().addList("JWT-Token"));
}
}
@Configuration
public class WebCorsConfigurer implements WebMvcConfigurer {
@Value("${server.host}")
String host;
@Value("${server.port}")
String serverPort;
@Value("${server.gateway.port}")
String gatewayPort;
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://" + host + ":" + serverPort,"http://" + host + ":" + gatewayPort)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH")
.allowedHeaders("*");
}
}
spring:
cloud:
gateway:
default-filters:
- DedupeResponseHeader=Access-Control-Allow-Origin Access-Control-Allow-Credentials
globalcors:
cors-configurations:
'[/**]':
allow-credentials: true
allowedHeaders:
- x-requested-with
- Authorization
- content-type
- credential
- X-AUTH-TOKEN
- X-CSRF-TOKEN
- X-User-Name
exposedHeaders:
- x-requested-with
- Authorization
- content-type
- credential
- X-AUTH-TOKEN
- X-CSRF-TOKEN
- X-User-Name
allowedMethods:
- GET
- POST
- PUT
- PATCH
- DELETE
- OPTIONS
cloud:
gateway:
globalcors:
cors-configurations:
'[/**]':
allowedOrigins:
- 'http://localhost:19092'
유정감