Swagger改用Starter

This commit is contained in:
macro
2022-06-25 15:40:33 +08:00
parent 4fc1755441
commit 2f7b30b1df
9 changed files with 93 additions and 17 deletions

View File

@@ -2,6 +2,8 @@ package com.macro.mall.config;
import com.macro.mall.common.config.BaseSwaggerConfig;
import com.macro.mall.common.domain.SwaggerProperties;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@@ -24,4 +26,10 @@ public class SwaggerConfig extends BaseSwaggerConfig {
.enableSecurity(true)
.build();
}
@Bean
public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return generateBeanPostProcessor();
}
}

View File

@@ -7,6 +7,9 @@ spring:
multipart:
enabled: true #开启文件上传
max-file-size: 10MB #限制文件上传大小为10M
mvc:
pathmatch:
matching-strategy: ant_path_matcher
mybatis:
mapper-locations:
@@ -30,15 +33,15 @@ redis:
secure:
ignored:
urls: #安全路径白名单
- /swagger-ui.html
- /swagger-ui/
- /swagger-resources/**
- /swagger/**
- /**/v2/api-docs
- /**/*.html
- /**/*.js
- /**/*.css
- /**/*.png
- /**/*.ico
- /webjars/springfox-swagger-ui/**
- /**/*.map
- /favicon.ico
- /actuator/**
- /druid/**
- /admin/login
@@ -59,9 +62,3 @@ aliyun:
callback: http://39.98.190.128:8080/aliyun/oss/callback # 文件上传成功后的回调地址
dir:
prefix: mall/images/ # 上传文件夹路径前缀
minio:
endpoint: http://192.168.3.101:9090 #MinIO服务所在地址
bucketName: mall #存储桶名称
accessKey: minioadmin #访问的key
secretKey: minioadmin #访问的秘钥

View File

@@ -1,7 +1,11 @@
package com.macro.mall.common.config;
import com.macro.mall.common.domain.SwaggerProperties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
@@ -9,9 +13,13 @@ 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.spring.web.plugins.WebFluxRequestHandlerProvider;
import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* Swagger基础配置
@@ -43,9 +51,9 @@ public abstract class BaseSwaggerConfig {
.build();
}
private List<ApiKey> securitySchemes() {
private List<SecurityScheme> securitySchemes() {
//设置请求头信息
List<ApiKey> result = new ArrayList<>();
List<SecurityScheme> result = new ArrayList<>();
ApiKey apiKey = new ApiKey("Authorization", "Authorization", "header");
result.add(apiKey);
return result;
@@ -74,6 +82,38 @@ public abstract class BaseSwaggerConfig {
return result;
}
public BeanPostProcessor generateBeanPostProcessor(){
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) {
customizeSpringfoxHandlerMappings(getHandlerMappings(bean));
}
return bean;
}
private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream()
.filter(mapping -> mapping.getPatternParser() == null)
.collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}
@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
};
}
/**
* 自定义Swagger配置
*/

View File

@@ -2,6 +2,8 @@ package com.macro.mall.demo.config;
import com.macro.mall.common.config.BaseSwaggerConfig;
import com.macro.mall.common.domain.SwaggerProperties;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@@ -25,4 +27,9 @@ public class SwaggerConfig extends BaseSwaggerConfig {
.build();
}
@Bean
public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return generateBeanPostProcessor();
}
}

View File

@@ -5,7 +5,7 @@ spring:
application:
name: mall-demo
datasource:
url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
url: jdbc:mysql://localhost:3306/mall?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false
username: root
password: root
thymeleaf:
@@ -14,6 +14,10 @@ spring:
servlet:
content-type: text/html
cache: false #开发时关闭缓存,不然没法看到实时页面
mvc:
pathmatch:
matching-strategy: ant_path_matcher
mybatis:
mapper-locations:
- classpath:mapper/*.xml

View File

@@ -2,6 +2,8 @@ package com.macro.mall.portal.config;
import com.macro.mall.common.config.BaseSwaggerConfig;
import com.macro.mall.common.domain.SwaggerProperties;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@@ -24,4 +26,9 @@ public class SwaggerConfig extends BaseSwaggerConfig {
.enableSecurity(true)
.build();
}
@Bean
public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return generateBeanPostProcessor();
}
}

View File

@@ -3,6 +3,9 @@ spring:
name: mall-portal
profiles:
active: dev #默认为开发环境
mvc:
pathmatch:
matching-strategy: ant_path_matcher
mybatis:
mapper-locations:
@@ -18,15 +21,15 @@ jwt:
secure:
ignored:
urls: #安全路径白名单
- /swagger-ui.html
- /swagger-ui/
- /swagger-resources/**
- /swagger/**
- /**/v2/api-docs
- /**/*.html
- /**/*.js
- /**/*.css
- /**/*.png
- /**/*.ico
- /webjars/springfox-swagger-ui/**
- /**/*.map
- /favicon.ico
- /druid/**
- /actuator/**
- /sso/**

View File

@@ -2,6 +2,8 @@ package com.macro.mall.search.config;
import com.macro.mall.common.config.BaseSwaggerConfig;
import com.macro.mall.common.domain.SwaggerProperties;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@@ -24,4 +26,9 @@ public class SwaggerConfig extends BaseSwaggerConfig {
.enableSecurity(false)
.build();
}
@Bean
public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
return generateBeanPostProcessor();
}
}

View File

@@ -3,6 +3,9 @@ spring:
name: mall-search
profiles:
active: dev #默认为开发环境
mvc:
pathmatch:
matching-strategy: ant_path_matcher
server:
port: 8081