基础配置类
import springfox.documentation.service.SecurityScheme;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* swagger配置
*
* @author mori
* @date 2021-07-28 09:38:00
*/
public class BaseSwaggerConfig {
private final String basePackage;
private final String title;
private final String description;
private final Contact contact;
private final String version;
public BaseSwaggerConfig(String basePackage, String title, String description, Contact contact, String version) {
this.basePackage = basePackage;
this.title = title;
this.description = description;
this.contact = contact;
this.version = version;
}
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.any())
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title(title).description(description).contact(contact)
.version(version).build();
}
private List<SecurityScheme> securitySchemes() {
return Collections.singletonList(HttpAuthenticationScheme.JWT_BEARER_BUILDER
.name("Authorization")
.scheme("bearer")
.build());
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts = new ArrayList<>();
securityContexts.add(SecurityContext.builder()
.securityReferences(defaultAuth())
.build());
return securityContexts;
}
/**
* bearer 授权上下文
*
* @return List<SecurityContext>
*/
private List<SecurityContext> securityContext() {
SecurityContext context = new SecurityContext(
defaultAuth(),
// 配置需要访问授权的请求,效果是对应页面上的请求有没有小锁图标
PathSelectors.regex("/auth.*").negate(),
// 配置需要访问授权的请求,效果是对应页面上的请求有没有小锁图标
each -> true,
// operationSelector优先级高于上面两个,配置需要访问授权的请求,效果是对应页面上的请求有没有小锁图标
// 将auth开头的请求在swagger页面上放行,不使用jwt bearer token 授权方案
operationContext -> !operationContext.requestMappingPattern().matches("/auth.*")
);
return Collections.singletonList(context);
}
/**
* bearer 授权范围
*
* @return List<SecurityReference>
*/
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope
= new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = {authorizationScope};
return Collections.singletonList(new SecurityReference("Authorization", authorizationScopes));
}
}
配置使用
@Configuration
@EnableOpenApi
private static class SwaggerConfig extends BaseSwaggerConfig {
private final static String BASE_PACKAGE = "com.xxx.api.controller";
private final static String TITLE = "API TITLE";
private final static String DESCRIPTION = "API DESCRIPTION";
private final static Contact CONTACT = new Contact("name", "url", "email");
private final static String VERSION = "v0.1";
public SwaggerConfig() {
super(BASE_PACKAGE, TITLE, DESCRIPTION, CONTACT, VERSION);
}
}