增加鉴权
parent
b6fb0372f1
commit
4779043537
|
@ -1,11 +1,15 @@
|
||||||
package com.muyu.common.api.doc.config;
|
package com.muyu.common.api.doc.config;
|
||||||
|
|
||||||
|
|
||||||
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 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 {
|
||||||
|
@ -31,4 +35,30 @@ public class SpringDocConfig {
|
||||||
.info(info())
|
.info(info())
|
||||||
.externalDocs(externalDocumentation());
|
.externalDocs(externalDocumentation());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@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))
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue