@Data
@ConfigurationProperties(prefix = “swagger2”)
public class SwaggerProperties {
/*
包扫描的路径
*/
_private String basePackage ;
/
联系人的名称
/
private String name ;
/
联系人的主页
/
private String url ;
/
联系人的邮箱
/
private String email ;
/
API的标题
/
private String title ;
/
API的描述
/
private String description ;
/
API的版本号
/
private String version ;
/
API的服务团队
/
_private String termsOfServiceUrl ;
}
SwaggerAutoConfiguration**
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(SwaggerProperties.class)
public class SwaggerAutoConfiguration {
private SwaggerProperties swaggerProperties;
public SwaggerAutoConfiguration(SwaggerProperties swaggerProperties) {
this.swaggerProperties = swaggerProperties;
}
@Bean
public Docket docket() {
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
.paths(PathSelectors.any())
.build();
// 安全的配置
docket.securitySchemes(securitySchemes()) // 安全规则
.securityContexts(securityContexts()); // 安全配置的上下问
return docket;
}
/**
* api 信息的简介
*
* @return
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder().contact(
new Contact(swaggerProperties.getName(), swaggerProperties.getUrl(), swaggerProperties.getEmail())
)
.title(swaggerProperties.getTitle())
.description(swaggerProperties.getDescription())
.version(swaggerProperties.getVersion())
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
.build();
}
/**
* 安全的规则配置
*
* @return
*/
private List<SecurityScheme> securitySchemes() {
return Arrays.asList(new ApiKey("Authorization", "Authorization", "Authorization"));
}
/**
* 安全的上下问
*
* @return
*/
private List<SecurityContext> securityContexts() {
return Arrays.asList(new SecurityContext(
Arrays.asList(new SecurityReference("Authorization", new AuthorizationScope[]{new AuthorizationScope("global", "accessResource")})),
PathSelectors.any()
));
}
}