2022-11-10 21:58:30
浏览数 (2)
导入依赖
代码语言:javascript
复制implementation 'com.github.xiaoymin:swagger-bootstrap-ui:1.9.6'
implementation 'com.spring4all:swagger-spring-boot-starter:1.9.1.RELEASE'
设置配置文件
代码语言:javascript
复制@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@Import({BeanValidatorPluginsConfiguration.class})
@EnableConfigurationProperties({SwaggerProperties.class})
@ConditionalOnProperty(
name = {"swagger.enabled"},
matchIfMissing = true
)
public class SwaggerAutoConfig {
public SwaggerAutoConfig() {
}
@Bean
@Order(1)
public Docket groupRestApi(SwaggerProperties swaggerProperties) {
return (new Docket(DocumentationType.SWAGGER_2)).host(swaggerProperties.getHost()).apiInfo(this.groupApiInfo(swaggerProperties)).select().apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage())).paths(PathSelectors.any()).build().securityContexts(Lists.newArrayList(new SecurityContext[]{this.securityContext()})).securitySchemes(Lists.newArrayList(new SecurityScheme[]{this.apiKey()}));
}
private ApiInfo groupApiInfo(SwaggerProperties swaggerProperties) {
Contact contact = new Contact(swaggerProperties.getContact().getName(), "https://www.dqgs.fun", swaggerProperties.getContact().getEmail());
return (new ApiInfoBuilder()).title(swaggerProperties.getTitle()).description(swaggerProperties.getDescription()).termsOfServiceUrl("http://www.dqgs.fun/").version(swaggerProperties.getVersion()).contact(contact).build();
}
private ApiKey apiKey() {
return new ApiKey("BearerToken", "Authorization", "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(this.defaultAuth()).forPaths(PathSelectors.regex("/.*")).build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[]{authorizationScope};
return Lists.newArrayList(new SecurityReference[]{new SecurityReference("BearerToken", authorizationScopes)});
}
}
页面展示