瀏覽代碼

fix:
- 适配mybatis-plus新版本,修改分页器注入
- 适配springboot新版本
- 修改spring.resources.static-locations为spring.web.resources.static-locations
- 修改跨越配置allowedOrigins为allowedOriginPatterns
- 解决springfox与springboot新版本不兼容问题

hong.yang 1 年之前
父節點
當前提交
37ca36a871

+ 7 - 4
DataRoom/dataroom-server/src/main/java/com/gccloud/DataRoomApplication.java

@@ -1,6 +1,7 @@
 package com.gccloud;
 
-import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
+import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
 import com.gccloud.common.constant.CommonConst;
 import com.gccloud.dataroom.core.constant.DataRoomConst;
 import com.gccloud.dataset.constant.DatasetConstant;
@@ -28,10 +29,12 @@ public class DataRoomApplication {
     /**
      * 分页插件
      *
-     * @return
+     * @return PaginationInterceptor
      */
     @Bean
-    public PaginationInterceptor paginationInterceptor() {
-        return new PaginationInterceptor();
+    public MybatisPlusInterceptor mybatisPlusInterceptor() {
+        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
+        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
+        return interceptor;
     }
 }

+ 3 - 2
DataRoom/dataroom-server/src/main/java/com/gccloud/dataroom/config/CorsBeanConfig.java

@@ -22,7 +22,8 @@ public class CorsBeanConfig implements WebMvcConfigurer {
         Cors cors = new Cors();
         CorsRegistration corsRegistration = registry.addMapping(cors.getMapping());
         corsRegistration
-                .allowedOrigins(cors.getAllowedOrigins().toArray(new String[cors.getAllowedOrigins().size()]))
+                // change 由allowedOrigins改为allowedOriginPatterns,springboot新版本中allowedOrigins不允许设置*,只能设置具体的域名
+                .allowedOriginPatterns(cors.getAllowedOrigins().toArray(new String[cors.getAllowedOrigins().size()]))
                 .allowCredentials(cors.getAllowCredentials())
                 .allowedMethods(cors.getAllowedMethods().toArray(new String[cors.getAllowedMethods().size()]))
                 .maxAge(cors.getMaxAge());
@@ -34,4 +35,4 @@ public class CorsBeanConfig implements WebMvcConfigurer {
             corsRegistration.exposedHeaders(exposedHeaders.toArray(new String[exposedHeaders.size()]));
         }
     }
-}
+}

+ 45 - 0
DataRoom/dataroom-server/src/main/java/com/gccloud/dataroom/config/SwaggerBootstrapConfig.java

@@ -6,12 +6,17 @@ import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrap
 import com.google.common.base.Predicates;
 import com.google.common.collect.Lists;
 import lombok.extern.slf4j.Slf4j;
+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.lang.NonNull;
 import org.springframework.stereotype.Component;
+import org.springframework.util.ReflectionUtils;
 import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
 import springfox.documentation.builders.ApiInfoBuilder;
 import springfox.documentation.builders.PathSelectors;
 import springfox.documentation.builders.RequestHandlerSelectors;
@@ -20,9 +25,12 @@ import springfox.documentation.service.ApiInfo;
 import springfox.documentation.service.ResponseMessage;
 import springfox.documentation.spi.DocumentationType;
 import springfox.documentation.spring.web.plugins.Docket;
+import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider;
 import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
+import java.lang.reflect.Field;
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * Swagger2
@@ -81,4 +89,41 @@ public class SwaggerBootstrapConfig implements WebMvcConfigurer {
         registry.addResourceHandler("doc.html")
                 .addResourceLocations("classpath:/META-INF/resources/");
     }
+
+    // NOTE 解决springfox与springboot新版本不兼容问题
+    @Bean
+    public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() {
+        return new BeanPostProcessor() {
+
+            @Override
+            public Object postProcessAfterInitialization(@NonNull  Object bean, @NonNull String beanName) throws BeansException {
+                if (bean instanceof WebMvcRequestHandlerProvider ) {
+                    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");
+                    assert field != null;
+                    field.setAccessible(true);
+                    return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
+                } catch (IllegalArgumentException | IllegalAccessException e) {
+                    throw new IllegalStateException(e);
+                }
+            }
+        };
+    }
+
+
 }

+ 7 - 1
DataRoom/dataroom-server/src/main/resources/application.yml

@@ -17,6 +17,7 @@ spring:
       # 字符串转允许List,否则导致 @RequestBody List<T> list 类型无法解析
       ACCEPT_SINGLE_VALUE_AS_ARRAY: true
   resources:
+    # 自springboot 2.5.5之后,该属性已经被废弃,使用web.resources.static-locations代替
     static-locations: classpath:/static/,classpath:/META-INF/resources/,classpath:/META-INF/resources/webjars/,file:${gc.starter.file.basePath}
   # 静态资源配置
   mvc:
@@ -26,6 +27,11 @@ spring:
     view:
       prefix: classpath:/static/
       suffix: .html
+    pathmatch:
+      matching-strategy: ANT_PATH_MATCHER
+  web:
+    resources:
+      static-locations: classpath:/static/,classpath:/META-INF/resources/,classpath:/META-INF/resources/webjars/,file:${gc.starter.file.basePath}
 
 mybatis-plus:
   # mybatis plus xml配置文件扫描,多个通过分号隔开
@@ -35,4 +41,4 @@ mybatis-plus:
   global-config:
     db-config:
       #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"唯一ID";
-      id-type: 0
+      id-type: AUTO