You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
69 lines
2.6 KiB
69 lines
2.6 KiB
package com.qyft.ms.system.config;
|
|
|
|
import cn.hutool.core.util.ArrayUtil;
|
|
import io.swagger.v3.oas.models.Components;
|
|
import io.swagger.v3.oas.models.OpenAPI;
|
|
import io.swagger.v3.oas.models.info.Info;
|
|
import io.swagger.v3.oas.models.security.SecurityRequirement;
|
|
import io.swagger.v3.oas.models.security.SecurityScheme;
|
|
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.util.AntPathMatcher;
|
|
|
|
import java.util.stream.Stream;
|
|
|
|
/***
|
|
* 创建Swagger配置
|
|
*/
|
|
@Configuration
|
|
public class SwaggerConfig {
|
|
|
|
@Bean
|
|
public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
|
|
return openApi -> {
|
|
// 全局添加Authorization
|
|
if (openApi.getPaths() != null) {
|
|
openApi.getPaths().forEach((path, pathItem) -> {
|
|
|
|
// 忽略认证的请求无需携带 Authorization
|
|
String[] ignoreUrls = {"/api/auth/login"};
|
|
if (ArrayUtil.isNotEmpty(ignoreUrls)) {
|
|
// Ant 匹配忽略的路径,不添加Authorization
|
|
AntPathMatcher antPathMatcher = new AntPathMatcher();
|
|
if (Stream.of(ignoreUrls).anyMatch(ignoreUrl -> antPathMatcher.match(ignoreUrl, path))) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
// 其他接口统一添加Authorization
|
|
pathItem.readOperations()
|
|
.forEach(operation ->
|
|
operation.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
|
|
);
|
|
});
|
|
}
|
|
};
|
|
}
|
|
|
|
@Bean
|
|
public OpenAPI customOpenAPI() {
|
|
return new OpenAPI()
|
|
.info(new Info()
|
|
.title("基质喷涂")
|
|
.version("1.0")) // 配置全局鉴权参数-Authorize
|
|
.components(new Components()
|
|
.addSecuritySchemes(HttpHeaders.AUTHORIZATION,
|
|
new SecurityScheme()
|
|
.name(HttpHeaders.AUTHORIZATION)
|
|
.type(SecurityScheme.Type.APIKEY)
|
|
.in(SecurityScheme.In.HEADER)
|
|
.scheme("Bearer")
|
|
.bearerFormat("JWT")
|
|
)
|
|
);
|
|
}
|
|
|
|
|
|
}
|