-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
47 additions
and
23 deletions.
There are no files selected for viewing
70 changes: 47 additions & 23 deletions
70
src/main/java/project/backend/global/config/SwaggerConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,69 @@ | ||
package project.backend.global.config; | ||
|
||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
import springfox.documentation.builders.ApiInfoBuilder; | ||
import springfox.documentation.builders.PathSelectors; | ||
import springfox.documentation.builders.RequestHandlerSelectors; | ||
import springfox.documentation.service.ApiInfo; | ||
import springfox.documentation.service.ApiKey; | ||
import springfox.documentation.spi.DocumentationType; | ||
import springfox.documentation.spring.web.plugins.Docket; | ||
import springfox.documentation.swagger2.annotations.EnableSwagger2; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import springfox.documentation.service.AuthorizationScope; | ||
import springfox.documentation.service.SecurityReference; | ||
import springfox.documentation.spi.service.contexts.SecurityContext; | ||
|
||
import java.util.List; | ||
|
||
import static java.util.Collections.singleton; | ||
import static java.util.Collections.singletonList; | ||
import static springfox.documentation.builders.RequestHandlerSelectors.withMethodAnnotation; | ||
|
||
@Configuration | ||
@EnableSwagger2 | ||
public class SwaggerConfig { | ||
|
||
@Value("${swagger.host}") | ||
private String swaggerHost; | ||
// Docket 설정 | ||
@Bean | ||
public Docket restApi() { | ||
return new Docket(DocumentationType.SWAGGER_2) | ||
.apiInfo(apiInfo()) // optional | ||
.securitySchemes(singletonList(apiKey())) | ||
.securityContexts(singletonList(securityContext())) // Security 관련 설정 | ||
.produces(singleton("application/json")) | ||
.consumes(singleton("application/json")) | ||
.useDefaultResponseMessages(false) | ||
.select() | ||
.apis(withMethodAnnotation(ApiOperation.class)) | ||
.build(); | ||
} | ||
|
||
@Value("${swagger.protocol}") | ||
private String swaggerProtocol; | ||
private ApiKey apiKey() { | ||
return new ApiKey("apiKey", "내가 지정한 jwt header key name", "header"); | ||
} | ||
|
||
@Profile("default") | ||
@Bean | ||
public Docket apiDocket() { | ||
return createDocket(swaggerHost, swaggerProtocol); | ||
private ApiInfo apiInfo() { | ||
return new ApiInfoBuilder() | ||
.title("티캣츠 API") | ||
.description("알파벳이 붙은 api만 사용해주세요.") | ||
.version("1.0.0") | ||
.build(); | ||
} | ||
|
||
@Profile("local") | ||
@Bean | ||
public Docket apiDocketLocal(@Value("127.0.0.1:8080") String hostLocal, | ||
@Value("http") String protocolLocal) { | ||
return createDocket(hostLocal, protocolLocal); | ||
private SecurityContext securityContext() { | ||
return SecurityContext.builder() | ||
.securityReferences(securityReference()) | ||
.operationSelector(o -> true) | ||
.build(); | ||
} | ||
|
||
private Docket createDocket(String host, String protocol) { | ||
return new Docket(DocumentationType.SWAGGER_2) | ||
.select() | ||
.apis(RequestHandlerSelectors.any()) | ||
.build() | ||
.host(host) | ||
.protocols(new HashSet<>(Arrays.asList(protocol))); | ||
private List<SecurityReference> securityReference() { | ||
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything"); | ||
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1]; | ||
authorizationScopes[0] = authorizationScope; | ||
return singletonList(new SecurityReference("apiKey", authorizationScopes)); | ||
} | ||
} |