feat: springdoc 开启认证

This commit is contained in:
xingyu 2022-12-22 00:49:23 +08:00
parent cacefb4eea
commit 683b49a8a4
2 changed files with 24 additions and 4 deletions

View File

@ -6,7 +6,10 @@ 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 io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityRequirement;
import io.swagger.v3.oas.models.security.SecurityScheme; import io.swagger.v3.oas.models.security.SecurityScheme;
import org.springdoc.core.GroupedOpenApi; import org.springdoc.core.*;
import org.springdoc.core.customizers.OpenApiBuilderCustomizer;
import org.springdoc.core.customizers.ServerBaseUrlCustomizer;
import org.springdoc.core.providers.JavadocProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@ -15,6 +18,8 @@ import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import java.util.Arrays; import java.util.Arrays;
import java.util.List;
import java.util.Optional;
/** /**
* Swagger3 自动配置类 * Swagger3 自动配置类
@ -38,17 +43,17 @@ public class YudaoSwaggerAutoConfiguration {
.license(new License().name("MIT").url("https://gitee.com/zhijiantianya/ruoyi-vue-pro/blob/master/LICENSE")); .license(new License().name("MIT").url("https://gitee.com/zhijiantianya/ruoyi-vue-pro/blob/master/LICENSE"));
//鉴权组件(随便起名的) //鉴权组件(随便起名的)
SecurityScheme securityScheme = new SecurityScheme() SecurityScheme securityScheme = new SecurityScheme()
.type(SecurityScheme.Type.OAUTH2) .type(SecurityScheme.Type.APIKEY)
.scheme("bearer")//固定写法 .scheme("bearer")//固定写法
.bearerFormat("JWT") .bearerFormat("JWT")
.in(SecurityScheme.In.HEADER) .in(SecurityScheme.In.HEADER)
.name(HttpHeaders.AUTHORIZATION); .name(HttpHeaders.AUTHORIZATION);
Components components = new Components() Components components = new Components()
.addSecuritySchemes("bearer", securityScheme); .addSecuritySchemes("Bearer", securityScheme);
//鉴权限制要求(随便起名的) //鉴权限制要求(随便起名的)
SecurityRequirement securityRequirement = new SecurityRequirement() SecurityRequirement securityRequirement = new SecurityRequirement()
.addList("bearer", Arrays.asList("read", "write")); .addList(HttpHeaders.AUTHORIZATION, Arrays.asList("read", "write"));
return new OpenAPI() return new OpenAPI()
.info(info) .info(info)
@ -56,6 +61,20 @@ public class YudaoSwaggerAutoConfiguration {
.addSecurityItem(securityRequirement); .addSecurityItem(securityRequirement);
} }
/**
* 自定义 openapi 处理器
*/
@Bean
public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,
SecurityService securityParser,
SpringDocConfigProperties springDocConfigProperties,
PropertyResolverUtils propertyResolverUtils,
Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomisers,
Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomisers,
Optional<JavadocProvider> javadocProvider) {
return new OpenAPIService(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomisers, serverBaseUrlCustomisers, javadocProvider);
}
@Bean @Bean
public GroupedOpenApi appApi() { public GroupedOpenApi appApi() {
return GroupedOpenApi.builder() return GroupedOpenApi.builder()

View File

@ -35,6 +35,7 @@ spring:
time-to-live: 1h # 设置过期时间为 1 小时 time-to-live: 1h # 设置过期时间为 1 小时
springdoc: springdoc:
show-actuator: true
swagger-ui: swagger-ui:
path: /swagger-ui path: /swagger-ui