2018-12-24 11:58:45 +08:00
|
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2018-2028, lengleng (wangiegie@gmail.com).
|
|
|
|
|
* <p>
|
2018-12-29 18:46:01 +08:00
|
|
|
|
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
* <p>
|
|
|
|
|
* http://www.gnu.org/licenses/lgpl.html
|
|
|
|
|
* <p>
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package org.springblade.core.swagger;
|
|
|
|
|
|
|
|
|
|
|
2021-01-15 20:37:59 +08:00
|
|
|
|
import com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver;
|
2020-11-06 17:12:53 +08:00
|
|
|
|
import com.google.common.collect.Lists;
|
2021-01-15 20:37:59 +08:00
|
|
|
|
import lombok.AllArgsConstructor;
|
|
|
|
|
import org.springblade.core.launch.props.BladeProperties;
|
2022-07-12 01:58:20 +08:00
|
|
|
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
2018-12-24 12:47:07 +08:00
|
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
import org.springframework.context.annotation.Bean;
|
2019-12-22 23:43:17 +08:00
|
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
|
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
import springfox.documentation.builders.ApiInfoBuilder;
|
|
|
|
|
import springfox.documentation.builders.PathSelectors;
|
|
|
|
|
import springfox.documentation.service.*;
|
|
|
|
|
import springfox.documentation.spi.DocumentationType;
|
|
|
|
|
import springfox.documentation.spi.service.contexts.SecurityContext;
|
2020-11-06 17:12:53 +08:00
|
|
|
|
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2020-11-06 17:12:53 +08:00
|
|
|
|
import java.util.Arrays;
|
2018-12-24 11:58:45 +08:00
|
|
|
|
import java.util.Collections;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* swagger配置
|
2018-12-27 13:29:11 +08:00
|
|
|
|
*
|
2018-12-29 18:43:11 +08:00
|
|
|
|
* @author Chill
|
2018-12-24 11:58:45 +08:00
|
|
|
|
*/
|
2022-07-12 01:58:20 +08:00
|
|
|
|
@AutoConfiguration
|
2020-11-06 17:12:53 +08:00
|
|
|
|
@EnableSwagger
|
2018-12-24 12:47:07 +08:00
|
|
|
|
@EnableConfigurationProperties(SwaggerProperties.class)
|
2019-12-22 23:43:17 +08:00
|
|
|
|
@Import(BeanValidatorPluginsConfiguration.class)
|
2021-01-15 20:37:59 +08:00
|
|
|
|
@AllArgsConstructor
|
2018-12-24 11:58:45 +08:00
|
|
|
|
public class SwaggerAutoConfiguration {
|
|
|
|
|
|
2020-11-06 17:12:53 +08:00
|
|
|
|
private static final String DEFAULT_BASE_PATH = "/**";
|
|
|
|
|
private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**");
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
2021-01-15 20:37:59 +08:00
|
|
|
|
/**
|
|
|
|
|
* 引入Knife4j扩展类
|
|
|
|
|
*/
|
|
|
|
|
private final OpenApiExtensionResolver openApiExtensionResolver;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 引入Blade环境变量
|
|
|
|
|
*/
|
|
|
|
|
private final BladeProperties bladeProperties;
|
|
|
|
|
|
2018-12-27 13:29:11 +08:00
|
|
|
|
@Bean
|
|
|
|
|
@ConditionalOnMissingBean
|
|
|
|
|
public SwaggerProperties swaggerProperties() {
|
|
|
|
|
return new SwaggerProperties();
|
|
|
|
|
}
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
2018-12-27 13:29:11 +08:00
|
|
|
|
@Bean
|
|
|
|
|
public Docket api(SwaggerProperties swaggerProperties) {
|
|
|
|
|
// base-path处理
|
|
|
|
|
if (swaggerProperties.getBasePath().size() == 0) {
|
2020-11-06 17:12:53 +08:00
|
|
|
|
swaggerProperties.getBasePath().add(DEFAULT_BASE_PATH);
|
2018-12-27 13:29:11 +08:00
|
|
|
|
}
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
2018-12-27 13:29:11 +08:00
|
|
|
|
// exclude-path处理
|
|
|
|
|
if (swaggerProperties.getExcludePath().size() == 0) {
|
2020-11-06 17:12:53 +08:00
|
|
|
|
swaggerProperties.getExcludePath().addAll(DEFAULT_EXCLUDE_PATH);
|
2018-12-27 13:29:11 +08:00
|
|
|
|
}
|
2020-11-06 17:12:53 +08:00
|
|
|
|
ApiSelectorBuilder apis = new Docket(DocumentationType.SWAGGER_2)
|
2018-12-27 13:29:11 +08:00
|
|
|
|
.host(swaggerProperties.getHost())
|
|
|
|
|
.apiInfo(apiInfo(swaggerProperties)).select()
|
2020-11-06 17:12:53 +08:00
|
|
|
|
.apis(SwaggerUtil.basePackages(swaggerProperties.getBasePackages()));
|
|
|
|
|
|
|
|
|
|
swaggerProperties.getBasePath().forEach(p -> apis.paths(PathSelectors.ant(p)));
|
|
|
|
|
swaggerProperties.getExcludePath().forEach(p -> apis.paths(PathSelectors.ant(p).negate()));
|
|
|
|
|
|
|
|
|
|
return apis.build()
|
2021-01-15 20:37:59 +08:00
|
|
|
|
.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()));
|
2018-12-27 13:29:11 +08:00
|
|
|
|
}
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
2018-12-27 13:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* 配置默认的全局鉴权策略的开关,通过正则表达式进行匹配;默认匹配所有URL
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2021-01-15 20:37:59 +08:00
|
|
|
|
private SecurityContext securityContext(SwaggerProperties swaggerProperties) {
|
2018-12-27 13:29:11 +08:00
|
|
|
|
return SecurityContext.builder()
|
2021-01-15 20:37:59 +08:00
|
|
|
|
.securityReferences(defaultAuth(swaggerProperties))
|
|
|
|
|
.forPaths(PathSelectors.regex(swaggerProperties.getAuthorization().getAuthRegex()))
|
2018-12-27 13:29:11 +08:00
|
|
|
|
.build();
|
|
|
|
|
}
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
2018-12-27 13:29:11 +08:00
|
|
|
|
/**
|
|
|
|
|
* 默认的全局鉴权策略
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2021-01-15 20:37:59 +08:00
|
|
|
|
private List<SecurityReference> defaultAuth(SwaggerProperties swaggerProperties) {
|
2018-12-27 13:29:11 +08:00
|
|
|
|
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
2021-01-15 20:37:59 +08:00
|
|
|
|
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
|
2018-12-27 13:29:11 +08:00
|
|
|
|
AuthorizationScope[] authorizationScopes = new AuthorizationScope[authorizationScopeList.size()];
|
|
|
|
|
return Collections.singletonList(SecurityReference.builder()
|
2021-01-15 20:37:59 +08:00
|
|
|
|
.reference(swaggerProperties.getAuthorization().getName())
|
2018-12-27 13:29:11 +08:00
|
|
|
|
.scopes(authorizationScopeList.toArray(authorizationScopes))
|
|
|
|
|
.build());
|
|
|
|
|
}
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
|
|
|
|
|
2021-01-15 20:37:59 +08:00
|
|
|
|
private OAuth securitySchema(SwaggerProperties swaggerProperties) {
|
2018-12-27 13:29:11 +08:00
|
|
|
|
ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
2021-01-15 20:37:59 +08:00
|
|
|
|
swaggerProperties.getAuthorization().getAuthorizationScopeList().forEach(authorizationScope -> authorizationScopeList.add(new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
|
2018-12-27 13:29:11 +08:00
|
|
|
|
ArrayList<GrantType> grantTypes = new ArrayList<>();
|
2021-01-15 20:37:59 +08:00
|
|
|
|
swaggerProperties.getAuthorization().getTokenUrlList().forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl)));
|
|
|
|
|
return new OAuth(swaggerProperties.getAuthorization().getName(), authorizationScopeList, grantTypes);
|
2018-12-27 13:29:11 +08:00
|
|
|
|
}
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
2018-12-27 13:29:11 +08:00
|
|
|
|
private ApiInfo apiInfo(SwaggerProperties swaggerProperties) {
|
|
|
|
|
return new ApiInfoBuilder()
|
|
|
|
|
.title(swaggerProperties.getTitle())
|
|
|
|
|
.description(swaggerProperties.getDescription())
|
|
|
|
|
.license(swaggerProperties.getLicense())
|
|
|
|
|
.licenseUrl(swaggerProperties.getLicenseUrl())
|
|
|
|
|
.termsOfServiceUrl(swaggerProperties.getTermsOfServiceUrl())
|
|
|
|
|
.contact(new Contact(swaggerProperties.getContact().getName(), swaggerProperties.getContact().getUrl(), swaggerProperties.getContact().getEmail()))
|
|
|
|
|
.version(swaggerProperties.getVersion())
|
|
|
|
|
.build();
|
|
|
|
|
}
|
2018-12-24 11:58:45 +08:00
|
|
|
|
|
|
|
|
|
}
|