diff --git a/blade-core-swagger/src/main/java/org/springblade/core/swagger/SwaggerAutoConfiguration.java b/blade-core-swagger/src/main/java/org/springblade/core/swagger/SwaggerAutoConfiguration.java index 3a22ef8..31f2123 100644 --- a/blade-core-swagger/src/main/java/org/springblade/core/swagger/SwaggerAutoConfiguration.java +++ b/blade-core-swagger/src/main/java/org/springblade/core/swagger/SwaggerAutoConfiguration.java @@ -16,7 +16,10 @@ package org.springblade.core.swagger; +import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver; import com.google.common.collect.Lists; +import lombok.AllArgsConstructor; +import org.springblade.core.launch.props.BladeProperties; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -45,12 +48,22 @@ import java.util.List; @EnableSwagger @EnableConfigurationProperties(SwaggerProperties.class) @Import(BeanValidatorPluginsConfiguration.class) +@AllArgsConstructor public class SwaggerAutoConfiguration { - private static final String DEFAULT_MAPPING_PATH = "/"; private static final String DEFAULT_BASE_PATH = "/**"; private static final List DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**"); + /** + * 引入Knife4j扩展类 + */ + private final OpenApiExtensionResolver openApiExtensionResolver; + + /** + * 引入Blade环境变量 + */ + private final BladeProperties bladeProperties; + @Bean @ConditionalOnMissingBean public SwaggerProperties swaggerProperties() { @@ -77,11 +90,11 @@ public class SwaggerAutoConfiguration { swaggerProperties.getExcludePath().forEach(p -> apis.paths(PathSelectors.ant(p).negate())); return apis.build() - .securitySchemes(Collections.singletonList(securitySchema())) - .securityContexts(Collections.singletonList(securityContext())) - .securityContexts(Lists.newArrayList(securityContext())) - .securitySchemes(Collections.singletonList(securitySchema())) - .pathMapping(DEFAULT_MAPPING_PATH); + .securitySchemes(Collections.singletonList(securitySchema(swaggerProperties))) + .securityContexts(Collections.singletonList(securityContext(swaggerProperties))) + .securityContexts(Lists.newArrayList(securityContext(swaggerProperties))) + .securitySchemes(Collections.singletonList(securitySchema(swaggerProperties))) + .extensions(openApiExtensionResolver.buildExtensions(bladeProperties.getName())); } /** @@ -89,10 +102,10 @@ public class SwaggerAutoConfiguration { * * @return */ - private SecurityContext securityContext() { + private SecurityContext securityContext(SwaggerProperties swaggerProperties) { return SecurityContext.builder() - .securityReferences(defaultAuth()) - .forPaths(PathSelectors.regex(swaggerProperties().getAuthorization().getAuthRegex())) + .securityReferences(defaultAuth(swaggerProperties)) + .forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex())) .build(); } @@ -101,23 +114,23 @@ public class SwaggerAutoConfiguration { * * @return */ - private List defaultAuth() { + private List defaultAuth(SwaggerProperties swaggerProperties) { ArrayList authorizationScopeList = new ArrayList<>(); - swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); + swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()]; return Collections.singletonList(SecurityReference.builder() - .reference(swaggerProperties().getAuthorization().getName()) + .reference(swaggerProperties.getAuthorization().getName()) .scopes(authorizationScopeList.toArray(authorizationScopes)) .build()); } - private OAuth securitySchema() { + private OAuth securitySchema(SwaggerProperties swaggerProperties) { ArrayList authorizationScopeList = new ArrayList<>(); - swaggerProperties().getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); + swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription()))); ArrayList grantTypes = new ArrayList<>(); - swaggerProperties().getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl))); - return new OAuth(swaggerProperties().getAuthorization().getName(), authorizationScopeList, grantTypes); + swaggerProperties.getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl))); + return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes); } private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {