liujie
2026-05-21 34fb8efd9fef8a6ed0d7cbaff74f37f06bf2db60
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
package com.estate.expert.admin.config;
 
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
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 ligf
 * @Description
 * @date 2023-07-10
 **/
@Slf4j
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        log.info("addCorsMappings");
        registry.addMapping("/**")
//                .allowedOrigins("*") // 允许的来源域名
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE") // 允许的HTTP方法
                .allowedHeaders("*") // 允许的请求头
                .allowCredentials(true) // 允许发送身份验证信息(如Cookies)
                .maxAge(3600); // 预检请求(OPTIONS)的缓存时间,单位秒
    }
}