From c49bb07e7121a7e9d34b6634a3746d4daf602bf7 Mon Sep 17 00:00:00 2001 From: JIUNG YANG Date: Sun, 18 Aug 2024 14:44:58 +0900 Subject: [PATCH] =?UTF-8?q?SB-261=20(Feat)=20:=20=ED=86=B5=ED=95=A9=20Swag?= =?UTF-8?q?ger=20API=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit SB-261 (Feat) : 통합 Swagger API 구현 --- .github/workflows/gradle.yml | 2 +- .../java/delivery/DeliveryApplication.java | 4 +- .../delivery/common/config/web/WebConfig.java | 37 +++++++++++++++++++ gateway/build.gradle | 2 + .../java/warehouse/WarehouseApplication.java | 2 +- .../common/config/web/WebConfig.java | 17 +++++++++ 6 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 delivery/src/main/java/delivery/common/config/web/WebConfig.java diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index 51def743..698fa9b6 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -21,7 +21,7 @@ jobs: - name: Make docker-compose.yml run: echo "${{ secrets.DOCKER_COMPOSE }}" > docker-compose.yml - - name: Make Warehouse application.yml + - name: Make Gateway application.yml run: | cd ./gateway/src/main/resources echo "${{ secrets.GATEWAY_APPLICATION }}" > application.yml diff --git a/delivery/src/main/java/delivery/DeliveryApplication.java b/delivery/src/main/java/delivery/DeliveryApplication.java index 35ced1a3..32ae6e6d 100644 --- a/delivery/src/main/java/delivery/DeliveryApplication.java +++ b/delivery/src/main/java/delivery/DeliveryApplication.java @@ -1,9 +1,11 @@ package delivery; +import io.swagger.v3.oas.annotations.OpenAPIDefinition; +import io.swagger.v3.oas.annotations.servers.Server; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -//@OpenAPIDefinition(servers = {@Server(url = "https://baobab.run", description = "fx server 연습용 도메인")}) +@OpenAPIDefinition(servers = {@Server(url = "https://baobab.run/delivery/", description = "Baobab Dev Server API")}) @SpringBootApplication public class DeliveryApplication { diff --git a/delivery/src/main/java/delivery/common/config/web/WebConfig.java b/delivery/src/main/java/delivery/common/config/web/WebConfig.java new file mode 100644 index 00000000..e9c4c07d --- /dev/null +++ b/delivery/src/main/java/delivery/common/config/web/WebConfig.java @@ -0,0 +1,37 @@ +package delivery.common.config.web; + +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.security.SecurityRequirement; +import io.swagger.v3.oas.models.security.SecurityScheme; +import java.util.Collections; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebConfig implements WebMvcConfigurer { + + @Value("${file.upload-dir}") + private String uploadDir; + + @Bean + public OpenAPI swaggerAuthorization() { + SecurityScheme securityScheme = new SecurityScheme() + .type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT") + .in(SecurityScheme.In.HEADER).name("Authorization"); + SecurityRequirement securityRequirement = new SecurityRequirement().addList("bearerAuth"); + + return new OpenAPI() + .components(new Components().addSecuritySchemes("bearerAuth", securityScheme)) + .security(Collections.singletonList(securityRequirement)); + } + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/images/**") + .addResourceLocations("file:" + uploadDir); + } +} \ No newline at end of file diff --git a/gateway/build.gradle b/gateway/build.gradle index c6e1a195..f77c40f1 100644 --- a/gateway/build.gradle +++ b/gateway/build.gradle @@ -23,6 +23,8 @@ ext { dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-gateway' + implementation 'org.springdoc:springdoc-openapi-starter-webflux-ui:2.0.2' + implementation 'org.springdoc:springdoc-openapi-ui:1.7.0' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/warehouse/src/main/java/warehouse/WarehouseApplication.java b/warehouse/src/main/java/warehouse/WarehouseApplication.java index 66d531be..b8270908 100644 --- a/warehouse/src/main/java/warehouse/WarehouseApplication.java +++ b/warehouse/src/main/java/warehouse/WarehouseApplication.java @@ -7,7 +7,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties; import warehouse.domain.image.properties.FileStorageProperties; -@OpenAPIDefinition(servers = {@Server(url = "https://baobab.run", description = "fx server 연습용 도메인")}) +@OpenAPIDefinition(servers = {@Server(url = "https://baobab.run/warehouse/", description = "Baobab Dev Server API")}) @SpringBootApplication @EnableConfigurationProperties({ FileStorageProperties.class diff --git a/warehouse/src/main/java/warehouse/common/config/web/WebConfig.java b/warehouse/src/main/java/warehouse/common/config/web/WebConfig.java index 485a8d82..f36c5a5e 100644 --- a/warehouse/src/main/java/warehouse/common/config/web/WebConfig.java +++ b/warehouse/src/main/java/warehouse/common/config/web/WebConfig.java @@ -1,5 +1,10 @@ package warehouse.common.config.web; +import io.swagger.v3.oas.models.Components; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.security.SecurityRequirement; +import io.swagger.v3.oas.models.security.SecurityScheme; +import java.util.Collections; import java.util.List; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -30,6 +35,18 @@ CorsConfigurationSource corsConfigurationSource() { return source; } + @Bean + public OpenAPI swaggerAuthorization() { + SecurityScheme securityScheme = new SecurityScheme() + .type(SecurityScheme.Type.HTTP).scheme("bearer").bearerFormat("JWT") + .in(SecurityScheme.In.HEADER).name("Authorization"); + SecurityRequirement securityRequirement = new SecurityRequirement().addList("bearerAuth"); + + return new OpenAPI() + .components(new Components().addSecuritySchemes("bearerAuth", securityScheme)) + .security(Collections.singletonList(securityRequirement)); + } + @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/images/**")