java 部分的代码
@Configuration
@EnableWebMvc
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
log.info("Adding CORS mappings");
registry.addMapping("/**") // 推荐限定路径前缀,而不是 /**
.allowedOrigins("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.exposedHeaders("Authorization", "Link", "X-Total-Count")
.allowCredentials(false)
.maxAge(3600);
}
}@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS})
@RestController
@RequestMapping("/count")
public class LaborHourCountController {
我还试了显示的处理 options 请求
@Slf4j
@RestController
@CrossOrigin(origins = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS})
@RequestMapping("/laborOrder")
public class LaborOrderController {
@Resource
private LaborOrderService laborOrderService;
/**
* 查询工时工单列表
* */
@PostMapping(value = "/getLaborOrder/list")
public Result getLaborOrderList(@RequestBody LaborOrderVo laborOrderVo){
PageResult<LaborOrder> laborOrderList = laborOrderService.listByVo(laborOrderVo);
return Result.success(laborOrderList);
}
// 显式处理 OPTIONS 请求
@RequestMapping(value = "/getLaborOrder/list", method = RequestMethod.OPTIONS)
public ResponseEntity<?> handleOptions() {
return ResponseEntity.ok()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST")
.header("Access-Control-Allow-Headers", "Content-Type")
.build();
}
}
仍然浏览器显示
Access to XMLHttpRequest at 'http://xxx:8085/laborOrder/getLaborOrder/list' from origin 'http://xxxx:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
![]() |
1
klo424 22 天前
感觉问 AI 快一点
|
2
Ayanokouji 22 天前
猜测 Access-Control-Allow-Credentials 问题
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Reference/Headers/Access-Control-Allow-Credentials |
3
adov 22 天前 via Android
看接口返回的响应标头
|
![]() |
4
DOLLOR 22 天前
看 DevTools 的 Network 里的请求结果
|
5
xiaohantx OP @DOLLOR
Provisional headers are shown Accept: application/json, text/plain, */* Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLlsLnmmZciLCJ1c2VySWQiOiIxNzEwOTA1MDQ5NTUyNiIsInBlcm1zIjpbXSwiYXZhdGFyIjoiaHR0cHM6Ly9zdGF0aWMtbGVnYWN5LmRpbmd0YSG1uMUFBXzcwN183MDcuanBnIiwidXNlck5hbWUiOiLlsLnmmZciLCJleHAiOjE3NTE1NDgzNDMsImlhdCI6MTc1MTUzMDM0M30.CeY0ER8LrElYGxMtYmz9DR_1jI84NRVh2Dq_FH3O_w5TQbhk8mJhIfrZIeor30YF7IURGzdUI8_ym6UNa_9Dcg Content-Type: application/json Referer: http://192.168.130.163:5173/ User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36 dingtalk-win/1.0.0 nw(0.14.7) DingTalk(7.0.20-RC.5169101) Mojo/1.0.0 Native AppType(rc) Channel/201200 Architecture/x86_64 |
6
Razio 22 天前 ![]() 月经贴之不会跨域的同事
|
7
SirYuxuan 22 天前
/**
* 处理跨域问题 * * @return 跨域配置 */ @Bean public CorsFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); CorsConfiguration config = new CorsConfiguration(); config.setAllowCredentials(true); config.addAllowedOriginPattern("*"); config.addAllowedHeader("*"); config.addAllowedMethod("*"); source.registerCorsConfiguration("/**", config); return new CorsFilter(source); } 试试这个呢 |
8
xiaohantx OP |
![]() |
10
leokun 22 天前
在无痕模式下,应该可以看到一次 OPTIONS 请求(有缓存,并不是每次都会发)
这个 OPTIONS 需要满足 http 状态码需要 200 响应头需要包含 Access-Control-Allow-Origin * 和 Access-Control-Allow-Methods: * |
![]() |
11
leokun 22 天前
上面截图里面 OPTIONS 的响应头 Access-Control-Allow-Methods 只允许 post ,所以 OPTIONS 检查实际是失败了
|
![]() |
12
lpe234 22 天前
|
14
xiaohantx OP @DOLLOR
@adov @Ayanokouji @SirYuxuan @leokun #11 有一个 login 接口反而没拦截下来 GET / 190:8085/login?authCode=28be5bbfd Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: http://192.168.130.163:5173 Connection: keep-alive Content-Type: application/json Date: Thu, 03 Jul 2025 09:10:01 GMT Keep-Alive: timeout=4 Proxy-Connection: keep-alive Transfer-Encoding: chunked Vary: Origin Vary: Access-Control-Request-Method Vary: Access-Control-Request-Headers |
15
xiaohantx OP |
![]() |
16
swczxf 22 天前 via iPhone
是不是你后面请求头比 login 接口多
|
![]() |
19
wxw752 22 天前
不会是在多个地方加了处理跨域的头吧,加多次等于没加
|
20
adov 22 天前 via Android
就是你后端的 Access-Control-Allow-Methods 没配置对,你看看 options 请求的返回,让后端配置到对为止
|
21
199808lanlan1111 22 天前 via Android
刚遇到同个问题 allowedOrigins 不支持配置*,看是否能配置 config.addAllowedOriginPattern ,这个支持*,我是不能配置后者,重写了 config 判断*的逻辑
|
![]() |
22
unco020511 22 天前
|
![]() |
23
Aolose 22 天前
不了解 java 但是我觉得预检请求 应该允许 option 而不是只是 post
另外非 bug 预检一般会返回 204 这个 ResponseEntity.ok() 是不是默认 200 ?(当然这不影响啥 @RequestMapping(value = "/getLaborOrder/list", method = RequestMethod.OPTIONS) public ResponseEntity<?> handleOptions() { return ResponseEntity.ok() .header("Access-Control-Allow-Origin", "*") .header("Access-Control-Allow-Methods", "POST") .header("Access-Control-Allow-Headers", "Content-Type") .build(); } |
![]() |
24
Al0rid4l 22 天前
你最好是把请求报文和响应报文贴出来(浏览器, 或者抓包代理), 而不是贴请求工具的截图, 只有一点点信息让人搁这猜, 很累的. 查 CORS 问题最好的办法就是贴原始报文而不是贴代码
然后, 假设你图里都是响应报文, 那肉眼可见的问题有 Access-Control-Allow-Methods 只允许了 POST 而没有 OPTIONS, 虽然看你 Java 代码里有 allowedMethods 给了 OPTIONS, 但也不知道有没有执行到这里或者有其他中间件提前响应或覆盖响应头, 这个要后端去查. 你说添加了还是不行, 至少从你第二张图看来, 它没生效呀...没看出来添加了个啥 请求里有个 Authorization 头, 但响应头 Access-Control-Request-Headers 又只有 Content-Type, 虽然 Java 代码里的设置也是 allowedHeaders("*"), 但还是上面一样的问题, 反正最终响应只有 Content-Type 而没有允许 Authorization 另外还不知道前端请求有没有设置 withCredentials, 如果有的话, 后端配置 Access-Control-Allow-Origin 是不能用*的 还有这个 login 接口是个 GET, auth 还在 URL, 所以是个简单请求就过去了, 不过也挺抽象的... |
![]() |
25
zpfhbyx 21 天前
* 不是*,*
|