From 52e0a723b94d46434e3e410417f29c27ee8bbb88 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=B5=B5=E5=95=B8=E9=9D=9E?= <13281114856@qq.com>
Date: Tue, 21 Jun 2022 14:28:21 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=BE=E5=A4=87=E7=AB=99?=
 =?UTF-8?q?=E7=82=B9=E5=90=8D=E7=A7=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../xhx/base/framework/config/CorsConfig.java | 22 ++++++++++++++++
 .../framework/config/CrossInterceptor.java    | 26 +++++++++++++++++++
 2 files changed, 48 insertions(+)
 create mode 100644 device-manager/src/main/java/com/mortals/xhx/base/framework/config/CrossInterceptor.java

diff --git a/device-manager/src/main/java/com/mortals/xhx/base/framework/config/CorsConfig.java b/device-manager/src/main/java/com/mortals/xhx/base/framework/config/CorsConfig.java
index ebfa145..a6ca25d 100644
--- a/device-manager/src/main/java/com/mortals/xhx/base/framework/config/CorsConfig.java
+++ b/device-manager/src/main/java/com/mortals/xhx/base/framework/config/CorsConfig.java
@@ -1,6 +1,10 @@
 package com.mortals.xhx.base.framework.config;
 
+import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.web.cors.CorsConfiguration;
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
+import org.springframework.web.filter.CorsFilter;
 import org.springframework.web.servlet.config.annotation.CorsRegistry;
 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 
@@ -12,6 +16,24 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 @Configuration
 public class CorsConfig implements WebMvcConfigurer {
 
+    @Bean
+    public CorsFilter corsFilter(){
+        //鍒濆鍖栭厤缃璞�
+        CorsConfiguration configuration = new CorsConfiguration();
+        //鍏佽璺ㄥ煙璁块棶鐨勫煙鍚�
+        configuration.addAllowedOrigin("*");
+        // configuration.setAllowCredentials(true);  //杩愯鎼哄甫cookie
+        configuration.addAllowedMethod("*"); //浠h〃鎵€鏈夎姹傛柟娉�
+        configuration.addAllowedHeader("*"); //鍏佽鎼哄甫浠讳綍澶翠俊鎭�
+
+        //鍒濆鍖朿ors閰嶇疆婧愬璞�
+        UrlBasedCorsConfigurationSource configurationSource=new UrlBasedCorsConfigurationSource();
+        configurationSource.registerCorsConfiguration("/**",configuration);
+
+        //杩斿洖CorSfilter瀹炰緥锛屽弬鏁�
+        return new CorsFilter(configurationSource);
+    }
+
     @Override
     public void addCorsMappings(CorsRegistry registry) {
         registry.addMapping("/**")
diff --git a/device-manager/src/main/java/com/mortals/xhx/base/framework/config/CrossInterceptor.java b/device-manager/src/main/java/com/mortals/xhx/base/framework/config/CrossInterceptor.java
new file mode 100644
index 0000000..038e575
--- /dev/null
+++ b/device-manager/src/main/java/com/mortals/xhx/base/framework/config/CrossInterceptor.java
@@ -0,0 +1,26 @@
+package com.mortals.xhx.base.framework.config;
+
+import org.springframework.stereotype.Component;
+import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * @author: zxfei
+ * @date: 2022/6/6 15:05
+ * @description:娣诲姞璺ㄥ煙鍝嶅簲
+ **/
+@Component
+public class CrossInterceptor extends HandlerInterceptorAdapter {
+
+    @Override
+    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
+        response.setHeader("Access-Control-Allow-Origin", "*");
+        response.setHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
+        response.setHeader("Access-Control-Max-Age", "3600");
+        response.setHeader("Access-Control-Allow-Headers", "*");
+        response.setHeader("Access-Control-Allow-Credentials", "true");
+        return true;
+    }
+}
-- 
2.24.3