介绍

浏览器跨域请求_跨域

 浏览器跨域请求_sed_02

 浏览器跨域请求_post请求_03

 浏览器跨域请求_响应头_04

 post请求会先发一个option的预请求

浏览器跨域请求_sed_05

解决方法

浏览器跨域请求_spring_06

 浏览器跨域请求_响应头_07

在网关使用filter 在响应返回之前 添加响应头

配置允许跨域请求

package com.luyi.gulimall.gateway.config;

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


@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        // 跨域的配置类  跨域的配置写在这个类
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        // 配置跨域
        //  允许哪些头 进行跨域  设置全部
        corsConfiguration.addAllowedHeader("*");
        //  允许哪些方法进行跨域 设置全部
        corsConfiguration.addAllowedMethod("*");
        //  允许哪些请求来源进行跨域  设置全部
        corsConfiguration.addAllowedOrigin("*");
        //  设置允许待Cookie的请求进行跨域
        corsConfiguration.setAllowCredentials(true);
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        return  new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}

现在发现发送了两个请求
第一个是option 预检请求 200了

浏览器跨域请求_跨域_08

 响应头多了这几个字段

浏览器跨域请求_post请求_09

 第二个是真实请求 带了请求数据

浏览器跨域请求_跨域_10