#swagger
springfox:
documentation:
swagger:
v2:
path: /swagger/api-docs
//swagger
// implementation 'io.springfox:springfox-swagger-ui:2.9.2'
// implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation group: 'io.springfox', name: 'springfox-boot-starter', version: '3.0.0'
@Component
public class Workaround implements WebMvcOpenApiTransformationFilter {
@Override
public OpenAPI transform(OpenApiTransformationContext<HttpServletRequest> context) {
OpenAPI openAPI = context.getSpecification();
Server localServer = new Server();
localServer.setDescription("local");
localServer.setUrl("http://localhost:9090");
Server realServer = new Server();
realServer.setDescription("server");
realServer.setUrl("https://sanneomeo.site/api");
openAPI.setServers(Arrays.asList(localServer,realServer));
return openAPI;
}
@Override
public boolean supports(DocumentationType delimiter) {
return delimiter.equals(DocumentationType.OAS_30);
}
}
서버를 선택해서 swagger api를 돌리기 위한 부분.
아무리 EnableSwagger2에서 url를 바꿔도 해당 부분을 설정안하면 실행이 안됨.
한번 변환이 필요.
해당 부분에는 token 설정을 할 수 있게 세팅하였음.
@Configuration
@EnableWebMvc
@EnableSwagger2
public class SwaagerConfig {
@Bean
public Docket api() {
Server serverLocal = new Server("local", "http://localhost:9090", "for local usages", Collections.emptyList(), Collections.emptyList());
Server realServer = new Server("test", "https://sanneomeo.site/api", "for testing", Collections.emptyList(), Collections.emptyList());
return new Docket(DocumentationType.OAS_30)
.servers(serverLocal,realServer)
.select()
.apis(RequestHandlerSelectors.basePackage("com.hikers.sanneomeo.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.securityContexts(Arrays.asList(securityContext()))
.securitySchemes(Arrays.asList(apiKey()));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Sanneomeo Swaager")
.description("Sanneomeo swagger")
.version("v3")
.build();
}
//전역적인 권한 해제를 위한 authorization api key 설정
private ApiKey apiKey() {
return new ApiKey("Authorization", "Authorization", "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.any())
.build();
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope
= new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Arrays.asList(
new SecurityReference("Authorization", authorizationScopes));
}
}
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
registry.addResourceHandler("/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
"/v3/api-docs/**", "/swagger-ui/**", "/swagger-resources/**"
해당 url에 대해 설정을 풀어준다.