zoukankan      html  css  js  c++  java
  • 浏览器跨域请求

    介绍

     

     

     

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

    解决方法

     

    在网关使用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了

     响应头多了这几个字段

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

    摘自:https://blog.csdn.net/weixin_43691773/article/details/110304955

  • 相关阅读:
    Go之运算符
    前端开发之工具库
    MVC与MVVM
    开发工具之Vscode编辑器
    常用名词汇总
    python常见错误总结
    Python之常用第三方库总结
    PHP程序员的成长路线
    web 应用常见安全漏洞
    redis和memcached的区别详解
  • 原文地址:https://www.cnblogs.com/lyh233/p/15038341.html
Copyright © 2011-2022 走看看