国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Spring boot 和Vue開發中CORS跨域問題解決

2024-05-06 15:30:36
字體:
來源:轉載
供稿:網友

跨域資源共享CORS(Cross-origin Resource Sharing),是W3C的一個標準,允許瀏覽器向跨源的服務器發起XMLHttpRequest請求,克服ajax請求只能同源使用的限制。關于CORS的詳細解讀,可參考阮一峰大神的博客:跨域資源共享CORS詳解。

1. 遇到的問題:

我用spring-boot 做Rest服務,Vue做前端框架,用了element-admin-ui這個框架做后臺管理。在調試的過程中遇到了如下錯誤:

Preflight response is not successful

2. 分析問題

這個問題是典型的CORS跨域問題。

所謂跨域:

跨域,指的是瀏覽器不能執行其他網站的腳本。它是由瀏覽器的同源策略造成的,是瀏覽器對JavaScript施加的安全限制。

3. 解決方法

在項目中添加類CustomCORSConfiguration 代碼如下:

import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.web.cors.CorsConfiguration;import org.springframework.web.cors.UrlBasedCorsConfigurationSource;import org.springframework.web.filter.CorsFilter;/** * @author spartajet * @description * @create 2018-05-15 下午5:00 * @email spartajet.guo@gmail.com */@Configurationpublic class CustomCORSConfiguration {  private CorsConfiguration buildConfig() {    CorsConfiguration corsConfiguration = new CorsConfiguration();    corsConfiguration.addAllowedOrigin("*");    corsConfiguration.addAllowedHeader("*");    corsConfiguration.addAllowedMethod("*");    corsConfiguration.setAllowCredentials(true);    return corsConfiguration;  }  @Bean  public CorsFilter corsFilter() {    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();    source.registerCorsConfiguration("/**", buildConfig());    return new CorsFilter(source);  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持錯新站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 雷州市| 信宜市| 平凉市| 虎林市| 白朗县| 新宁县| 讷河市| 阿克苏市| 海伦市| 乌拉特中旗| 茶陵县| 红原县| 大竹县| 盈江县| 舟曲县| 江口县| 苗栗县| 连平县| 峡江县| 改则县| 方山县| 巫溪县| 通山县| 丰台区| 读书| 丹江口市| 禄劝| 同仁县| 鄱阳县| 东山县| 广水市| 蛟河市| 枣强县| 灵石县| 玛纳斯县| 华蓥市| 白银市| 罗山县| 永兴县| 田阳县| 安龙县|