feat: springdoc success

pull/2/head
xingyu 2023-01-16 22:58:56 +08:00
parent 18c6445802
commit 4d35bcef65
6 changed files with 81 additions and 37 deletions

View File

@ -18,7 +18,8 @@
<!-- 统一依赖管理 -->
<spring.boot.version>2.7.7</spring.boot.version>
<!-- Web 相关 -->
<knife4j.version>4.0.0</knife4j.version>
<springdoc.version>1.6.14</springdoc.version>
<!-- <knife4j.version>4.0.0</knife4j.version>-->
<servlet.versoin>2.5</servlet.versoin>
<!-- DB 相关 -->
<druid.version>1.2.15</druid.version>
@ -163,10 +164,15 @@
<version>${revision}</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.xiaoymin</groupId>-->
<!-- <artifactId>knife4j-openapi3-spring-boot-starter</artifactId>-->
<!-- <version>${knife4j.version}</version>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc.version}</version>
</dependency>
<!-- DB 相关 -->
@ -276,16 +282,16 @@
<groupId>org.apache.skywalking</groupId>
<artifactId>apm-toolkit-opentracing</artifactId>
<version>${skywalking.version}</version>
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <artifactId>opentracing-api</artifactId>-->
<!-- <groupId>io.opentracing</groupId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <artifactId>opentracing-util</artifactId>-->
<!-- <groupId>io.opentracing</groupId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
<!-- <exclusions>-->
<!-- <exclusion>-->
<!-- <artifactId>opentracing-api</artifactId>-->
<!-- <groupId>io.opentracing</groupId>-->
<!-- </exclusion>-->
<!-- <exclusion>-->
<!-- <artifactId>opentracing-util</artifactId>-->
<!-- <groupId>io.opentracing</groupId>-->
<!-- </exclusion>-->
<!-- </exclusions>-->
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
@ -448,7 +454,7 @@
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId> <!-- 移除 Freemarker 依赖,采用 Velocity 作为模板引擎 -->
</exclusion>
<exclusion>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId> <!-- 最新版screw-core1.0.5依赖fastjson1.2.73存在漏洞,移除。 -->
</exclusion>

View File

@ -58,10 +58,16 @@
<scope>provided</scope> <!-- 设置为 provided只有工具类需要使用到 -->
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.xiaoymin</groupId>-->
<!-- <artifactId>knife4j-openapi3-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<scope>provided</scope> <!-- 设置为 provided主要是 PageParam 使用到 -->
</dependency>
<!-- 监控相关 -->
<dependency>
<groupId>org.apache.skywalking</groupId>

View File

@ -33,9 +33,13 @@
<optional>true</optional>
</dependency>
<!-- <dependency>-->
<!-- <groupId>com.github.xiaoymin</groupId>-->
<!-- <artifactId>knife4j-openapi3-spring-boot-starter</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-openapi3-spring-boot-starter</artifactId>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
</dependency>
<dependency>

View File

@ -1,9 +1,12 @@
package cn.iocoder.yudao.framework.swagger.config;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import io.swagger.v3.oas.models.media.IntegerSchema;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.GroupedOpenApi;
import org.springdoc.core.*;
@ -17,10 +20,11 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID;
/**
* Swagger3
*
@ -40,20 +44,23 @@ public class YudaoSwaggerAutoConfiguration {
.title(properties.getTitle())
.description(properties.getDescription())
.version(properties.getVersion())
.contact(new Contact().name("xingyuv").url("xingyuv.com").email("xingyu4j@vip.qq.com"))
.license(new License().name("MIT").url("https://gitee.com/zhijiantianya/ruoyi-vue-pro/blob/master/LICENSE"));
//鉴权组件(随便起名的)
SecurityScheme securityScheme = new SecurityScheme()
.type(SecurityScheme.Type.APIKEY)
.scheme("Bearer")//固定写法
.bearerFormat("JWT")
.in(SecurityScheme.In.HEADER)
.name(HttpHeaders.AUTHORIZATION);
Components components = new Components()
.addSecuritySchemes("bearer", securityScheme);
return new OpenAPI()
.info(info)
.components(components);
.schemaRequirement(HttpHeaders.AUTHORIZATION, securityScheme())
.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION));
}
private SecurityScheme securityScheme() {
SecurityScheme securityScheme = new SecurityScheme();
//类型
securityScheme.setType(SecurityScheme.Type.APIKEY);
//请求头的name
securityScheme.setName(HttpHeaders.AUTHORIZATION);
//token所在未知
securityScheme.setIn(SecurityScheme.In.HEADER);
return securityScheme;
}
/**
@ -74,6 +81,9 @@ public class YudaoSwaggerAutoConfiguration {
public GroupedOpenApi appApi() {
return GroupedOpenApi.builder()
.group("app")
.addOperationCustomizer((operation, handlerMethod) ->
operation.addParametersItem(globalHeaderParameter())
)
.pathsToMatch("/app-api/**")
.build();
}
@ -82,9 +92,24 @@ public class YudaoSwaggerAutoConfiguration {
public GroupedOpenApi adminApi() {
return GroupedOpenApi.builder()
.group("admin")
.addOperationCustomizer((operation, handlerMethod) ->
operation.addParametersItem(globalHeaderParameter())
)
.pathsToMatch("/admin-api/**")
.build();
}
private static Parameter globalHeaderParameter() {
return new Parameter()
.name(HEADER_TENANT_ID)
.description("租户编号")
.in(String.valueOf(SecurityScheme.In.HEADER))
.schema(new IntegerSchema()
._default(1L)
.name(HEADER_TENANT_ID)
.description("租户编号")
);
}
}

View File

@ -37,14 +37,17 @@ spring:
springdoc:
show-actuator: true
swagger-ui:
enabled: true
path: /swagger-ui
api-docs:
enabled: true
path: /v3/api-docs
knife4j:
enable: true
setting:
language: zh_cn
#knife4j:
# enable: true
# setting:
# language: zh_cn
# 工作流 Flowable 配置
flowable:

View File

@ -8,5 +8,5 @@ import { ref } from 'vue'
import { IFrame } from '@/components/IFrame'
const BASE_URL = import.meta.env.VITE_BASE_URL
const src = ref(BASE_URL + '/doc.html')
const src = ref(BASE_URL + '/swagger-ui')
</script>