springboot vue 跨域問題的解決
1、Spring Boot跨域配置有兩種方法
在后端使用Spring Boot。Spring Boot跨域非常簡單,只需書寫以下代碼即可。
@Configuration public 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); } }
2.Nginx跨域配置
Spring Boot應(yīng)用用Nginx反向代理。而前端跨域請求的需求不減。
Nginx跨域也比較簡單,只需添加以下配置即可。
location / { proxy_pass http://localhost:8080; if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain; charset=utf-8'; add_header 'Content-Length' 0; return 204; } if ($request_method = 'POST') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token'; add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token'; } if ($request_method = 'GET') { add_header 'Access-Control-Allow-Origin' '*'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token'; add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token'; } }
其中:add_header 'Access-Control-Expose-Headers' 務(wù)必加上你請求時所帶的header。
例如本例中的“Token”,其實是前端傳給后端過來的。如果記不得也沒有關(guān)系,瀏覽器的調(diào)試器會有詳細說明。
三、瀏覽器設(shè)置跨域
Chrome、Firefox本身是可以通過配置支持跨域請求的。
Chrome跨域:參考文檔:Chrome跨域
四、前端Vue設(shè)置跨域
先設(shè)置 axios
axios.defaults.withCredentials = true; axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'; //證明是ajax 請求 psot 請求加入 headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', }
設(shè)置Config 文件下面的index.js 然后就可以再其它頁面訪問了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
httpclient模擬post請求json封裝表單數(shù)據(jù)的實現(xiàn)方法
下面小編就為大家?guī)硪黄猦ttpclient模擬post請求json封裝表單數(shù)據(jù)的實現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12java使用selenium自動化WebDriver等待的示例代碼
顯式等待和隱式等待是WebDriver中兩種常用的等待方式,它們都可以用來等待特定的條件滿足后再繼續(xù)執(zhí)行代碼,本文給大家介紹java使用selenium自動化WebDriver等待,感興趣的朋友一起看看吧2023-09-09idea運行main方法或Test避免編譯整個應(yīng)用的實現(xiàn)方法
這篇文章主要介紹了idea運行main方法或Test避免編譯整個應(yīng)用的方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04配置JAVA環(huán)境變量中CLASSPATH變量的作用
這篇文章主要介紹了配置JAVA環(huán)境變量中CLASSPATH變量的作用,需要的朋友可以參考下2023-06-06