当前位置:首页 > 后端开发 > gateway跨域问题解决方法

gateway跨域问题解决方法

6个月前 (05-24)51

在解决之前在gateway中用了全局配置类跨域

package com.sky.wlmall.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.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;


@Configuration
public class CorsConfig {
     
    @Bean
    public CorsWebFilter corsFilter() {
     
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }
}

之后却报错了:
报错提示:Access to XMLHttpRequest at ‘http://localhost:88/api/sys/menu/nav?t=1646366047125’ from origin ‘http://localhost:8001’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: The value of the ‘Access-Control-Allow-Origin’ header in the response must not be the wildcard ‘*’ when the request’s credentials mode is ‘include’. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute

将上面的配置类修改为如下,并配置yaml就解决了:

package com.sky.wlmall.gateway.config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.web.reactive.config.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@SpringBootConfiguration
public class CorsConfig implements WebMvcConfigurer  {
     

    public void addCorsMappings(CorsRegistry corsRegistry){
     
        /**
         * 所有请求都允许跨域,使用这种配置就不需要
         * 在interceptor中配置header了
         */
        corsRegistry.addMapping("/**")
                .allowCredentials(true)
                .allowedOrigins("http://localhost:8001")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowedHeaders("*")
                .maxAge(3600);
    }

}
spring:
  cloud:
    gateway:
      globalcors:
        add-to-simple-url-handler-mapping: true
        cors-configurations:
          '[/**]':
            allowedOrigins:
              - "http://localhost:8001"
            allowedMethods:
              - "GET"
              - "POST"
              - "DELETE"
              - "PUT"
              - "OPTIONS"
            allowedHeaders: "*"
            allowCredentials: true
            maxAge: 360000

作者:sky~
来源链接:https://blog.csdn.net/Badman0726/article/details/123273687

标签: Gateway

“gateway跨域问题解决方法” 的相关文章

Gateway新一代路由网关

Gateway新一代路由网关

概述简介 Cloud中重要的组件就是网关,在1.x版本中采用的是Zuul网关; 但2.x版本中,zuul的升级一直跳票,SpringCloud...

gateway 实现接口日志保存

1.CachePostBodyFilter将request body 取出来重存 import io.netty.buffer.UnpooledByteBufAllocator;...

【微服务SpringCloud】:Gateway 网关

【微服务SpringCloud】:Gateway 网关

文章目录 1、什么是 API 网关? 2、什么是 Gateway ?...

网络配置的四大基本要素: IP + Netmask + Gateway + DNS

转载:https://blog.csdn.net/yuanbinquan/article/details/52963845 1.  IP IP地址(英语:Inter...

springcloud gateway 整合swagger3.0.0

springcloud gateway 整合swagger3.0.0

版本和说明 swagger:3.0.0 gateway微服务的使用方式和单应用没啥区别,只是多了设置右上角模块的操作。 以...

Gateway详解

Gateway详解

一、词汇表 路由:是网关基本的模块,分别为id、目标uri、一组谓词+过滤器一起组合而成,如果谓词匹配成功,则路由匹配成功。 谓...

spring gateway 笔记

spring gateway 笔记

  目录 路由定义 配置路由谓词 配置路由过滤器 自定义全局过滤器 自定义全局异常处理器 http请求超时配置 跨域...

gateway整合Hystrix详细教程

gateway整合Hystrix详细教程 一、首先maven依赖导入 <dependency>...

SpringCloud之Gateway统一网关

SpringCloud之Gateway统一网关

SpringCloud之Gateway统一网关 Gateway网关可以帮助处理一些校验的问题。并不是所有的请求都可以请求...