使用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

最新文章

  1. [leetcode] 47. Permutations II
  2. Asp.net MVC5 框架揭秘 S412 实例解析 – 绝妙的扩展 模式的胜利
  3. [我给Unity官方视频教程做中文字幕]beginner Graphics – Lessons系列之材质了解Materials
  4. number-of-segments-in-a-string
  5. net cookie操作
  6. [z]CAP原理和BASE思想
  7. PHP magic_quotes_gpc的详细使用方法
  8. VitamioBundle-master
  9. windows下nginx+tomcat分布式集群部署
  10. 【Hibernate步步为营】--双向关联一对一映射具体解释(一)
  11. 【基础】Attribute的妙用
  12. QC3.0充电标准
  13. C# 从网站下载图片
  14. 6.ST LINK 下调试异常
  15. nginx介绍(五) - 高可用
  16. [JZOJ5987] 仙人掌毒题
  17. eclipse中中文注释乱码怎么解决
  18. SQL Server 2008“备份集中的数据库备份与现有的数据库不同”解决方法
  19. Redis(二)持久化
  20. hdu 2642

热门文章

  1. 上下文管理器和else块
  2. mysql日常优化细节
  3. Django-CRM后台管理系统
  4. DOM元素
  5. 锁对象Lock-同步问题更完美的处理方式
  6. Java中boolean类型占用多少个字节?我说一个,面试官让我回家等通知
  7. js 继承的一个例子
  8. SpringMVC 理论
  9. linux下的开源NFC协议栈
  10. Android 电量优化