|
@@ -2,26 +2,38 @@ package com.example.hotel.config;
|
|
|
|
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
-import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
|
|
|
|
-import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
|
|
|
|
|
+import org.springframework.web.cors.CorsConfiguration;
|
|
|
|
|
+import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
|
|
|
+import org.springframework.web.filter.CorsFilter;
|
|
|
|
|
|
|
|
-/**
|
|
|
|
|
- * @Author: chenyizong
|
|
|
|
|
- * @Date: 2020-02-29
|
|
|
|
|
- */
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
|
public class CORSConfig {
|
|
public class CORSConfig {
|
|
|
|
|
+ private static String[] originsVal = new String[]{
|
|
|
|
|
+ "localhost:8081",
|
|
|
|
|
+ "127.0.0.1:8081",
|
|
|
|
|
+ "127.0.0.1",
|
|
|
|
|
+ "localhost",
|
|
|
|
|
+ "172.19.144.143",
|
|
|
|
|
+ "172.19.144.143:8000"
|
|
|
|
|
+ };
|
|
|
|
|
|
|
|
@Bean
|
|
@Bean
|
|
|
- public WebMvcConfigurer corsConfigurer(){
|
|
|
|
|
- return new WebMvcConfigurer() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void addCorsMappings(CorsRegistry registry) {
|
|
|
|
|
- registry.addMapping("/**")
|
|
|
|
|
- .allowedHeaders("*")
|
|
|
|
|
- .allowedMethods("*")
|
|
|
|
|
- .allowedOrigins("*");
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
|
|
+ public CorsFilter corsFilter() {
|
|
|
|
|
+ UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
|
|
|
|
+ CorsConfiguration corsConfiguration = new CorsConfiguration();
|
|
|
|
|
+ addAllowedOrigins(corsConfiguration); // 1
|
|
|
|
|
+ //corsConfiguration.addAllowedOrigin("*");
|
|
|
|
|
+ corsConfiguration.addAllowedHeader("*"); // 2
|
|
|
|
|
+ corsConfiguration.addAllowedMethod("*"); // 3
|
|
|
|
|
+ corsConfiguration.setAllowCredentials(true); // 跨域session共享
|
|
|
|
|
+ source.registerCorsConfiguration("/**", corsConfiguration); // 4
|
|
|
|
|
+ return new CorsFilter(source);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void addAllowedOrigins(CorsConfiguration corsConfiguration) {
|
|
|
|
|
+ for (String origin : originsVal) {
|
|
|
|
|
+ corsConfiguration.addAllowedOrigin("http://" + origin);
|
|
|
|
|
+ corsConfiguration.addAllowedOrigin("https://" + origin);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|