解决Spring Cloud Gateway获取body内容,不影响GET请求的操作
废话
这几天换了新工作,需要重新开发一套系统,技术选用SpringCloud。在对接终端接口的时候要做验签,就涉及到在网关做拦截器,然后取出BODY里面的数据。
网上找了几个方法,有的拿不到数据,有的拿到数据之后不支持GET请求了。没有一个合理的解决办法,最后想到在动态路由构建的时候可以指定METHOD,于是有了如下解决办法
解决
@Bean publicRouteLocatorvmRouteLocator(RouteLocatorBuilderbuilder){ returnbuilder.routes() .route(r->r.method(HttpMethod.POST).and() .readBody(Object.class,requestBody->{ //相当于缓存了body信息,在filter中可以这么获取exchange.getAttribute("cachedRequestBodyObject"); log.info("requestBodyis{}",requestBody); returntrue; }) .and().path("/terminal/**") .filters(f->f.filter(terminalSignFilter())) .uri("lb://TERMINAL-SERVICE") .order(0) .id("terminal-service") ) .route(r->r.method(HttpMethod.GET).and() .path("/terminal/**") .filters(f->f.filter(terminalSignFilter())) .uri("lb://TERMINAL-SERVICE") .order(1) .id("terminal-service") ) .build(); }
关键代码:
r.method(HttpMethod.POST)
r.method(HttpMethod.GET)
分别指定了不同请求METHOD对应的路由策略
在POST请求中需要缓存BODY信息,在Filter中便可以获取到
GET请求因为没有BODY,所以如果不指定GET的路由便会报错
可能会有更通用的方法,但是目前只想到这么多,以后有好的解决办法会继续更新
补充知识:SpringCloudGateway2.x获取body中的数据并缓存在请求中
场景
因为http请求中的body,读取过一次后就无法重新再读,但是我们希望网关项目中可以在所有filter中共享body中的内容。
写法
importlombok.extern.slf4j.Slf4j; importorg.springframework.cloud.gateway.filter.GatewayFilterChain; importorg.springframework.cloud.gateway.filter.GlobalFilter; importorg.springframework.core.Ordered; importorg.springframework.core.io.buffer.DataBuffer; importorg.springframework.core.io.buffer.DataBufferUtils; importorg.springframework.http.server.reactive.ServerHttpRequest; importorg.springframework.http.server.reactive.ServerHttpRequestDecorator; importorg.springframework.stereotype.Component; importorg.springframework.web.reactive.function.server.HandlerStrategies; importorg.springframework.web.reactive.function.server.ServerRequest; importorg.springframework.web.server.ServerWebExchange; importreactor.core.publisher.Flux; importreactor.core.publisher.Mono; @Component @Slf4j publicclassCacheBodyParamsFilterimplementsGlobalFilter,Ordered{ @Override publicMonofilter(ServerWebExchangeexchange,GatewayFilterChainchain){ if(ParamsUtil.logBody(exchange)){ returnDataBufferUtils.join(exchange.getRequest().getBody()) .flatMap(dataBuffer->{ byte[]bytes=newbyte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); DataBufferUtils.release(dataBuffer); Flux cachedFlux=Flux.defer(()->{ DataBufferbuffer=exchange.getResponse().bufferFactory().wrap(bytes); DataBufferUtils.retain(buffer); returnMono.just(buffer); }); ServerHttpRequestmutatedRequest=newServerHttpRequestDecorator(exchange.getRequest()){ @Override publicFlux getBody(){ returncachedFlux; } }; ServerWebExchangemutatedExchange=exchange.mutate().request(mutatedRequest).build(); returnServerRequest.create(mutatedExchange,HandlerStrategies.withDefaults().messageReaders()) .bodyToMono(String.class) .doOnNext(objectValue->{ //在此处,将body中的params值获取到,并存放在本次请求的attributes属性中,这样就可以在本次请求中的所有地方进行使用了 mutatedExchange.getAttributes().put(CommonConstant.PARAMS,ParamsUtil.buildParams(mutatedRequest,objectValue)); }).then(chain.filter(mutatedExchange)); }); } returnchain.filter(exchange); } @Override publicintgetOrder(){ returnOrdered.HIGHEST_PRECEDENCE; } }
以上这篇解决SpringCloudGateway获取body内容,不影响GET请求的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。