✨ 同步 ballcat swagger 的开关配置至 springfox,这样只需配置 ballcat.swagger.enabled=false 即可完全关闭 swagger
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
|
||||
import com.hccake.ballcat.common.swagger.property.SwaggerAggregatorProperties;
|
||||
import com.hccake.ballcat.common.swagger.property.SwaggerProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -21,7 +22,7 @@ import java.util.List;
|
||||
* @date 2019/11/1 20:03
|
||||
*/
|
||||
@Import(SwaggerConfiguration.class)
|
||||
@ConditionalOnProperty(name = "ballcat.swagger.enabled", havingValue = "true", matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = SwaggerProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class SwaggerAggregatorAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@@ -32,8 +33,8 @@ public class SwaggerAggregatorAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 聚合文档
|
||||
* @param defaultResourcesProvider
|
||||
* @return
|
||||
* @param defaultResourcesProvider 本地内存的资源提供者
|
||||
* @return SwaggerResourcesProvider
|
||||
*/
|
||||
@Primary
|
||||
@Bean
|
||||
|
||||
@@ -8,8 +8,6 @@ import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
@@ -19,7 +17,6 @@ import springfox.documentation.spring.web.plugins.Docket;
|
||||
* @date 2019/11/1 19:43
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@EnableConfigurationProperties(SwaggerProperties.class)
|
||||
public class SwaggerConfiguration {
|
||||
|
||||
@@ -36,7 +33,8 @@ public class SwaggerConfiguration {
|
||||
Docket docket = new Docket(swaggerProperties.getDocumentationType().getType())
|
||||
.host(swaggerProperties.getHost())
|
||||
.apiInfo(helper.apiInfo())
|
||||
.groupName(swaggerProperties.getGroupName());
|
||||
.groupName(swaggerProperties.getGroupName())
|
||||
.enable(swaggerProperties.getEnabled());
|
||||
|
||||
// 2. 安全配置
|
||||
docket.securitySchemes(helper.securitySchema())
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
|
||||
import com.hccake.ballcat.common.swagger.property.SwaggerProperties;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.env.EnvironmentPostProcessor;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
import org.springframework.core.env.MapPropertySource;
|
||||
import org.springframework.core.env.MutablePropertySources;
|
||||
import org.springframework.core.env.PropertySource;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 将 ballcat swagger 的开关配置同步至 springfox
|
||||
*
|
||||
* @author hccake
|
||||
*/
|
||||
public class SwaggerEnabledStatusReplaceEnvironmentPostProcessor implements EnvironmentPostProcessor {
|
||||
|
||||
/**
|
||||
* 资源名称
|
||||
*/
|
||||
private static final String REPLACE_SOURCE_NAME = "replaceEnvironment";
|
||||
|
||||
private static final String SPRINGFOX_SWAGGER_ENABLED_KEY = "springfox.documentation.enabled";
|
||||
|
||||
private static final String BALLCAT_SWAGGER_ENABLED_KEY = SwaggerProperties.PREFIX + "enabled";
|
||||
|
||||
@Override
|
||||
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
|
||||
// 如果已经独立配置了 springfox 的开关信息,则不处理
|
||||
String springFoxSwaggerEnabledValue = environment.getProperty(SPRINGFOX_SWAGGER_ENABLED_KEY);
|
||||
if (StringUtils.hasText(springFoxSwaggerEnabledValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 获取 ballcat 的 swagger 开关状态
|
||||
boolean ballcatEnabledSwagger = true;
|
||||
String ballcatSwaggerEnabledValue = environment.getProperty(BALLCAT_SWAGGER_ENABLED_KEY);
|
||||
if (StringUtils.hasText(ballcatSwaggerEnabledValue)) {
|
||||
ballcatEnabledSwagger = "true".equalsIgnoreCase(ballcatSwaggerEnabledValue);
|
||||
}
|
||||
|
||||
// 将 ballcat swagger 的开关状态同步至 springfox
|
||||
Map<String, Object> map = new HashMap<>(1);
|
||||
map.put(SPRINGFOX_SWAGGER_ENABLED_KEY, ballcatEnabledSwagger);
|
||||
replace(environment.getPropertySources(), map);
|
||||
}
|
||||
|
||||
private void replace(MutablePropertySources propertySources, Map<String, Object> map) {
|
||||
MapPropertySource target = null;
|
||||
if (propertySources.contains(REPLACE_SOURCE_NAME)) {
|
||||
PropertySource<?> source = propertySources.get(REPLACE_SOURCE_NAME);
|
||||
if (source instanceof MapPropertySource) {
|
||||
target = (MapPropertySource) source;
|
||||
target.getSource().putAll(map);
|
||||
}
|
||||
}
|
||||
if (target == null) {
|
||||
target = new MapPropertySource(REPLACE_SOURCE_NAME, map);
|
||||
}
|
||||
if (!propertySources.contains(REPLACE_SOURCE_NAME)) {
|
||||
propertySources.addFirst(target);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
|
||||
import com.hccake.ballcat.common.swagger.property.SwaggerProperties;
|
||||
import com.hccake.ballcat.common.swagger.property.SwaggerProviderProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
@@ -17,7 +18,7 @@ import org.springframework.web.filter.CorsFilter;
|
||||
* @date 2019/11/1 20:03
|
||||
*/
|
||||
@Import(SwaggerConfiguration.class)
|
||||
@ConditionalOnProperty(name = "ballcat.swagger.enabled", havingValue = "true", matchIfMissing = true)
|
||||
@ConditionalOnProperty(prefix = SwaggerProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class SwaggerProviderAutoConfiguration {
|
||||
|
||||
private static final String ALL = "*";
|
||||
|
||||
@@ -12,9 +12,11 @@ import java.util.List;
|
||||
* @date 2019/11/1 19:37
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties("ballcat.swagger")
|
||||
@ConfigurationProperties(SwaggerProperties.PREFIX)
|
||||
public class SwaggerProperties {
|
||||
|
||||
public static final String PREFIX = "ballcat.swagger";
|
||||
|
||||
/**
|
||||
* 是否开启swagger
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
# Environment Post Processor
|
||||
org.springframework.boot.env.EnvironmentPostProcessor=\
|
||||
com.hccake.ballcat.common.swagger.SwaggerEnabledStatusReplaceEnvironmentPostProcessor
|
||||
Reference in New Issue
Block a user