網(wǎng)站首頁 編程語言 正文
Spring mvc解決跨域請求:Response to preflight request doesn‘t pass access control check
作者:heroleader 更新時間: 2022-03-14 編程語言在nginx跨域請求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; }
主要是添加了三個用于控制CORS的頭信息:
- Access-Control-Allow-Origin:允許的來源
- Access-Control-Allow-Credentials:設置為true,允許ajax異步請求帶cookie信息
- Access-Control-Allow-Headers:設置為x-requested-with,content-type,允許ajax余部請求。
這個配置也可以在spring mvc里配置。
問題:
配置好后,異步跨域請求成功。
當發(fā)起post請求,請求的數(shù)據(jù)是放在request body里,請求的Content-Type為application/json。Spring MVC需要使用@RequestBody來接收數(shù)據(jù)。
這是一次非簡單的請求,瀏覽器會發(fā)起一次Options的預請求。發(fā)起Options請求報403錯誤:
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的調用,發(fā)現(xiàn)在DispacerServlet是因為沒有找都到執(zhí)行Options請求的方法。
在做跨域請求時,配置好CORS相關頭信息后,以為就可以了,這很容易忽略在Spring MVC添加對Options請求的處理。
解決方法有兩種:
- 在攔截器統(tǒng)一添加對Options請求的支持
- 添加一個支持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
相關推薦
- 2023-04-21 如何在C#項目中鏈接一個文件夾下的所有文件詳解_C#教程
- 2022-06-25 說說react中引入css的方式有哪些并區(qū)別在哪_React
- 2023-02-09 Python的Flask項目中獲取請求用戶IP地址?addr問題_python
- 2022-10-18 一文詳解Python中的Map,Filter和Reduce函數(shù)_python
- 2022-07-22 px和em和rem的區(qū)別
- 2023-01-14 GoLang日志監(jiān)控系統(tǒng)實現(xiàn)_Golang
- 2022-10-16 python?os.path模塊使用方法介紹_python
- 2022-11-07 深入了解Python中運算符函數(shù)的使用_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支