CorsConfig.java
4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.glsk.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 解决异步访问跨域
*/
//@Configuration
//public class CorsConfig {
//
// @Bean
// public WebMvcConfigurer corsConfigurer(){
// return new WebMvcConfigurer() {
// @Override
// public void addCorsMappings(CorsRegistry registry) {
// registry.addMapping("/**")
// .allowedHeaders("*")
// .allowedMethods("*")
// .maxAge(86400L)
// .allowedOrigins("*");
// }
// };
// }
//
//
//// public class CorsConfig implements WebMvcConfigurer {
//// @Override
//// public void addCorsMappings(CorsRegistry registry) {
//// //本应用的所有方法都会去处理跨域请求
//// registry.addMapping("/**")
//// .allowedOrigins("*")
//// //允许远端访问的域名
////// .allowedOrigins("http://192.168.1.149:9527")
//// .allowedOrigins("*")
//// //允许请求的方法("POST", "GET", "PUT", "OPTIONS", "DELETE")
//// .allowedMethods("*")
//// //允许请求头
//// .allowedHeaders("*");
////
////
////
//// }
////
////
////
//// private CorsConfiguration buildConfig() {
//// CorsConfiguration corsConfiguration = new CorsConfiguration();
//// corsConfiguration.setAllowCredentials(true); //sessionid 多次访问一致
////
//// // 允许访问的客户端域名
//// List<String> allowedOriginPatterns = new ArrayList<>();
//// allowedOriginPatterns.add("*");
////
////// corsConfiguration.setAllowedOriginPatterns(allowedOriginPatterns);
//// corsConfiguration.setAllowedOrigins(allowedOriginPatterns);
//// corsConfiguration.addAllowedOrigin("*"); // 允许任何域名使用
//// corsConfiguration.addAllowedHeader("*"); // 允许任何头
//// corsConfiguration.addAllowedMethod("*"); // 允许任何方法(post、get等)
//// return corsConfiguration;
//// }
////
//// @Bean
//// public CorsFilter corsFilter() {
//// UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
//// source.registerCorsConfiguration("/**", buildConfig()); // 对接口配置跨域设置
////
//// CorsConfiguration corsConfiguration = new CorsConfiguration();
//// corsConfiguration.addAllowedHeader("*");
//// corsConfiguration.addAllowedOrigin("*");
//// corsConfiguration.addAllowedMethod("*");
//// corsConfiguration.setAllowCredentials(true);
//// source.registerCorsConfiguration("/**",corsConfiguration);
////
////
//// CorsFilter corsFilter = new CorsFilter(source);
////
////
//// return corsFilter;//new CorsFilter(source)
//// }
////
//
//}
/**
* 跨域配置
*/
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").
allowedOrigins("*"). //允许跨域的域名,可以用*表示允许任何域名使用
// allowedOrigins("https://www.dustyblog.cn"). //允许跨域的域名,可以用*表示允许任何域名使用
allowedMethods("*"). //允许任何方法(post、get等)
allowedHeaders("*"). //允许任何请求头
allowCredentials(true). //带上cookie信息
exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L); //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
}
};
}
}