首页 > 作文

SpringCloud Gateway读取Request Body方式

更新时间:2023-04-06 03:56:09 阅读: 评论:0

gateway读取request body

我们使用springcloud gateway做微服务网关的时候,经常需要在过滤器filter中读取到post请求中的body内容进行日志记录、签名验证、权限验证等操作。我们知道,request的body是只能读取一次的,如果直接通过在filter中读取,而不封装回去回导致后面的服务无法读取数据。

springcloud gateway内部提供了一个断言工厂类readbodypredicatefactory,这个类实现了读取request的body内容并放入缓存,我们可以通过从缓存中获取body内容来实现我们的目的。

分析readbodypredicatefactory

public asyncpredicate<rverwebexchange> applyasync(readbodypredicatefactory.config config) {    return (exchange) -> {      class inclass = config.getinclass();      object cachedbody = exchange.getattribute("cachedrequestbodyobject");      if (cachedbody != null) {        try {          boolean test = config.predicate.test(cachedbody);          exchange.getattributes().put("read_body_predicate_test_attribute", test);          return mono.just(test);        } catch (classcastexception var7) {          if (logger.isdebugenabled()) {            logger.debug("predicate test failed becau class in predicate does not match the cached body object", var7);          }          return mono.just(fal);        }      } el {        return databufferutils.join(exchange.getrequest().getbody()).flatmap((databuffer) -> {          databufferutils.retain(databuffer);          final flux<databuffer>四年级美术教案; cachedflux = flux.defer(() -> {            return flux.just(databuffer.slice(0, databuffer.readablebytecount()));          });          rverhttprequest mutatedrequest = new rverhttprequestdecorator(exchange.getrequest()) {            public flux<databuffer> getbody() {              return cachedflux;            }          };          return rverrequest.create(exchange.mutate().request(mutatedrequest).build(), messagereaders).bodytomono(inclass).doonnext((objectvalue) -> {            exchange.getattributes().put("cachedrequestbodyobject", objectvalue);            exchange.getattributes().put("cachedrequestbody", cachedflux);          }).map((objectvalue) -> {            return config.predicate.test(objectvalue);          });        });      }    };  }

通过查看readbodypredicatefactory内部实现,我们可以看到,该工厂类将request body内容读取后存放在 exchange的cachedrequestbodyobject中。

那么我们可以通过代码:

exchange.getattribute(“cachedrequestbodyobject”); //将body内容取出来

知道如何取body内容后,我们只需将该工厂类注册到yml配置文件中的predicates,然后从filter中获取即可。

配置readbodypredicatefactory

查看readbodypredicatefactory关于配置的代码:

public <t> readbodypredicatefactory.config tpredicate(class<t> inc中国大运河lass, predicate<t> predicate) {      this.tinclass(inclass);      this.predicate = predicate;      return this;    }

配置该工厂类需要两个参数:

inclass:接收body内容的对象class,我们用字符串接收,配置string即可。predicate:predicate的接口实现类,我们自定义一个predicate的实现类即可。

自定义predicate实现,并注册bean。

  /**  * 用于readbody断言,可配置到yml  * @return  */  @bean  public predicate bodypredicate(){    return new predicate() {      @override      public boolean test(object o) {        return true;      }    };  }

两个参数都有了,直接在yml中配置:

    predicates:    - path=/card/api/**    - name: readbodypredicatefactory #使用readbodypredicatefactory断言,将body读入缓存     args:      inclass: '#{t(string)}'      predicate: '#{@bodypredicate}' #注入实现predicate接口类

编写自定义gatewayfilterfactory

编写自己的过滤器工厂类,读取缓存的body内容,并支持在配置文件中配置。

public class readbodygatewayfilterfactory    extends abstractgatewayfilterfactory<readbodygatewayfilterfactory.config> {  private logger logger = loggerfactory.getlogger(readbodygatewayfilterfactory.class);  private static final string cache_request_body_object_key = "cachedrequestbodyobject";  public readbodygatewayfilterfactory() {    super(config.class);  }  @override  public gatewayfilter apply(config config) {    return ((exchange, chain) -> {      //利用readbodypredicatefactory断言,会将body读入exchange的cachedrequestbodyobject中      object分米的符号 requestbody = exchange.getattribute(cache_request_body_object_key);      logger.info("request body is:{}", requestbody);      return chain.filter(exchange);    });  }  @override  public list<string> shortcutfieldorder() {    return arrays.aslist("withparams");//将参数放入  }  public static class config {    private boolean withparams;//接收配置的参数值,可以随便写    public boolean iswithparams() {      return withparams;    }    public void twithparams(boolean withparams) {      this.withparams = withparams;    }  }}

将readbodygatewayfilterfactory工程类在容器中注入。

  /**  * 注入readbody过滤器  * @return  */  @bean  public readbodygatewayfilterfactory readbodygatewayfilterfactory() {    return new readbodygatewayfilterfactory();  }

