如何解决跨域问题

一、什么是跨域

是浏览器的的同源策略造成的,是浏览器对javascript施加的安全限制。

同源策略:是指协议,域名,端口都要相同,其中有一个不同都会产生跨域;

二、简单请求和非简单请求

简单请求包括 get、post、head

非简单请求会在发送实际请求之前先发一个预检请求

预检请求:
在这里插入图片描述
预检请求的响应头可以看到实际请求的具体信息

在这里插入图片描述

三、如何解决跨域

这里我采用的是在网关中进行配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GuLiMallCorsWebFilter {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许哪些头跨域
        corsConfiguration.addAllowedHeader("*");
        // 允许哪些方式跨域   get  post  delete 等方式
        corsConfiguration.addAllowedMethod("*");
        //允许哪些请求来源跨域    *  任意来源
        corsConfiguration.addAllowedOrigin("*");
        // 是否允许携带cooker跨域
        corsConfiguration.setAllowCredentials(true);
        //注册跨越配置       /**配置请求路径
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}

再 次访问

在这里插入图片描述
发现响应头有两个请求地址 http://localhost:8001

可能是配置了多个跨域,关掉其他配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值