一、跨域分类
1、协议跨域
http://localhost:8080 访问 https://localhost:8080
2、端口跨域
http://localhost:8080 访问 http://localhost:9090
3、域名跨域
http://192.168.125.1:8080 访问 192.168.125.2:8080
二、解决方案
1、全局配置(添加跨域配置类)
思路:实现 WebMvcConfigurer 接口,然后实现接口中的 addCorsMappings() 方法。
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* CORS配置,解决前后端跨域问题
*/
@Configuration
public class CorsConfig implements WebMvcConfigure
SpringBoot配置解决CORS跨域问题

本文介绍了跨域的三种类型:协议、端口和域名跨域,并详细讲解了SpringBoot中如何通过全局配置和局部注解两种方式解决CORS跨域问题。
最低0.47元/天 解锁文章
1109

被折叠的 条评论
为什么被折叠?



