|
@@ -7,8 +7,13 @@ import com.ruoyi.common.utils.StringUtils;
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
|
import com.ruoyi.framework.config.properties.SwaggerProperties;
|
|
import com.ruoyi.framework.config.properties.SwaggerProperties;
|
|
import io.swagger.models.auth.In;
|
|
import io.swagger.models.auth.In;
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
+import org.springframework.beans.BeansException;
|
|
|
|
+import org.springframework.beans.factory.config.BeanPostProcessor;
|
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
+import org.springframework.util.ReflectionUtils;
|
|
|
|
+import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
|
|
import springfox.documentation.builders.ApiInfoBuilder;
|
|
import springfox.documentation.builders.ApiInfoBuilder;
|
|
import springfox.documentation.builders.PathSelectors;
|
|
import springfox.documentation.builders.PathSelectors;
|
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
|
import springfox.documentation.builders.RequestHandlerSelectors;
|
|
@@ -16,8 +21,11 @@ import springfox.documentation.service.*;
|
|
import springfox.documentation.spi.DocumentationType;
|
|
import springfox.documentation.spi.DocumentationType;
|
|
import springfox.documentation.spi.service.contexts.SecurityContext;
|
|
import springfox.documentation.spi.service.contexts.SecurityContext;
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
import springfox.documentation.spring.web.plugins.Docket;
|
|
|
|
+import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider;
|
|
|
|
+import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
import javax.annotation.PostConstruct;
|
|
|
|
+import java.lang.reflect.Field;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
|
@@ -26,18 +34,45 @@ import java.util.List;
|
|
*
|
|
*
|
|
* @author Lion Li
|
|
* @author Lion Li
|
|
*/
|
|
*/
|
|
|
|
+@RequiredArgsConstructor
|
|
@Configuration
|
|
@Configuration
|
|
@EnableKnife4j
|
|
@EnableKnife4j
|
|
public class SwaggerConfig {
|
|
public class SwaggerConfig {
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private SwaggerProperties swaggerProperties;
|
|
|
|
|
|
+ private final SwaggerProperties swaggerProperties;
|
|
|
|
+ private final TokenProperties tokenProperties;
|
|
|
|
+ private final OpenApiExtensionResolver openApiExtensionResolver;
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private TokenProperties tokenProperties;
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 用于适配springboot 2.6
|
|
|
|
+ */
|
|
|
|
+ @Bean
|
|
|
|
+ @SuppressWarnings("all")
|
|
|
|
+ public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
|
|
- @Autowired
|
|
|
|
- private OpenApiExtensionResolver openApiExtensionResolver;
|
|
|
|
|
|
+ private <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
|
|
|
|
+ mappings.removeIf(mapping -> mapping.getPatternParser() != null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 创建API
|
|
* 创建API
|
|
@@ -97,7 +132,7 @@ public class SwaggerConfig {
|
|
* 默认的安全上引用
|
|
* 默认的安全上引用
|
|
*/
|
|
*/
|
|
private List<SecurityReference> defaultAuth() {
|
|
private List<SecurityReference> defaultAuth() {
|
|
- AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
|
|
|
|
|
|
+ AuthorizationScope authorizationScope = new AuthorizationScope("global" , "accessEverything");
|
|
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
|
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
|
|
authorizationScopes[0] = authorizationScope;
|
|
authorizationScopes[0] = authorizationScope;
|
|
List<SecurityReference> securityReferences = new ArrayList<>();
|
|
List<SecurityReference> securityReferences = new ArrayList<>();
|