Spring Boot和Vue跨域請求問題原理解析
這篇文章主要介紹了Spring Boot和Vue跨域請求問題原理解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
使用Spring Boot + Vue 做前后端分離項目搭建,實現(xiàn)登錄時,出現(xiàn)跨域請求
Access to XMLHttpRequest at 'http://localhost/open/login' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Vue中使用的Axios,配置main.js文件
Axios.defaults.baseURL = 'http://localhost:80' Axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8' Axios.defaults.withCredentials = true
Spring Boot中重寫WebMvcConfigurationSupport的方法addCorsMapping
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport{ @Override public void addCorsMappings(CorsRegistry registry) { String[] origins = {"http://localhost:8080"}; registry.addMapping("/**") .allowedOrigins(origins) .allowCredentials(true) .allowedMethods("*") .allowedHeaders("*") .maxAge(3600); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Web零xml配置原理以及父子容器關(guān)系詳解
這篇文章主要介紹了Spring Web零xml配置原理以及父子容器關(guān)系詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08Spring Boot中快速操作Mongodb數(shù)據(jù)庫指南
這篇文章主要給大家介紹了關(guān)于Spring Boot中如何快速操作Mongodb的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05Windows下 IDEA編譯調(diào)試 hive2.3.9的過程解析
這篇文章主要介紹了Windows下 IDEA編譯調(diào)試 hive2.3.9的過程,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解
這篇文章主要介紹了Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12