refactor: springboot3 + knife4j 请求添加token配置
This commit is contained in:
@@ -1,54 +1,74 @@
|
|||||||
package com.youlai.system.config;
|
package com.youlai.system.config;
|
||||||
|
|
||||||
import cn.hutool.core.util.RandomUtil;
|
|
||||||
import io.swagger.v3.oas.models.OpenAPI;
|
import io.swagger.v3.oas.models.OpenAPI;
|
||||||
import io.swagger.v3.oas.models.info.Info;
|
import io.swagger.v3.oas.models.info.Info;
|
||||||
import io.swagger.v3.oas.models.info.License;
|
import io.swagger.v3.oas.models.info.License;
|
||||||
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
|
import io.swagger.v3.oas.models.parameters.Parameter;
|
||||||
|
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||||
|
import org.springdoc.core.customizers.OperationCustomizer;
|
||||||
|
import org.springdoc.core.models.GroupedOpenApi;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
|
||||||
import java.util.HashMap;
|
/**
|
||||||
import java.util.Map;
|
* 接口文档配置
|
||||||
|
*
|
||||||
|
* @author haoxr
|
||||||
|
* @date 2023/2/17
|
||||||
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class SwaggerConfig {
|
public class SwaggerConfig {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据@Tag 上的排序,写入x-order
|
* 接口分组-系统接口
|
||||||
*
|
*
|
||||||
* @return the global open api customizer
|
* @param operationCustomizer
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public GlobalOpenApiCustomizer orderGlobalOpenApiCustomizer() {
|
public GroupedOpenApi systemApi(OperationCustomizer operationCustomizer) {
|
||||||
return openApi -> {
|
return GroupedOpenApi.builder()
|
||||||
if (openApi.getTags() != null) {
|
.group("系统接口")
|
||||||
openApi.getTags().forEach(tag -> {
|
.packagesToScan("com.youlai.system.controller")
|
||||||
Map<String, Object> map = new HashMap<>();
|
.pathsToMatch( "/api/**")
|
||||||
map.put("x-order", RandomUtil.randomInt(0, 100));
|
.addOperationCustomizer(operationCustomizer)
|
||||||
tag.setExtensions(map);
|
.build();
|
||||||
});
|
|
||||||
}
|
|
||||||
if (openApi.getPaths() != null) {
|
|
||||||
openApi.addExtension("x-test123", "333");
|
|
||||||
openApi.getPaths().addExtension("x-abb", RandomUtil.randomInt(1, 100));
|
|
||||||
}
|
|
||||||
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 鉴权
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public OpenAPI customOpenAPI() {
|
public OperationCustomizer operationCustomizer() {
|
||||||
return new OpenAPI()
|
return (operation, handlerMethod) -> operation.addParametersItem(
|
||||||
.info(new Info()
|
new Parameter()
|
||||||
.title("XXX用户系统API")
|
.in(SecurityScheme.In.HEADER.toString())
|
||||||
.version("1.0")
|
.name(HttpHeaders.AUTHORIZATION)
|
||||||
|
.required(true)
|
||||||
|
.description("token 校验"));
|
||||||
|
}
|
||||||
|
|
||||||
.description("Knife4j集成springdoc-openapi示例")
|
|
||||||
.termsOfService("http://doc.xiaominfo.com")
|
/**
|
||||||
|
* 接口信息
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public OpenAPI apiInfo() {
|
||||||
|
return new OpenAPI().info(
|
||||||
|
new Info()
|
||||||
|
.title("youlai-boot接口文档")
|
||||||
|
.version("0.0.1")
|
||||||
|
.description("youlai-boot接口文档")
|
||||||
.license(new License().name("Apache 2.0")
|
.license(new License().name("Apache 2.0")
|
||||||
.url("http://doc.xiaominfo.com")));
|
.url("https://www.youlai.tech")
|
||||||
|
)
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user