|
@@ -1,143 +0,0 @@
|
|
|
-/*
|
|
|
|
|
- * Copyright (c) 2018-2025, hnqz All rights reserved.
|
|
|
|
|
- *
|
|
|
|
|
- * Redistribution and use in source and binary forms, with or without
|
|
|
|
|
- * modification, are permitted provided that the following conditions are met:
|
|
|
|
|
- *
|
|
|
|
|
- * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
- * this list of conditions and the following disclaimer.
|
|
|
|
|
- * Redistributions in binary form must reproduce the above copyright
|
|
|
|
|
- * notice, this list of conditions and the following disclaimer in the
|
|
|
|
|
- * documentation and/or other materials provided with the distribution.
|
|
|
|
|
- * Neither the name of the pig4cloud.com developer nor the names of its
|
|
|
|
|
- * contributors may be used to endorse or promote products derived from
|
|
|
|
|
- * this software without specific prior written permission.
|
|
|
|
|
- * Author: hnqz
|
|
|
|
|
- */
|
|
|
|
|
-package net.yyc.common.swagger.config;
|
|
|
|
|
-
|
|
|
|
|
-import com.google.common.base.Predicate;
|
|
|
|
|
-import com.google.common.base.Predicates;
|
|
|
|
|
-import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
|
|
|
|
-import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
|
|
|
-import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
|
-import org.springframework.context.annotation.Bean;
|
|
|
|
|
-import org.springframework.context.annotation.Configuration;
|
|
|
|
|
-import springfox.documentation.builders.ApiInfoBuilder;
|
|
|
|
|
-import springfox.documentation.builders.ParameterBuilder;
|
|
|
|
|
-import springfox.documentation.builders.PathSelectors;
|
|
|
|
|
-import springfox.documentation.builders.RequestHandlerSelectors;
|
|
|
|
|
-import springfox.documentation.schema.ModelRef;
|
|
|
|
|
-import springfox.documentation.service.*;
|
|
|
|
|
-import springfox.documentation.spi.DocumentationType;
|
|
|
|
|
-import springfox.documentation.spi.service.contexts.SecurityContext;
|
|
|
|
|
-import springfox.documentation.spring.web.plugins.Docket;
|
|
|
|
|
-import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
|
|
|
|
-
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.Arrays;
|
|
|
|
|
-import java.util.Collections;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-
|
|
|
|
|
-/**
|
|
|
|
|
- * @author hnqz swagger配置 禁用方法1:使用注解@Profile({"dev","test"})
|
|
|
|
|
- * 表示在开发或测试环境开启,而在生产关闭。(推荐使用) 禁用方法2:使用注解@ConditionalOnProperty(name = "swagger.enable",
|
|
|
|
|
- * havingValue = "true") 然后在测试配置或者开发配置中添加swagger.enable=true即可开启,生产环境不填则默认关闭Swagger.
|
|
|
|
|
- */
|
|
|
|
|
-@Configuration
|
|
|
|
|
-@EnableSwagger2
|
|
|
|
|
-@EnableAutoConfiguration
|
|
|
|
|
-@ConditionalOnProperty(name = "swagger.enabled", matchIfMissing = true)
|
|
|
|
|
-public class SwaggerAutoConfiguration {
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 默认的排除路径,排除Spring Boot默认的错误处理路径和端点
|
|
|
|
|
- */
|
|
|
|
|
- private static final List<String> DEFAULT_EXCLUDE_PATH = Arrays.asList("/error", "/actuator/**");
|
|
|
|
|
-
|
|
|
|
|
- private static final String BASE_PATH = "/**";
|
|
|
|
|
-
|
|
|
|
|
- @Bean
|
|
|
|
|
- @ConditionalOnMissingBean
|
|
|
|
|
- public SwaggerProperties swaggerProperties() {
|
|
|
|
|
- return new SwaggerProperties();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Bean
|
|
|
|
|
- public Docket api(SwaggerProperties swaggerProperties) {
|
|
|
|
|
- // base-path处理
|
|
|
|
|
- if (swaggerProperties.getBasePath().isEmpty()) {
|
|
|
|
|
- swaggerProperties.getBasePath().add(BASE_PATH);
|
|
|
|
|
- }
|
|
|
|
|
- // noinspection unchecked
|
|
|
|
|
- List<Predicate<String>> basePath = new ArrayList();
|
|
|
|
|
- swaggerProperties.getBasePath().forEach(path -> basePath.add(PathSelectors.ant(path)));
|
|
|
|
|
-
|
|
|
|
|
- // exclude-path处理
|
|
|
|
|
- if (swaggerProperties.getExcludePath().isEmpty()) {
|
|
|
|
|
- swaggerProperties.getExcludePath().addAll(DEFAULT_EXCLUDE_PATH);
|
|
|
|
|
- }
|
|
|
|
|
- List<Predicate<String>> excludePath = new ArrayList<>();
|
|
|
|
|
- swaggerProperties.getExcludePath().forEach(path -> excludePath.add(PathSelectors.ant(path)));
|
|
|
|
|
-
|
|
|
|
|
- // 版本请求头处理
|
|
|
|
|
- ParameterBuilder versionPar = new ParameterBuilder();
|
|
|
|
|
- List<Parameter> pars = new ArrayList<>();
|
|
|
|
|
- versionPar.name("VERSION").description("灰度路由版本信息").modelRef(new ModelRef("string")).parameterType("header")
|
|
|
|
|
- .required(false).build();
|
|
|
|
|
- pars.add(versionPar.build());
|
|
|
|
|
-
|
|
|
|
|
- // noinspection Guava
|
|
|
|
|
- return new Docket(DocumentationType.SWAGGER_2).host(swaggerProperties.getHost())
|
|
|
|
|
- .apiInfo(apiInfo(swaggerProperties)).globalOperationParameters(pars).select()
|
|
|
|
|
- .apis(RequestHandlerSelectors.basePackage(swaggerProperties.getBasePackage()))
|
|
|
|
|
- .paths(Predicates.and(Predicates.not(Predicates.or(excludePath)), Predicates.or(basePath))).build()
|
|
|
|
|
- .securitySchemes(Collections.singletonList(securitySchema()))
|
|
|
|
|
- .securityContexts(Collections.singletonList(securityContext())).pathMapping("/");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 配置默认的全局鉴权策略的开关,通过正则表达式进行匹配;默认匹配所有URL
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- private SecurityContext securityContext() {
|
|
|
|
|
- return SecurityContext.builder().securityReferences(defaultAuth())
|
|
|
|
|
- .forPaths(PathSelectors.regex(swaggerProperties().getAuthorization().getAuthRegex())).build();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 默认的全局鉴权策略
|
|
|
|
|
- * @return
|
|
|
|
|
- */
|
|
|
|
|
- private List<SecurityReference> defaultAuth() {
|
|
|
|
|
- ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
|
|
|
|
- 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())
|
|
|
|
|
- .scopes(authorizationScopeList.toArray(authorizationScopes)).build());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private OAuth securitySchema() {
|
|
|
|
|
- ArrayList<AuthorizationScope> authorizationScopeList = new ArrayList<>();
|
|
|
|
|
- swaggerProperties().getAuthorization().getAuthorizationScopeList()
|
|
|
|
|
- .forEach(authorizationScope -> authorizationScopeList.add(
|
|
|
|
|
- new AuthorizationScope(authorizationScope.getScope(), authorizationScope.getDescription())));
|
|
|
|
|
- ArrayList<GrantType> grantTypes = new ArrayList<>();
|
|
|
|
|
- swaggerProperties().getAuthorization().getTokenUrlList()
|
|
|
|
|
- .forEach(tokenUrl -> grantTypes.add(new ResourceOwnerPasswordCredentialsGrant(tokenUrl)));
|
|
|
|
|
- return new OAuth(swaggerProperties().getAuthorization().getName(), authorizationScopeList, grantTypes);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- 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();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|