解决SpringCloud Gateway网关跨域问题

10 篇文章 0 订阅
6 篇文章 0 订阅

前后端分离项目,非同源地址会发生跨域问题。因此需要解决跨域问题

总的来说,解决方案:

  • NGINX解决跨域问题
  • 在网关处解决跨域问题

这里记录自己在网关处解决跨域的方法:

技术:Springcloud Gateway

springboot版本:2.1.4

方法1:

在application.xml:

spring:
  cloud: # 跨域配置
    gateway:
      globalcors:
        cors-configurations:
          '[/**]': # 匹配所有请求
            allowedOrigins: "*" #跨域处理 允许所有的域
            allowedMethods: # 支持的方法
              - GET
              - POST
              - PUT
              - DELETE

这样可以解决跨域的问题。

方法2:
在网关处新增配置类:

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.util.pattern.PathPatternParser;

/**
 * 跨域允许
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();


        config.setAllowCredentials(true); // 允许cookies跨域
        config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
        config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        config.addAllowedMethod("OPTIONS");// 允许提交请求的方法类型,*表示全部允许
        config.addAllowedMethod("HEAD");
        config.addAllowedMethod("GET");
        config.addAllowedMethod("PUT");
        config.addAllowedMethod("POST");
        config.addAllowedMethod("DELETE");
        config.addAllowedMethod("PATCH");

        org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource source =
                new org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);

        return new CorsWebFilter(source);
    }


}

可以解决跨域问题。

记录@CrossOrigin发生的问题

在各个微服务对应的Controller,在微服务的Controller中,一般会加上注解@CrossOrigin

当在网关处已经配置了跨域解决时,此时前端会提示:but only one is allowed.

即:重复处理跨域请求

因此只需要去掉这个@CrossOrigin即可

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Cloud GatewaySpring Cloud生态圈中的一个API网关,它提供了一种构建微服务架构的解决方案。跨域是Web开发中常见的问题,特别是在微服务架构中,不同服务之间需要进行跨域访问。本篇文章将介绍使用Spring Cloud Gateway实现跨域的方法。 首先,需要在Spring Cloud Gateway中添加Corsconfiguration Bean,代码如下: @Bean public CorsConfigurationSource corsConfigurationSource() { CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowedOrigins(Arrays.asList("*")); configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE")); configuration.setAllowedHeaders(Arrays.asList("Content-Type", "Authorization")); UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", configuration); return source; } 然后,在application.yml文件中添加跨域配置,代码如下: spring: cloud: gateway: globalcors: corsConfigurations: '[/**]': allowedOrigins: "*" allowedMethods: - GET - POST - PUT - DELETE allowedHeaders: - "Content-Type" - "Authorization" allowCredentials: true maxAge: 3600 这里设置了允许的来源,方法和头部,还指定了是否允许发送cookie信息,以及最大响应时间。此外,您也可以通过使用代码配置来自定义跨域规则,具体请参考Spring Cloud Gateway官方文档。 最后,在需要进行跨域访问的路由前添加跨域过滤器,代码如下: @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("path_route", r -> r.path("/hello") .filters(f -> f.addRequestHeader("Hello", "World") .addFilter(new CrossOriginFilter())) .uri("http://localhost:8081")) .build(); } 在这个例子中,我们将拦截/hello路由,并添加一个跨域过滤器。 总结来说,使用Spring Cloud Gateway实现跨域需要以下步骤:添加Corsconfiguration Bean,配置application.yml文件,添加跨域过滤器。这样,在微服务架构中实现跨域问题就变得容易了。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值