Commit 683b49a8a49d6e1833b230431fddc5ef48acb1ed
1 parent
cacefb4e
feat: springdoc 开启认证
Showing
2 changed files
with
24 additions
and
4 deletions
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/swagger/config/YudaoSwaggerAutoConfiguration.java
| ... | ... | @@ -6,7 +6,10 @@ import io.swagger.v3.oas.models.info.Info; |
| 6 | 6 | import io.swagger.v3.oas.models.info.License; |
| 7 | 7 | import io.swagger.v3.oas.models.security.SecurityRequirement; |
| 8 | 8 | import io.swagger.v3.oas.models.security.SecurityScheme; |
| 9 | -import org.springdoc.core.GroupedOpenApi; | |
| 9 | +import org.springdoc.core.*; | |
| 10 | +import org.springdoc.core.customizers.OpenApiBuilderCustomizer; | |
| 11 | +import org.springdoc.core.customizers.ServerBaseUrlCustomizer; | |
| 12 | +import org.springdoc.core.providers.JavadocProvider; | |
| 10 | 13 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 11 | 14 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 12 | 15 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| ... | ... | @@ -15,6 +18,8 @@ import org.springframework.context.annotation.Bean; |
| 15 | 18 | import org.springframework.http.HttpHeaders; |
| 16 | 19 | |
| 17 | 20 | import java.util.Arrays; |
| 21 | +import java.util.List; | |
| 22 | +import java.util.Optional; | |
| 18 | 23 | |
| 19 | 24 | /** |
| 20 | 25 | * Swagger3 自动配置类 |
| ... | ... | @@ -38,17 +43,17 @@ public class YudaoSwaggerAutoConfiguration { |
| 38 | 43 | .license(new License().name("MIT").url("https://gitee.com/zhijiantianya/ruoyi-vue-pro/blob/master/LICENSE")); |
| 39 | 44 | //鉴权组件(随便起名的) |
| 40 | 45 | SecurityScheme securityScheme = new SecurityScheme() |
| 41 | - .type(SecurityScheme.Type.OAUTH2) | |
| 46 | + .type(SecurityScheme.Type.APIKEY) | |
| 42 | 47 | .scheme("bearer")//固定写法 |
| 43 | 48 | .bearerFormat("JWT") |
| 44 | 49 | .in(SecurityScheme.In.HEADER) |
| 45 | 50 | .name(HttpHeaders.AUTHORIZATION); |
| 46 | 51 | Components components = new Components() |
| 47 | - .addSecuritySchemes("bearer", securityScheme); | |
| 52 | + .addSecuritySchemes("Bearer", securityScheme); | |
| 48 | 53 | |
| 49 | 54 | //鉴权限制要求(随便起名的) |
| 50 | 55 | SecurityRequirement securityRequirement = new SecurityRequirement() |
| 51 | - .addList("bearer", Arrays.asList("read", "write")); | |
| 56 | + .addList(HttpHeaders.AUTHORIZATION, Arrays.asList("read", "write")); | |
| 52 | 57 | |
| 53 | 58 | return new OpenAPI() |
| 54 | 59 | .info(info) |
| ... | ... | @@ -56,6 +61,20 @@ public class YudaoSwaggerAutoConfiguration { |
| 56 | 61 | .addSecurityItem(securityRequirement); |
| 57 | 62 | } |
| 58 | 63 | |
| 64 | + /** | |
| 65 | + * 自定义 openapi 处理器 | |
| 66 | + */ | |
| 67 | + @Bean | |
| 68 | + public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI, | |
| 69 | + SecurityService securityParser, | |
| 70 | + SpringDocConfigProperties springDocConfigProperties, | |
| 71 | + PropertyResolverUtils propertyResolverUtils, | |
| 72 | + Optional<List<OpenApiBuilderCustomizer>> openApiBuilderCustomisers, | |
| 73 | + Optional<List<ServerBaseUrlCustomizer>> serverBaseUrlCustomisers, | |
| 74 | + Optional<JavadocProvider> javadocProvider) { | |
| 75 | + return new OpenAPIService(openAPI, securityParser, springDocConfigProperties, propertyResolverUtils, openApiBuilderCustomisers, serverBaseUrlCustomisers, javadocProvider); | |
| 76 | + } | |
| 77 | + | |
| 59 | 78 | @Bean |
| 60 | 79 | public GroupedOpenApi appApi() { |
| 61 | 80 | return GroupedOpenApi.builder() | ... | ... |