gateway跨域问题解决方法

在解决之前在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
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Gateway是Spring Cloud生态系统中的一个重要组件,用于构建微服务网关。在实际应用中,由于微服务的数量和种类可能很多,网关的主要功能就是将各个服务整合起来,并提供统一的入口。然而,在跨域请求方面,Spring Gateway也存在一些问题,需要进行相应的解决跨域请求是指在一个域下的前端页面请求另一个域的资源。由于浏览器的同源策略,通常情况下这种请求是不允许的,因此需要进行一些特殊的处理。 在Spring Gateway中,跨域请求可以通过配置路由(Route)来解决。具体来说,可以在Route的配置中添加一个CorsConfiguration对象,用于设置允许跨域的规则。例如,可以设置允许的来源(Allow-Origin)、允许的请求头(Allow-Headers)等。 下面是一个使用了CorsConfiguration的路由配置示例: ``` spring: cloud: gateway: routes: - id: myRoute uri: http://localhost:8080 predicates: - Path=/api/** filters: - RewritePath=/api/(?<path>.*), /$\{path} - name: CorsFilter args: allowedOrigins: http://*.example.com, http://localhost:8080 allowedMethods: GET, POST allowedHeaders: header1, header2, header3 exposedHeaders: header1, header2 maxAge: 3600 ``` 在这个配置示例中,表示通过路由myRoute(对应/api/**路径)请求http://localhost:8080时,允许的来源为http://*.example.com和http://localhost:8080,允许的请求方法为GET和POST,允许的请求头为header1、header2和header3,暴露的响应头为header1和header2,缓存时间为3600秒。 总的来说,在Spring Gateway解决跨域请求问题需要通过路由配置来设置CorsConfiguration。通过合理设置允许来源、允许方法和允许请求头等参数,可以解决大部分跨域请求问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值