欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring cloud gateway请求跨域问题解决方案

程序员文章站 2024-01-28 14:06:40
这篇文章主要介绍了spring cloud gateway请求跨域问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 代码...

这篇文章主要介绍了spring cloud gateway请求跨域问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

代码如下

@configuration
public class corsconfig implements globalfilter, ordered {
  private static final string all = "*";
  private static final string max_age = "18000l";

  @bean
  public routedefinitionlocator discoveryclientroutedefinitionlocator(discoveryclient discoveryclient,
                                    discoverylocatorproperties properties) {
    return new discoveryclientroutedefinitionlocator(discoveryclient, properties);
  }

  @bean
  public servercodecconfigurer servercodecconfigurer() {
    return new defaultservercodecconfigurer();
  }
   //添加请求头 
  @bean
  public webfilter corsfilter() {
    return (serverwebexchange ctx, webfilterchain chain) -> {
      serverhttprequest request = ctx.getrequest();
      if (!corsutils.iscorsrequest(request)) {
        return chain.filter(ctx);
      }
      httpheaders requestheaders = request.getheaders();
      serverhttpresponse response = ctx.getresponse();
      httpmethod requestmethod = requestheaders.getaccesscontrolrequestmethod();
      httpheaders headers = response.getheaders();
      headers.add(httpheaders.access_control_allow_origin, requestheaders.getorigin());
      headers.addall(httpheaders.access_control_allow_headers, requestheaders.getaccesscontrolrequestheaders());
      if (requestmethod != null) {
        headers.add(httpheaders.access_control_allow_methods, requestmethod.name());
      }
      headers.add(httpheaders.access_control_allow_credentials, "true");
      headers.add(httpheaders.access_control_expose_headers, all);
      headers.add(httpheaders.access_control_max_age, max_age);
      if (request.getmethod() == httpmethod.options) {
        response.setstatuscode(httpstatus.ok);
        return mono.empty();
      }
      return chain.filter(ctx);
    };
  }
  //将请求头中有多个值的去掉 这是该版本的一个bug
  @override
  public mono<void> filter(serverwebexchange exchange, gatewayfilterchain chain) {
    return chain.filter(exchange).then(mono.defer(() -> {
      exchange.getresponse().getheaders().entryset().stream()
          .filter(kv -> (kv.getvalue() != null && kv.getvalue().size() > 1))
          .filter(kv -> (kv.getkey().equals(httpheaders.access_control_allow_origin)
              || kv.getkey().equals(httpheaders.access_control_allow_credentials)))
          .foreach(kv -> {
            kv.setvalue(new arraylist<string>() {{
              add(kv.getvalue().get(0));
            }});
          });

      return chain.filter(exchange);
    }));
  }

  @override
  public int getorder() {
    // 指定此过滤器位于nettywriteresponsefilter之后
    // 即待处理完响应体后接着处理响应头
    return nettywriteresponsefilter.write_response_filter_order + 1;
  }
}

此处的spring-cloud-gateway的版本是2.1.3.release

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。