feat():增加鉴权
parent
8f0f886884
commit
259ae08ca6
|
@ -1,11 +1,16 @@
|
||||||
package com.muyu.common.api.doc.config;
|
package com.muyu.common.api.doc.config;
|
||||||
|
|
||||||
|
import io.swagger.v3.oas.models.Components;
|
||||||
import io.swagger.v3.oas.models.ExternalDocumentation;
|
import io.swagger.v3.oas.models.ExternalDocumentation;
|
||||||
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.security.SecurityRequirement;
|
||||||
|
import io.swagger.v3.oas.models.security.SecurityScheme;
|
||||||
|
import org.springdoc.core.customizers.GlobalOpenApiCustomizer;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
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;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public class SpringDocConfig {
|
public class SpringDocConfig {
|
||||||
|
@ -29,6 +34,37 @@ public class SpringDocConfig {
|
||||||
public OpenAPI springShopOpenAPI() {
|
public OpenAPI springShopOpenAPI() {
|
||||||
return new OpenAPI()
|
return new OpenAPI()
|
||||||
.info(info())
|
.info(info())
|
||||||
.externalDocs(externalDocumentation());
|
.externalDocs(externalDocumentation())
|
||||||
|
.components(new Components()
|
||||||
|
.addSecuritySchemes(HttpHeaders.AUTHORIZATION, new SecurityScheme()
|
||||||
|
.name(HttpHeaders.AUTHORIZATION)
|
||||||
|
.type(SecurityScheme.Type.APIKEY)
|
||||||
|
.in(SecurityScheme.In.HEADER)
|
||||||
|
.scheme("Bearer")
|
||||||
|
.bearerFormat("JWT")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public GlobalOpenApiCustomizer globalOpenApiCustomizer() {
|
||||||
|
return openApi -> {
|
||||||
|
// 全局添加鉴权参数
|
||||||
|
if (openApi.getPaths() != null) {
|
||||||
|
openApi.getPaths().forEach((s, pathItem) -> {
|
||||||
|
// 登录接口/验证码不需要添加鉴权参数
|
||||||
|
if (s.equals("/login") || s.equals("/code") || s.equals("/register")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// 接口添加鉴权参数
|
||||||
|
pathItem.readOperations()
|
||||||
|
.forEach(operation ->
|
||||||
|
operation.addSecurityItem(
|
||||||
|
new SecurityRequirement().addList(HttpHeaders.AUTHORIZATION)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
|
@ -1,5 +1,5 @@
|
||||||
#Generated by Maven
|
#Generated by Maven
|
||||||
#Wed Jul 24 20:31:46 CST 2024
|
#Thu Aug 08 14:00:55 CST 2024
|
||||||
groupId=com.muyu
|
groupId=com.muyu
|
||||||
artifactId=cloud-common-api-doc
|
artifactId=cloud-common-api-doc
|
||||||
version=3.6.3
|
version=3.6.3
|
||||||
|
|
Loading…
Reference in New Issue