
本文详细讲解如何在 spring boot 项目中正确配置 cors,解决 angular 前端(https://www.php.cn/link/cbd35b795565394c06635007e20f1583)调用后端接口(http://localhost:8090)时因预检请求失败导致的 “request header field domain is not allowed” 报错。
在前后端分离开发中,Angular 运行于 https://www.php.cn/link/cbd35b795565394c06635007e20f1583,而 Spring Boot 后端部署在 http://localhost:8090,浏览器会因同源策略(Same-Origin Policy)触发 CORS(跨域资源共享)检查。你遇到的错误:
Access to XMLHttpRequest at 'http://localhost:8090/bites/service/signup' from origin 'https://www.php.cn/link/cbd35b795565394c06635007e20f1583' has been blocked by CORS policy: Request header field domain is not allowed by Access-Control-Allow-Headers in preflight response.
本质是预检请求(OPTIONS)失败:前端发送了自定义请求头(如 domain),但后端响应中 Access-Control-Allow-Headers 未包含该字段,导致浏览器拒绝后续实际请求。
⚠️ 注意:你当前手动在 Filter 中设置 CORS 响应头的方式存在严重隐患——它未处理预检请求(OPTIONS)的短路逻辑,也未动态匹配或放行自定义请求头(如 domain),更未对 OPTIONS 请求提前返回 200 OK,因此浏览器在预检阶段即中断流程。
✅ 正确做法是优先使用 Spring MVC 内置的 CORS 配置机制,它自动处理预检、响应头注入、请求放行与方法/头白名单校验,安全且健壮。
✅ 推荐方案:使用 @Bean WebMvcConfigurer 全局配置(Spring Boot 2.4+ 推荐)
✅ 适用于 Spring Boot 2.0+(含最新版本),兼容性好、语义清晰、无需手动处理 Filter 生命周期。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() { // 注意:Spring Boot 2.4+ 已弃用 WebMvcConfigurerAdapter
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**") // 匹配所有路径
.allowedOrigins("https://www.php.cn/link/cbd35b795565394c06635007e20f1583") // 明确允许的前端源
.allowCredentials(true) // 允许携带 Cookie/Authorization
.maxAge(3600) // 预检结果缓存 1 小时
.allowedHeaders("Origin", "X-Requested-With",
"Content-Type", "Accept",
"Key", "Authorization", "domain"); // ✅ 关键:显式添加 "domain"
}
};
}
}? 关键点说明:
- .allowedHeaders(...) 必须精确包含前端实际发送的所有自定义请求头(如 domain),否则预检失败;
- .allowCredentials(true) 时,allowedOrigins *不能为 `""**,必须指定具体源(如"https://www.php.cn/link/cbd35b795565394c06635007e20f1583"`);
- addMapping("/**") 支持 Ant 风格路径,也可细化为 .addMapping("/bites/service/**") 提升安全性。
? 备选方案:@CrossOrigin 注解(细粒度控制)
若只需对特定 Controller 或方法开放跨域,可直接注解:
@RestController
@RequestMapping("/bites/service")
@CrossOrigin(
origins = "https://www.php.cn/link/cbd35b795565394c06635007e20f1583",
allowCredentials = "true",
maxAge = 3600,
allowedHeaders = {"Origin", "X-Requested-With", "Content-Type", "Accept", "Key", "Authorization", "domain"}
)
public class UserController {
@PostMapping("/signup")
public ResponseEntity<?> signup(@RequestBody User user) {
// ...
}
}⚠️ 重要注意事项
- ❌ 不要混用 Filter + WebMvcConfigurer:二者叠加可能导致响应头重复或冲突,优先选择声明式配置;
- ❌ 避免在 Filter 中硬编码响应头而不拦截 OPTIONS:手动 Filter 需额外判断 request.getMethod().equals("OPTIONS") 并 return,否则业务逻辑仍会执行,造成冗余;
- ✅ 生产环境务必替换 allowedOrigins:禁止使用 "*"(尤其开启 allowCredentials 时),应配置可信域名白名单(如 https://myapp.com, https://admin.myapp.com);
- ?️ 若使用 Spring Security,需确保其未覆盖 CORS 配置——在 SecurityConfig 中添加 .cors(Customizer.withDefaults()) 并确保 CorsConfigurationSource Bean 已注册。
✅ 验证是否生效
启动应用后,向任意接口发起预检请求(如用 curl):
curl -I -X OPTIONS http://localhost:8090/bites/service/signup \ -H "Origin: https://www.php.cn/link/cbd35b795565394c06635007e20f1583" \ -H "Access-Control-Request-Method: POST" \ -H "Access-Control-Request-Headers: content-type,domain"
预期响应中应包含:
Access-Control-Allow-Origin: https://www.php.cn/link/cbd35b795565394c06635007e20f1583 Access-Control-Allow-Credentials: true Access-Control-Allow-Headers: content-type,domain,... Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
至此,Angular 的 signup 请求即可成功抵达 Spring Boot 控制器,CORS 问题彻底解决。










