網(wǎng)站首頁 編程語言 正文
Spring mvc解決跨域請(qǐng)求:Response to preflight request doesn‘t pass access control check
作者:heroleader 更新時(shí)間: 2022-03-14 編程語言在nginx跨域請(qǐng)求cors配置如下:
location / { add_header 'Access-Control-Allow-Origin' 'https://api.xxxx.com'; add_header "Access-Control-Allow-Credentials" "true"; add_header "Access-Control-Allow-Headers" "x-requested-with,content-type"; proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }
主要是添加了三個(gè)用于控制CORS的頭信息:
- Access-Control-Allow-Origin:允許的來源
- Access-Control-Allow-Credentials:設(shè)置為true,允許ajax異步請(qǐng)求帶cookie信息
- Access-Control-Allow-Headers:設(shè)置為x-requested-with,content-type,允許ajax余部請(qǐng)求。
這個(gè)配置也可以在spring mvc里配置。
問題:
配置好后,異步跨域請(qǐng)求成功。
當(dāng)發(fā)起post請(qǐng)求,請(qǐng)求的數(shù)據(jù)是放在request body里,請(qǐng)求的Content-Type為application/json。Spring MVC需要使用@RequestBody來接收數(shù)據(jù)。
這是一次非簡(jiǎn)單的請(qǐng)求,瀏覽器會(huì)發(fā)起一次Options的預(yù)請(qǐng)求。發(fā)起Options請(qǐng)求報(bào)403錯(cuò)誤:
Failed to load https://api.xxxx.com/auth.json: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://other.xxxx.com' is therefore not allowed access. The response had HTTP status code 403.
?
解決方法
原以為是自己的配置有誤,但找了很多資料可以確定配置是沒有問題。后來debug跟蹤Spring MVC DispatcherServlet的調(diào)用,發(fā)現(xiàn)在DispacerServlet是因?yàn)闆]有找都到執(zhí)行Options請(qǐng)求的方法。
在做跨域請(qǐng)求時(shí),配置好CORS相關(guān)頭信息后,以為就可以了,這很容易忽略在Spring MVC添加對(duì)Options請(qǐng)求的處理。
解決方法有兩種:
- 在攔截器統(tǒng)一添加對(duì)Options請(qǐng)求的支持
- 添加一個(gè)支持Options的ReqeuestMapping。
攔截器示例:
public class CrosDomainAllowInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if(request.getMethod().equals(RequestMethod.OPTIONS.name())) { response.setStatus(HttpStatus.OK.value()); return false; } return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { } @Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { } }
@RequestMapping示例
@RequestMapping(value = "/*",method = RequestMethod.OPTIONS) public ResponseEntity handleOptions(){ return ResponseEntity.noContent(); }
原文鏈接:https://blog.csdn.net/baoyinwang/article/details/110955167
相關(guān)推薦
- 2022-06-23 QT實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能_C 語言
- 2022-09-25 Stream流水線的實(shí)現(xiàn)原理是什么
- 2022-08-27 python中DataFrame數(shù)據(jù)合并merge()和concat()方法詳解_python
- 2022-03-16 ASP.NET?Core在Linux下為dotnet創(chuàng)建守護(hù)進(jìn)程_基礎(chǔ)應(yīng)用
- 2022-05-17 Git 克隆指定分支的代碼
- 2023-05-21 python?jinjia2的項(xiàng)目使用_python
- 2022-04-07 Redis數(shù)據(jù)庫分布式設(shè)計(jì)方案介紹_Redis
- 2024-02-26 IDEA隱藏指定文件/文件夾
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支