|
@@ -0,0 +1,53 @@
|
|
|
+package com.llisoft.sms.config;
|
|
|
+
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+import org.springframework.http.HttpMethod;
|
|
|
+import org.springframework.web.cors.CorsConfiguration;
|
|
|
+import org.springframework.web.cors.CorsConfigurationSource;
|
|
|
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
+import org.springframework.web.filter.CorsFilter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 跨域配置
|
|
|
+ * @author YangJie [2018年12月25日]
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@Order(Integer.MIN_VALUE) // 指定顺序,第一个
|
|
|
+public class CorsFilterConfig extends CorsFilter {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 跨域规则
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static CorsConfigurationSource configurationSource() {
|
|
|
+ CorsConfiguration configuration = new CorsConfiguration();
|
|
|
+ configuration.addAllowedMethod(HttpMethod.OPTIONS);
|
|
|
+ configuration.addAllowedMethod(HttpMethod.POST);
|
|
|
+ configuration.addAllowedMethod(HttpMethod.GET);
|
|
|
+ configuration.addAllowedOrigin("*");
|
|
|
+ configuration.addAllowedHeader("*");
|
|
|
+ UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
|
|
|
+ configurationSource.registerCorsConfiguration("/**", configuration);
|
|
|
+ return configurationSource;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 重写构造方法
|
|
|
+ */
|
|
|
+ public CorsFilterConfig() {
|
|
|
+ super(configurationSource());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 在WebMvcConfigurer的子类中处理跨域
|
|
|
+ // 如果在Filter或interceptor中return会使跨域失效
|
|
|
+// @Override // 跨域配置
|
|
|
+// public void addCorsMappings(CorsRegistry registry) {
|
|
|
+// registry.addMapping("/**")
|
|
|
+// .allowedOrigins("*")
|
|
|
+// .allowedHeaders("*")
|
|
|
+// .allowedMethods("POST", "OPTIONS");
|
|
|
+// }
|
|
|
+
|
|
|
+}
|