Spring Boot + Vue 跨域请求问题
2024-08-27 12:20:07
使用Spring Boot + Vue 做前后端分离项目搭建,实现登录时,出现跨域请求
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);
}
}
参考:https://blog.csdn.net/qq_16645099/article/details/89415997
最新文章
- [leetcode] 47. Permutations II
- Asp.net MVC5 框架揭秘 S412 实例解析 – 绝妙的扩展 模式的胜利
- [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials
- number-of-segments-in-a-string
- net cookie操作
- [z]CAP原理和BASE思想
- PHP magic_quotes_gpc的详细使用方法
- VitamioBundle-master
- windows下nginx+tomcat分布式集群部署
- 【Hibernate步步为营】--双向关联一对一映射具体解释(一)
- 【基础】Attribute的妙用
- QC3.0充电标准
- C# 从网站下载图片
- 6.ST LINK 下调试异常
- nginx介绍(五) - 高可用
- [JZOJ5987] 仙人掌毒题
- eclipse中中文注释乱码怎么解决
- SQL Server 2008“备份集中的数据库备份与现有的数据库不同”解决方法
- Redis(二)持久化
- hdu 2642