網站首頁 編程語言 正文
跨域問題Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin
作者:_花開富貴 更新時間: 2022-03-14 編程語言文章目錄
1.bug詳情:
vue項目,用axios請求,前后端聯調,在局域網內,
瀏覽器訪問別人的地址可以獲取數據,但是通過接口請求,就會跨域
2.解決方案【前后端解決方案】:
2.1.前端方案:
2.1.1. 設置Access-Control-Allow-Origin
如果XMLHttpRequest 請求的URL和當前頁面不同一個域中時,
瀏覽器會檢測響應http header中有沒有 Access-Control-Allow-Origin項,
如果此項值為空或者與當前頁面的域不匹配時,就會報此錯誤。
解決方案:設置Access-Control-Allow-Origin為【請求的域名+端口】,有人也說設置*,情況不同吧,可以試試
config.headers["Access-Control-Allow-Origin"]="http://127.0.0.1:8080";
還有很多其他實現方式,例如:
config.headers.setItem("Access-Control-Allow-Origin","127.0.0.1:8081");
所以自由發揮,最后頭信息加上Access-Control-Allow-Origin就OK
2.1.2. 設置proxyTable
設置proxyTable,實際上就是設置代理路徑
(PS:設置config文件之后,需要重新npm run dev)
dev: {
env: require('./dev.env'),
port: 8080,
autoOpenBrowser: true,
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
'/api': {
target: '請求地址ip加端口',
changeOrigin: true,
pathRewrite: {
'^/api': '/'
}
}
},
}
2.2.后端方案:
2.2.1. 在controller對應的方法里添加“Access-Control-Allow-Origin”標頭”
既然說了《請求的資源上不存在“Access-Control-Allow-Origin”標頭》,
那么我就在controller文件對應的方法里給他添加一個標頭就好了
/**
* 查詢當前登錄用戶的信息
* DSY
*/
@RequestMapping(value = "/login", method = RequestMethod.GET)
@ResponseBody
public User login(HttpServletResponse response, @RequestParam(value="username")String username,@RequestParam(value="password")String password) {
response.setHeader("Access-Control-Allow-Origin", "*");
return loginService.getUser(username, password);
}
2.2.2. @CrossOrigin注解解決跨域
如果你用的也是spring框架,并且版本在4.2以上,可以用@CrossOrigin這個注解,
括號中的url可以換成“”*“”,如果這個controller里有多個方法,注解可以直接寫在類的最上邊。
a.寫在方法上:
/**
* 查詢當前登錄用戶的信息
* DSY
*/
//寫在方法上
@CrossOrigin(origins = "http://localhost:8080/") // 實現跨域 要求spring的版本必須4.2以上
@RequestMapping(value = "/login", method = RequestMethod.GET)
@ResponseBody
public User login(HttpServletResponse response, @RequestParam(value="username")String username,@RequestParam(value="password")String password) {
return loginService.getUser(username, password);
}
b.寫在類上:
//寫在類上,不用每個方法都寫這個注解了
@CrossOrigin(origins="*",maxAge=3600)
@RestController
public class LoginController {}
2.2.3. 今天發現一個特別強的,只需要啟動類上加代碼就可以了
@Bean
public CorsFilter corsFilter() {
final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
final CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); // 允許cookies跨域
config.addAllowedOrigin("*");// 允許向該服務器提交請求的URI,*表示全部允許。。這里盡量限制來源域,比如http://xxxx:8080 ,以降低安全風險。。
config.addAllowedHeader("*");// 允許訪問的頭信息,*表示全部
config.setMaxAge(18000L);// 預檢請求的緩存時間(秒),即在這個時間段里,對于相同的跨域請求不會再預檢了
config.addAllowedMethod("*");// 允許提交請求的方法,*表示全部允許,也可以單獨設置GET、PUT等
source.registerCorsConfiguration("/**", config);
return new CorsFilter(source);
}
3.結語:
解決辦法還有很多,我提及部分只是對我的項目比較受用,大家可以多嘗試嘗試,
然后如果單使用哪一條無效就把這些都加上吧,總有一條適合你!!!(我就這么干的?( ? ▽ ? )?)
以后我遇到新的解決辦法也會在博客里更新的。
原文鏈接:https://blog.csdn.net/sinat_42338962/article/details/104452022
- 上一篇:npm 更改為淘寶鏡像的方法
- 下一篇:命令刪除node_modules
相關推薦
- 2022-04-20 C++的多態和虛函數你真的了解嗎_C 語言
- 2024-01-12 如何理解 Elasticsearch 中的 Indices、Types、Documents、Fiel
- 2022-05-01 C#使用log4net打日志_C#教程
- 2023-05-14 Python實現批量導入1000條xlsx數據_python
- 2022-08-24 python多線程死鎖現象及解決方法_python
- 2022-03-11 fatal error LNK1120: 1 個無法解析的外部命令 的解決辦法
- 2022-09-13 iOS開發之UIMenuController使用示例詳解_IOS
- 2023-11-12 解決yolov3編譯中出現的問題:darknet make include/darknet.h:16
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支