日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/7/14 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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跨域问题的全部內容,希望文章能夠幫你解決所遇到的問題。

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