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 ebfa14501b2ab1ec184acdd50268edd06f20d7ba..a6ca25dab51798fe46d6267406f549f404efca50 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 0000000000000000000000000000000000000000..038e575c7b81d75af05f6d595e30acf6ecc7fc9a --- /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; + } +}