Skip to content

Commit

Permalink
Docs : Swagger 문서 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
rrosiee committed May 23, 2024
1 parent a443229 commit ee4e545
Showing 1 changed file with 49 additions and 5 deletions.
54 changes: 49 additions & 5 deletions src/main/java/project/backend/global/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,66 @@

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.ApiInfoBuilder;
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 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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

// Docket 설정
@Bean
public Docket api() {
public Docket restApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) // optional
.apiInfo(apiInfo())
.securitySchemes(singletonList(apiKey()))
.securityContexts(singletonList(securityContext())) // Security 관련 설정
.securityContexts(singletonList(securityContext()))
.produces(singleton("application/json"))
.consumes(singleton("application/json"))
.useDefaultResponseMessages(false)
.select()
.apis(RequestHandlerSelectors.basePackage("project.backend")) // 변경 필요
.paths(PathSelectors.any())
.apis(RequestHandlerSelectors.basePackage("project.backend.domain"))
.build();
}

private ApiKey apiKey() {
return new ApiKey("Authorization", "Authorization", "header");
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("티캣츠 API")
.description("알파벳이 붙은 api만 사용해주세요.")
.version("1.0.0")
.build();
}

private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(securityReference())
.operationSelector(o -> true)
.build();
}
}

private List<SecurityReference> securityReference() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return singletonList(new SecurityReference("Authorization", authorizationScopes));
}
}

0 comments on commit ee4e545

Please sign in to comment.