🎨 swagger配置文件调整
This commit is contained in:
@@ -40,6 +40,11 @@
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
|
||||
import com.hccake.ballcat.common.swagger.property.SwaggerAggregatorProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -24,8 +26,8 @@ public class SwaggerAggregatorAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SwaggerProviderProperties swaggerProviderProperties() {
|
||||
return new SwaggerProviderProperties();
|
||||
public SwaggerAggregatorProperties swaggerAggregatorProperties() {
|
||||
return new SwaggerAggregatorProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,15 +38,16 @@ public class SwaggerAggregatorAutoConfiguration {
|
||||
*/
|
||||
@Primary
|
||||
@Bean
|
||||
@ConditionalOnBean(SwaggerAggregatorProperties.class)
|
||||
public SwaggerResourcesProvider swaggerResourcesProvider(
|
||||
InMemorySwaggerResourcesProvider defaultResourcesProvider,
|
||||
SwaggerProviderProperties swaggerProviderProperties) {
|
||||
SwaggerAggregatorProperties swaggerAggregatorProperties) {
|
||||
|
||||
return () -> {
|
||||
// 聚合者自己的 Resources
|
||||
List<SwaggerResource> resources = new ArrayList<>(defaultResourcesProvider.get());
|
||||
// 提供者的 Resources
|
||||
List<SwaggerResource> providerResources = swaggerProviderProperties.getResources();
|
||||
List<SwaggerResource> providerResources = swaggerAggregatorProperties.getProviderResources();
|
||||
if (!CollectionUtils.isEmpty(providerResources)){
|
||||
resources.addAll(providerResources);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
|
||||
import com.hccake.ballcat.common.swagger.property.SwaggerProperties;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import springfox.documentation.builders.ApiInfoBuilder;
|
||||
@@ -27,7 +27,6 @@ import java.util.List;
|
||||
public class SwaggerConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("swagger")
|
||||
public SwaggerProperties swaggerProperties() {
|
||||
return new SwaggerProperties();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import com.hccake.ballcat.common.swagger.property.SwaggerProviderProperties;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
@@ -19,8 +20,11 @@ import org.springframework.web.filter.CorsFilter;
|
||||
@ConditionalOnProperty(name = "swagger.enabled", havingValue = "true", matchIfMissing = true)
|
||||
public class SwaggerProviderAutoConfiguration {
|
||||
|
||||
@Value("${swagger.aggregator.origin:*}")
|
||||
private String aggregatorOrigin;
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public SwaggerProviderProperties swaggerProviderProperties() {
|
||||
return new SwaggerProviderProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* 允许swagger文档跨域访问
|
||||
@@ -28,14 +32,14 @@ public class SwaggerProviderAutoConfiguration {
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public FilterRegistrationBean corsFilter() {
|
||||
@ConditionalOnBean(SwaggerProviderProperties.class)
|
||||
public FilterRegistrationBean corsFilter(SwaggerProviderProperties swaggerProviderProperties) {
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
CorsConfiguration config = new CorsConfiguration();
|
||||
config.setAllowCredentials(true);
|
||||
config.addAllowedHeader("*");
|
||||
config.addAllowedMethod("*");
|
||||
config.addAllowedOrigin(aggregatorOrigin);
|
||||
config.addAllowedOrigin(swaggerProviderProperties.getAggregatorOrigin());
|
||||
source.registerCorsConfiguration("/v2/api-docs**", config);
|
||||
FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
|
||||
bean.setOrder(0);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
package com.hccake.ballcat.common.swagger.constant;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -1,8 +1,7 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
package com.hccake.ballcat.common.swagger.property;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
import springfox.documentation.swagger.web.SwaggerResource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -14,12 +13,11 @@ import java.util.List;
|
||||
* @date 2019/11/1 20:05
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties("swagger.provider")
|
||||
public class SwaggerProviderProperties {
|
||||
@ConfigurationProperties("swagger.aggregator")
|
||||
public class SwaggerAggregatorProperties {
|
||||
|
||||
/**
|
||||
* 聚合文档源信息
|
||||
*/
|
||||
private List<SwaggerResource> resources = new ArrayList<>();
|
||||
private List<SwaggerResource> providerResources = new ArrayList<>();
|
||||
}
|
||||
@@ -1,7 +1,8 @@
|
||||
package com.hccake.ballcat.common.swagger;
|
||||
package com.hccake.ballcat.common.swagger.property;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -12,6 +13,7 @@ import java.util.List;
|
||||
* @date 2019/11/1 19:37
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties("swagger")
|
||||
public class SwaggerProperties {
|
||||
/**
|
||||
* 是否开启swagger
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.hccake.ballcat.common.swagger.property;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* @author Hccake
|
||||
* @version 1.0
|
||||
* @date 2019/11/1 20:05
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties("swagger.provider")
|
||||
public class SwaggerProviderProperties {
|
||||
|
||||
/**
|
||||
* 聚合者的来源,用于控制跨域放行
|
||||
*/
|
||||
private String aggregatorOrigin = "*";
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user