温红权 3 år sedan
förälder
incheckning
71a3e03fa0

BIN
.gradle/buildOutputCleanup/buildOutputCleanup.lock


+ 31 - 0
maxkey-gateway/src/main/java/org/gateway/route/CorsConfig.java

@@ -0,0 +1,31 @@
+package org.gateway.route;
+
+import org.springframework.boot.SpringBootConfiguration;
+import org.springframework.web.servlet.config.annotation.CorsRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+/**
+ * @Author AnHui_XiaoYang
+ * @Email 939209948@qq.com
+ * @Date 2021/5/10 17:49
+ * @Description
+ */
+@SpringBootConfiguration
+public class CorsConfig implements WebMvcConfigurer {
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+        //添加映射路径
+        registry.addMapping("/**")
+                //是否发送Cookie
+                .allowCredentials(true)
+                //设置放行哪些原始域   SpringBoot2.4.4下低版本使用.allowedOrigins("*")
+                .allowedOriginPatterns("*")
+                //放行哪些请求方式
+                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
+                //.allowedMethods("*") //或者放行全部
+                //放行哪些原始请求头部信息
+                .allowedHeaders("*")
+                //暴露哪些原始请求头部信息
+                .exposedHeaders("*");
+    }
+}

+ 46 - 0
maxkey-gateway/src/main/java/org/gateway/route/WebGlobalConfig.java

@@ -0,0 +1,46 @@
+package org.gateway.route;
+
+import org.springframework.boot.SpringBootConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+
+/**
+ * @Author AnHui_XiaoYang
+ * @Email 939209948@qq.com
+ * @Date 2021/5/10 17:07
+ * @Description
+ */
+@SpringBootConfiguration
+public class WebGlobalConfig {
+
+    @Bean
+    public CorsFilter corsFilter() {
+
+        //创建CorsConfiguration对象后添加配置
+        CorsConfiguration config = new CorsConfiguration();
+        //设置放行哪些原始域
+        config.addAllowedOriginPattern("*");
+        //放行哪些原始请求头部信息
+        config.addAllowedHeader("*");
+        //暴露哪些头部信息
+        config.addExposedHeader("*");
+        //放行哪些请求方式
+        config.addAllowedMethod("GET");     //get
+        config.addAllowedMethod("PUT");     //put
+        config.addAllowedMethod("POST");    //post
+        config.addAllowedMethod("DELETE");  //delete
+        //corsConfig.addAllowedMethod("*");     //放行全部请求
+
+        //是否发送Cookie
+        config.setAllowCredentials(true);
+
+        //2. 添加映射路径
+        UrlBasedCorsConfigurationSource corsConfigurationSource =
+                new UrlBasedCorsConfigurationSource();
+        corsConfigurationSource.registerCorsConfiguration("/**", config);
+        //返回CorsFilter
+        return new CorsFilter(corsConfigurationSource);
+    }
+}

+ 18 - 4
maxkey-webs/maxkey-web-maxkey/src/main/java/org/maxkey/MaxKeyMvcConfig.java

@@ -35,10 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.web.servlet.config.annotation.EnableWebMvc;
-import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
-import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+import org.springframework.web.servlet.config.annotation.*;
 import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
 
 @Configuration
@@ -251,5 +248,22 @@ public class MaxKeyMvcConfig implements WebMvcConfigurer {
         
 
     }
+
+    @Override
+    public void addCorsMappings(CorsRegistry registry) {
+        //添加映射路径
+        registry.addMapping("/**")
+                //是否发送Cookie
+                .allowCredentials(true)
+                //设置放行哪些原始域   SpringBoot2.4.4下低版本使用.allowedOrigins("*")
+                .allowedOriginPatterns("*")
+                //放行哪些请求方式
+                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
+                //.allowedMethods("*") //或者放行全部
+                //放行哪些原始请求头部信息
+                .allowedHeaders("*")
+                //暴露哪些原始请求头部信息
+                .exposedHeaders("*");
+    }
     
 }

+ 46 - 0
maxkey-webs/maxkey-web-maxkey/src/main/java/org/maxkey/WebGlobalConfig.java

@@ -0,0 +1,46 @@
+package org.maxkey;
+
+import org.springframework.boot.SpringBootConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
+
+/**
+ * @Author AnHui_XiaoYang
+ * @Email 939209948@qq.com
+ * @Date 2021/5/10 17:07
+ * @Description
+ */
+@SpringBootConfiguration
+public class WebGlobalConfig {
+
+    @Bean
+    public CorsFilter corsFilter() {
+
+        //创建CorsConfiguration对象后添加配置
+        CorsConfiguration config = new CorsConfiguration();
+        //设置放行哪些原始域
+        config.addAllowedOriginPattern("*");
+        //放行哪些原始请求头部信息
+        config.addAllowedHeader("*");
+        //暴露哪些头部信息
+        config.addExposedHeader("*");
+        //放行哪些请求方式
+        config.addAllowedMethod("GET");     //get
+        config.addAllowedMethod("PUT");     //put
+        config.addAllowedMethod("POST");    //post
+        config.addAllowedMethod("DELETE");  //delete
+        //corsConfig.addAllowedMethod("*");     //放行全部请求
+
+        //是否发送Cookie
+        config.setAllowCredentials(true);
+
+        //2. 添加映射路径
+        UrlBasedCorsConfigurationSource corsConfigurationSource =
+                new UrlBasedCorsConfigurationSource();
+        corsConfigurationSource.registerCorsConfiguration("/**", config);
+        //返回CorsFilter
+        return new CorsFilter(corsConfigurationSource);
+    }
+}