到此,我们的filter类也可以在yml配置文件中直接配置使用了。

完整的yml配置

   - id: bod有谁y_route #读取post中的body路由    order: 5    uri: lb://api-card    filters:    - readbody=true #使用自定义的过滤器工厂类,读取request body内容    predicates:    - path=/card/api/**    - name: readbodypredicatefactory #使用readbodypredicatefactory断言,将body读入缓存     args:      inclass: '#{t(string)}'      predicate: '#{@bodypredicate}' #注入实现predicate接口类

ok,以上是通过readbodypredicatefactory这个类读取到request body内容。

另外springcloud gateway内部还提供了modifyrequestbodygatewayfilterfactory类用于修改body内容,既然能修改,自然也能获取body,大家可自行去研究。

gateway自定义filter获取body的数据为空

最近在使用springcloud gateway进行网关的开发,我使用的版本是:springboot的2.3.4.relea+springcloud的hoxton.sr8,在自定义过滤器时需要获取rverhttprequest中body的数据,发现一直无法获取到数据,经过各种百度、谷歌,再加上自己的实践,终于找到解决方案:

首先创建一个全局过滤器把body中的数据缓存起来

package com.cloudpath.gateway.portal.filter;import lombok.extern.slf4j.slf4j;import org.springframework.cloud.gateway.filter.gatewayfilterchain;import org.springframework.cloud.gateway.filter.globalfilter;import org.springframework.core.ordered;import org.springframework.core.io.buffer.databuffer;import org.springframework.core.io.buffer.databufferutils;import org.springframework.http.rver.reactive.rverhttprequest;import org.springframework.http.rver.reactive.rverhttprequestdecorator;import org.springframework.stereotype.component;import org.springframework.web.rver.rverwebexchange;import reactor.core.publisher.flux;import reactor.core.publisher.mono;/*** @author mazhen* @classname cachebodyglobalfilter* @description 把body中的数据缓存起来* @date 2020/10/28 18:02*/@slf4j@componentpublic class cachebodyglobalfilter implements ordered, globalfilter {  // public static final string cache_request_body_object_key = "cachedrequestbodyobject";  @override  public mono<void> filter(rverwebexchange exchange, gatewayfilterchain chain) {    if (exchange.getrequest().getheaders().getcontenttype() == null) {      return chain.filter(exchange);    } el {      return databufferutils.join(exchange.getrequest().getbody())          .flatmap(databuffer -> {            databufferutils.retain(databuffer);            flux<databuffer> cachedflux = flux                .defer(() -> flux.just(databuffer.slice(0, databuffer.readablebytecount())));            rverhttprequest mutatedrequest = new rverhttprequestdecorator(                exchange.getrequest()) {              @override              public flux<databuffer> getbody() {                return cachedflux;              }            };            //exchange.getattributes().put(cache_request_body_object_key, cachedflux);            return chain.filter(exchange.mutate().request(mutatedrequest).build());          });    }  }  @override  public int getorder() {    return ordered.highest_precedence;  }}

cachebodyglobalfilter这个全局过滤器的目的就是把原有的request请求中的body内容读出来,并且使用rverhttprequestdecorator这个请求装饰器对request进行包装,重写getbody方法,并把包装后的请求放到过滤器链中传递下去。这样后面的过滤器中再使用exchange.getrequest().getbody()来获取body时,实际上就是调用的重载后的getbody方法,获取的最先已经缓存了的body数据。这样就能够实现body的多次读取了。

值得一提的是,这个过滤器的order设置的是ordered.highest_precedence,即最高优先级的过滤器。优先级设置这么高的原因是某些系统内置的过滤器可能也会去读body,这样就会导致我们自定义过滤器中获取body的时候报body只能读取一次这样的错误如下:

java.lang.illegalstateexception: only one connection receive subscriber allowed.
at reactor.ipc.netty.channel.fluxreceive.startreceiver(fluxreceive.java:279)
at reactor.ipc.netty.channel.fluxreceive.lambda$subscribe$2(fluxreceive.java:129)
at

所以,必须把cachebodyglobalfilter的优先级设到最高。

在自定义的过滤器中尝试获取body中的数据

package com.cloudpath.iam.gateway.customerfilter;import com.cloudpath.iam.gateway.utils.filterrequestresponutil;import lombok.extern.slf4j.slf4j;import org.springframework.cloud.gateway.filter.gatewayfilter;import org.springframework.cloud.gateway.filter.factory.abstractgatewayfilterfactory;import org.springframework.core.io.buffer.databuffer;import org.springframework.http.rver.reactive.rverhttprequest;import org.springframework.stereotype.component;import reactor.core.publisher.flux;import java.util.arrays;import java.util.list;/*** @author by mazhen* @classname testgatewayfilterfactory* @description 自定义过滤器获取body中的数据* @date 2020/10/27 14:38*/@component@slf4jpublic class testgatewayfilterfactory extends abstractgatewayfilterfactory<testgatewayfilterfactory.config> {  @override  public list<string> shortcutfieldorder() {    return arrays.aslist("enabled");  }  public testgatewayfilterfactory() {    super(config.class);    log.info("loaded testgatewayfilterfactory");  }  @override  public gatewayfilter apply(config config) {    return (exchange, chain) -> {      if (!config.inabled()) {        return chain.filter(exchange);      }      if (null != exchange) {        rverhttprequest httprequest = exchange.getrequest();          try {            flux<databuffer> databufferflux = httprequest.getbody();            //获取body中的数据            string body = filterrequestresponutil.resolvebodyfromrequest(databufferflux);            log.info("body:{}",body);          } catch (exception e) {            log.error("异常:",e);            return chain.filter(exchange);          }      }      return chain.filter(exchange);    };  }  public static class config {    /**    * 控制是否开启统计    */    private boolean enabled;    public config() {    }    public boolean inabled() {      return enabled;    }    public void tenabled(boolean enabled) {      this.enabled = enabled;    }  }}

解析body的工具类

package com.cloudpath.iam.gateway.utils;import org.springframework.core.io.buffer.databuffer;import org.springframework.core.io.buffer.databufferuti汉文帝汉景帝ls;import reactor.core.publisher.flux;import java.nio.charbuffer;import java.nio.chart.standardcharts;import java.util.concurrent.atomic.atomicreference;import java.util.regex.matcher;import java.util.regex.pattern;/*** @author mazhen* @classname filterheadersutil* @description 过滤器请求/响应工具类* @date 2020/10/29 9:31*/public final class filterrequestresponutil {  /**  * spring cloud gateway 获取post请求的body体  * @param body  * @return  */  public static string resolvebodyfromrequest( flux<databuffer> body){    atomicreference<string> bodyref = new atomicreference<>();    // 缓存读取的request body信息    body.subscribe(databuffer -> {      charbuffer charbuffer = standardcharts.utf_8.decode(databuffer.asbytebuffer());      databufferutils.relea(databuffer);      bodyref.t(charbuffer.tostring());    });    //获取request body    return bodyref.get();  }  /**  * 读取body内容  * @param body  * @return  */  public static string resolvebodyfromrequest2( flux<databuffer> body){    stringbuilder sb = new stringbuilder();    body.subscribe(buffer -> {      byte[] bytes = new byte[buffer.readablebytecount()];      buffer.read(bytes);      databufferutils.relea(buffer);      string bodystring = new string(bytes, standardcharts.utf_8);      sb.append(bodystring);    });    return formatstr(sb.tostring());  }  /**  * 去掉空格,换行和制表符  * @param str  * @return  */  private static string formatstr(string str){    if (str != null && str.length() > 0) {      pattern p = pattern.compile("\\s*|\t|\r|\n");      matcher m = p.matcher(str);      return m.replaceall("");    }    return str;  }}

解析body的内容,网上普遍是上面的两种方式,亲测resolvebodyfromrequest方法解析body中的数据,没有1024字节的限制。

ps:我传的参数有1万多字节。。。。。。。

大家可以按需所选。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持www.887551.com。

本文发布于:2023-04-06 03:56:08,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/e37af5d680750999303aacc9191132f2.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:SpringCloud Gateway读取Request Body方式.doc

本文 PDF 下载地址:SpringCloud Gateway读取Request Body方式.pdf

标签:过滤器   自定义   内容   缓存
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图