org.zodiac.autoconfigure.knife4j.ReactiveKnife4jAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.knife4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.env.Environment;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.zodiac.sdk.toolkit.constants.StringPool;
@SpringBootConfiguration
@ConditionalOnWebApplication(type = Type.REACTIVE)
@ComponentScan(basePackages = {"com.github.xiaoymin.knife4j.spring.plugin"})
@ConditionalOnProperty(name = "knife4j.enable", havingValue = "true")
@EnableConfigurationProperties(value = {com.github.xiaoymin.knife4j.spring.configuration.Knife4jProperties.class})
@ConditionalOnClass(value = {com.github.xiaoymin.knife4j.core.extend.OpenApiExtendSetting.class,
com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver.class, org.zodiac.knife4j.spring.reactive.ReactiveSecurityBasicAuthFilter.class})
public class ReactiveKnife4jAutoConfiguration implements WebFluxConfigurer {
final Logger logger = LoggerFactory.getLogger(ReactiveKnife4jAutoConfiguration.class);
private Environment environment;
public ReactiveKnife4jAutoConfiguration(Environment environment) {
this.environment = environment;
}
@Bean(value = "knife4jCorsFilter")
@ConditionalOnMissingBean(value = CorsWebFilter.class)
@ConditionalOnProperty(name = "knife4j.cors", havingValue = "true")
protected CorsWebFilter corsFilter() {
logger.info("init CorsFilter...");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setAllowCredentials(true);
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("*");
corsConfiguration.addAllowedMethod("*");
corsConfiguration.setMaxAge(10000L);
/*匹配所有API*/
source.registerCorsConfiguration("/**", corsConfiguration);
CorsWebFilter corsFilter = new CorsWebFilter(source);
return corsFilter;
}
@Bean(initMethod = "start")
@ConditionalOnProperty(name = "knife4j.enable", havingValue = "true")
@ConditionalOnMissingBean(value = com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver.class)
protected com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver
markdownResolver(com.github.xiaoymin.knife4j.spring.configuration.Knife4jProperties knife4jProperties) {
com.github.xiaoymin.knife4j.core.extend.OpenApiExtendSetting setting = knife4jProperties.getSetting();
if (setting == null) {
setting = new com.github.xiaoymin.knife4j.core.extend.OpenApiExtendSetting();
}
return new com.github.xiaoymin.knife4j.spring.extension.OpenApiExtensionResolver(setting,
knife4jProperties.getDocuments());
}
@Bean
@ConditionalOnMissingBean(value = org.zodiac.knife4j.spring.reactive.ReactiveSecurityBasicAuthFilter.class)
@ConditionalOnProperty(name = "knife4j.basic.enable", havingValue = "true")
protected org.zodiac.knife4j.spring.reactive.ReactiveSecurityBasicAuthFilter
securityBasicAuthFilter(com.github.xiaoymin.knife4j.spring.configuration.Knife4jProperties knife4jProperties) {
boolean enableSwaggerBasicAuth = false;
String dftUserName = "admin", dftPass = "123321";
org.zodiac.knife4j.spring.reactive.ReactiveSecurityBasicAuthFilter securityBasicAuthFilter = null;
if (knife4jProperties == null) {
if (environment != null) {
String enableAuth = environment.getProperty("knife4j.basic.enable");
enableSwaggerBasicAuth = Boolean.valueOf(enableAuth);
if (enableSwaggerBasicAuth) {
/*如果开启basic验证,从配置文件中获取用户名和密码*/
String pUser = environment.getProperty("knife4j.basic.username");
String pPass = environment.getProperty("knife4j.basic.password");
if (pUser != null && !StringPool.EMPTY.equals(pUser)) {
dftUserName = pUser;
}
if (pPass != null && !StringPool.EMPTY.equals(pPass)) {
dftPass = pPass;
}
}
securityBasicAuthFilter = new org.zodiac.knife4j.spring.reactive.ReactiveSecurityBasicAuthFilter(
enableSwaggerBasicAuth, dftUserName, dftPass);
}
} else {
/*判断非空*/
if (knife4jProperties.getBasic() == null) {
securityBasicAuthFilter = new org.zodiac.knife4j.spring.reactive.ReactiveSecurityBasicAuthFilter(
enableSwaggerBasicAuth, dftUserName, dftPass);
} else {
securityBasicAuthFilter = new org.zodiac.knife4j.spring.reactive.ReactiveSecurityBasicAuthFilter(
knife4jProperties.getBasic().isEnable(), knife4jProperties.getBasic().getUsername(),
knife4jProperties.getBasic().getPassword());
}
}
return securityBasicAuthFilter;
}
@Bean
@ConditionalOnMissingBean(value = org.zodiac.knife4j.spring.reactive.ReactiveProductionSecurityFilter.class)
@ConditionalOnProperty(name = "knife4j.production", havingValue = "true")
protected org.zodiac.knife4j.spring.reactive.ReactiveProductionSecurityFilter
productionSecurityFilter(com.github.xiaoymin.knife4j.spring.configuration.Knife4jProperties knife4jProperties) {
boolean prod = false;
org.zodiac.knife4j.spring.reactive.ReactiveProductionSecurityFilter p = null;
if (knife4jProperties == null) {
if (environment != null) {
String prodStr = environment.getProperty("knife4j.production");
if (logger.isDebugEnabled()) {
logger.debug("swagger.production:{}", prodStr);
}
prod = Boolean.valueOf(prodStr);
}
p = new org.zodiac.knife4j.spring.reactive.ReactiveProductionSecurityFilter(prod);
} else {
p = new org.zodiac.knife4j.spring.reactive.ReactiveProductionSecurityFilter(
knife4jProperties.isProduction());
}
return p;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/*配置knife4j 显示文档 */
//registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("api.html").addResourceLocations("classpath:/META-INF/resources/");
/** 公共部分内容 */
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}