当前位置: 首页 > news >正文

Spring Boot中解决跨域问题(CORS)

1. 跨域介绍

首先解释什么是跨域,跨域就是前端和后端的端口号不同;会产生跨域问题,这里浏览器的保护机制(同源策略)。
同源策略:前端和后端的协议、域名、端口号三者都相同叫做同源。
我们看一下不同源:
VUE:http://localhost:8080
Spring: http://localhost:8081/list
当我们出现跨域问题,前端就会报一个错(篮框扩这那个):
在这里插入图片描述

2. 解决方法

上方就是不同源,两者的协议、域名相同,但是端口号不同;如何解决呢,使用Spring Boot解决,它提供三种方案:

  1. 直接在方法上方添加@CrossOrigin注解即可解决问题
	@CrossOrigin@RequestMapping("/getuserbyid")public UserInfo getUserById(Integer id) {if(id == null ) return null;return userService.getUserById(id);}
  1. 添加 CORS 过滤器
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;@Configuration
public class CorsConfig {@Beanpublic CorsFilter corsFilter() {CorsConfiguration corsConfiguration = new CorsConfiguration();corsConfiguration.setAllowCredentials(true); // 允许cookies跨域corsConfiguration.addAllowedHeader("*"); // 请求头字段corsConfiguration.addAllowedMethod("*"); // 方法corsConfiguration.addAllowedOrigin("*"); // 允许向该服务器提交请求的URI,*表示全部允许,自定义可以添加多个,在SpringMVC中,如果设成*,会自动转成当前请求头中的OriginUrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**",corsConfiguration); // 添加映射路径,以及参数return new CorsFilter(source);}
}
  1. 重写 WebMvcConfigurer 接口中的 addCorsMappings 方法
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {// 先设置映射registry.addMapping("/**").allowedOriginPatterns("*") // 允许向该服务器提交请求的URI,*表示全部允许,自定义可以添加多个,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin.allowCredentials(true) // 允许cookies跨域.allowedHeaders("*") // 请求头字段.allowedMethods("GET","POST") // 允许跨域的方法.maxAge(3600);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了}
}
http://www.lryc.cn/news/217498.html

相关文章:

  • 基于生成对抗网络的照片上色动态算法设计与实现 - 深度学习 opencv python 计算机竞赛
  • 广州华锐互动:数字孪生可视化制作软件有哪些亮点?
  • 设计模式之工厂模式讲解与案例
  • (免费领源码)php#MySQL软件测试文档管理系统28035-计算机毕业设计项目选题推荐
  • 05.Oracle数据库对象
  • 某国产中间件企业:提升研发安全能力,助力数字化建设安全发展
  • Servlet中主要的内置对象
  • STL-set和map
  • 【WinForm详细教程四】WinForm中的ProgressBar 、ImageList和ListView控件
  • 写一个简单实用的Excel工具类
  • C#中LINQtoObjects、LINQtoDataSet和LINQtoXML
  • k8s中 RBAC中,clusterrole,serviceaccount , rolebinding 是什么关系谁先谁后
  • 什么是文件安全
  • maven的settings.xml和pom.xml配置文件详解
  • YB2503HV 100V 3A SOP8内置MOS 高效率降压IC(昱灿)
  • Redis安装Linux
  • PCL点云处理(007)-Ransac
  • 有方N58 HTTP POST 请求连接 TDengine
  • 基于Python+Pygame实现一个滑雪小游戏
  • 【限制输入框值类型】自定义指令el-input输入类型限制,vue和html两个版本
  • 对一个金融风控测额公式的理解(1)
  • 【GEE】2、探索数据集
  • 开发一款直播弹幕游戏需要多少钱?
  • STM32F103C8T6第一天:认识STM32 标准库与HAL库 GPIO口 推挽输出与开漏输出
  • selenium元素定位 —— 提高篇 CSS定位元素
  • 隔离和非隔离电源的区别
  • C语言自定义数据类型
  • SoftwareTest5 - 你就只知道功能测试吗 ?
  • Uniapp 中,能够同时兼容H5、web、app、微信小程序的引入高德地图的语法格式
  • 基于nodejs+vue网上鲜花销售系统