com.eworkcloud.swagger.SwaggerConfiguration Maven / Gradle / Ivy
package com.eworkcloud.swagger;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.SecurityConfiguration;
import springfox.documentation.swagger.web.SecurityConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Configuration
@EnableSwagger2WebMvc
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerConfiguration implements WebMvcConfigurer {
private static final String[] defKeys = new String[]{"Authorization"};
@Resource
private SwaggerProperties swaggerProperties;
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(swaggerProperties.getTitle())
.version(swaggerProperties.getVersion())
.build();
}
private List securitySchemes() {
String[] apiKeys = defKeys;
if (StringUtils.hasText(swaggerProperties.getApiKeys())) {
apiKeys = swaggerProperties.getApiKeys().split(",");
}
List securitySchemes = new ArrayList<>();
for (String apikey : apiKeys) {
securitySchemes.add(new ApiKey(apikey, apikey, "header"));
}
return securitySchemes;
}
private List securityReferences() {
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = new AuthorizationScope("global", "accessEverything");
String[] apiKeys = defKeys;
if (StringUtils.hasText(swaggerProperties.getApiKeys())) {
apiKeys = swaggerProperties.getApiKeys().split(",");
}
List securityReferences = new ArrayList<>();
for (String apikey : apiKeys) {
securityReferences.add(new SecurityReference(apikey, authorizationScopes));
}
return securityReferences;
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(securityReferences())
.forPaths(PathSelectors.any())
.build();
}
@Bean
public Docket swagger2Docket() {
return new Docket(DocumentationType.SWAGGER_2)
.enable(StringUtils.hasText(swaggerProperties.getTitle()))
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build()
.securitySchemes(securitySchemes())
.securityContexts(Collections.singletonList(securityContext()));
}
@Bean
public SecurityConfiguration securityConfiguration() {
return SecurityConfigurationBuilder.builder()
.scopeSeparator(",")
.additionalQueryStringParams(null)
.useBasicAuthenticationWithAccessCodeGrant(false)
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy