Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringCloud Gateway gets request body response write

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

If you don't say much, just put in the code.

/ * @ author liuhanling * @ create 2018-12-14 20:01 * @ desc * / @ Slf4j@Componentpublic class RequestBodyFilter implements GatewayFilter, Ordered {private static final String CACHE_REQUEST_BODY_OBJECT_KEY = "cachedRequestBodyObject"; @ Override @ SuppressWarnings ("unchecked") public Mono filter (ServerWebExchange exchange, GatewayFilterChain chain) {Class inClass = String.class; ServerRequest serverRequest = new DefaultServerRequest (exchange); ServerHttpRequest request = exchange.getRequest () / record only http requests (including https) String schema = request.getURI () .getScheme (); if ((! "http" .equals (schema) & &! "https" .equals (schema)) {return chain.filter (exchange);} String contentType = request.getHeaders (). GetFirst ("Content-Type"); String upload = request.getHeaders (). GetFirst ("upload") / / the get method gets the parameter if ("GET" .equals (request.getMethod (). Name () {if (! request.getQueryParams () .isEmpty ()) {Map map = new HashMap (); request.getQueryParams () .toSingleValueMap () .forEach ((key, val)-> map.put (key, getURLDecoder (val) String param = new Gson (). ToJson (map); exchange.getAttributes (). Put (CACHE_REQUEST_BODY_OBJECT_KEY, param);} return chain.filter (exchange);} / / do not read body if without content type (contentType = = null | | contentType.length () = = 0) {return chain.filter (exchange) } / / File upload does not read body if ("true" .equals (upload)) {return chain.filter (exchange);} Mono modifiedBody = serverRequest.bodyToMono (inClass) .flatMap (o-> {log.debug ("requestBody: {}", o); exchange.getAttributes () .put (CACHE_REQUEST_BODY_OBJECT_KEY, o) Return Mono.justOrEmpty (o);}); BodyInserter bodyInserter = BodyInserters.fromPublisher (modifiedBody, inClass); HttpHeaders headers = new HttpHeaders (); headers.putAll (exchange.getRequest (). GetHeaders ()); CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage (exchange, headers) Return bodyInserter.insert (outputMessage, new BodyInserterContext ()) .then (Mono.defer (()-> {ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator (exchange.getRequest ()) {@ Override public HttpHeaders getHeaders () {long contentLength = headers.getContentLength ()) HttpHeaders httpHeaders = new HttpHeaders (); httpHeaders.putAll (super.getHeaders ()); if (contentLength > 0) {httpHeaders.setContentLength (contentLength) } else {/ / TODO: this causes a 'HTTP/1.1 411 Length Required' on httpbin.org httpHeaders.set (HttpHeaders.TRANSFER_ENCODING, "chunked");} return httpHeaders } @ Override public Flux getBody () {return outputMessage.getBody ();}}; return chain.filter (exchange.mutate () .request (decorator) .build ()) });} @ Override public int getOrder () {return 0;} / * URL transcoding * @ param val * @ return * / private String getURLDecoder (String val) {try {return URLDecoder.decode (val, "utf-8") } catch (Exception e) {log.error ("getURLDecoder error", e);} return val;}}

Rewrite ResponseBody

Create 2018-12-27 10:39 * @ desc * / @ Component@Slf4jpublic class ResponseBodyFilter implements GatewayFilter, Ordered {@ Override @ SuppressWarnings ("unchecked") public Mono filter (ServerWebExchange exchange, GatewayFilterChain chain) {ServerHttpResponseDecorator responseDecorator = new ServerHttpResponseDecorator (exchange.getResponse ()) {@ Override public Mono writeWith (Publisher)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report