Skip to content

Commit

Permalink
추가: swagger authorization 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosiee committed May 12, 2024
1 parent a2f1ee0 commit 34006d5
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions src/main/java/project/backend/global/config/SwaggerConfig.java
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));
}
}

0 comments on commit 34006d5

Please sign in to comment.