WebMvcConfig.java
1.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
package cn.com.glsx.entmicro.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Author yelq
* @Version 1.0
* @Description: 跨域处理
* @Date 2022/9/23 10:54
*/
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private Environment environment;
@Override
public void addCorsMappings(CorsRegistry corsRegistry){
String mappings = "/picture/compression/upload,/file/upload,/video/compression/upload";
String origins = environment.getProperty("entmicro.allowed-origins");
if (origins==null){
origins = "http://localhost:8080";
}
for (String mapping : mappings.split(",")) {
corsRegistry.addMapping(mapping)
.allowedOrigins(origins.split(","))
.allowedMethods(CorsConfiguration.ALL)
.allowedHeaders(CorsConfiguration.ALL)
.allowCredentials(true)//携带凭证
.maxAge(3600);
}
}
}