日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

Spring boot 和Vue开发中CORS跨域问题

發布時間:2025/7/14 vue 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring boot 和Vue开发中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*/ @Configuration public class CustomCORSConfiguration {private CorsConfiguration buildConfig() {CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.addAllowedOrigin("*");corsConfiguration.addAllowedHeader("*");corsConfiguration.addAllowedMethod("*");corsConfiguration.setAllowCredentials(true);return corsConfiguration;}@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", buildConfig());return new CorsFilter(source);} }

總結

以上是生活随笔為你收集整理的Spring boot 和Vue开发中CORS跨域问题的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。