refactor: 接口文档信息完善和移除硬编码
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
package com.youlai.boot.config;
|
||||
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import com.youlai.boot.config.property.SecurityProperties;
|
||||
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.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@@ -13,6 +17,8 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* OpenAPI 接口文档配置
|
||||
*
|
||||
@@ -27,10 +33,11 @@ public class OpenApiConfig {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
/**
|
||||
* 接口信息
|
||||
*/
|
||||
private final SecurityProperties securityProperties;
|
||||
|
||||
/**
|
||||
* 接口文档信息
|
||||
*/
|
||||
@Bean
|
||||
public OpenAPI openApi() {
|
||||
|
||||
@@ -38,8 +45,18 @@ public class OpenApiConfig {
|
||||
|
||||
return new OpenAPI()
|
||||
.info(new Info()
|
||||
.title("系统接口文档")
|
||||
.title("管理系统 API 文档")
|
||||
.description("本文档涵盖管理系统的所有API接口,包括登录认证、用户管理、角色管理、部门管理等功能模块,提供详细的接口说明和使用指南。")
|
||||
.version(appVersion)
|
||||
.license(new License()
|
||||
.name("Apache License 2.0")
|
||||
.url("http://www.apache.org/licenses/LICENSE-2.0")
|
||||
)
|
||||
.contact(new Contact()
|
||||
.name("youlai")
|
||||
.email("youlaitech@163.com")
|
||||
.url("https://www.youlai.tech")
|
||||
)
|
||||
)
|
||||
// 配置全局鉴权参数-Authorize
|
||||
.components(new Components()
|
||||
@@ -57,21 +74,23 @@ public class OpenApiConfig {
|
||||
|
||||
/**
|
||||
* 全局自定义扩展
|
||||
* <p>
|
||||
* 在OpenAPI规范中,Operation 是一个表示 API 端点(Endpoint)或操作的对象。
|
||||
* 每个路径(Path)对象可以包含一个或多个 Operation 对象,用于描述与该路径相关联的不同 HTTP 方法(例如 GET、POST、PUT 等)。
|
||||
*/
|
||||
@Bean
|
||||
public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
|
||||
return openApi -> {
|
||||
// 全局添加鉴权参数
|
||||
// 全局添加Authorization
|
||||
if (openApi.getPaths() != null) {
|
||||
openApi.getPaths().forEach((s, pathItem) -> {
|
||||
// 登录接口/验证码不需要添加鉴权参数
|
||||
if ("/api/v1/auth/login".equals(s) || "/api/v1/auth/captcha".equals(s)) {
|
||||
return;
|
||||
openApi.getPaths().forEach((path, pathItem) -> {
|
||||
|
||||
// 忽略认证的请求无需携带Authorization
|
||||
String[] ignoreUrls = securityProperties.getIgnoreUrls();
|
||||
if (ArrayUtil.isNotEmpty(ignoreUrls)) {
|
||||
if (Stream.of(ignoreUrls).anyMatch(path::equals)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 接口添加鉴权参数
|
||||
|
||||
// 其他接口统一添加Authorization
|
||||
pathItem.readOperations()
|
||||
.forEach(operation ->
|
||||
operation.addSecurityItem(new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION))
|
||||
|
||||
Reference in New Issue
Block a user