| | |
| | | //package cn.mb.cloud.gateway.filter; |
| | | // |
| | | //import com.alibaba.fastjson.JSON; |
| | | //import lombok.extern.slf4j.Slf4j; |
| | | //import org.reactivestreams.Publisher; |
| | | //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.DataBufferFactory; |
| | | //import org.springframework.core.io.buffer.DataBufferUtils; |
| | | //import org.springframework.http.HttpStatus; |
| | | //import org.springframework.http.server.reactive.ServerHttpRequest; |
| | | //import org.springframework.http.server.reactive.ServerHttpResponse; |
| | | //import org.springframework.http.server.reactive.ServerHttpResponseDecorator; |
| | | //import org.springframework.stereotype.Component; |
| | | //import org.springframework.web.server.ServerWebExchange; |
| | | //import reactor.core.publisher.Flux; |
| | | //import reactor.core.publisher.Mono; |
| | | // |
| | | //import java.nio.charset.StandardCharsets; |
| | | //import java.util.ArrayList; |
| | | //import java.util.List; |
| | | // |
| | | ///** |
| | | // * 日志拦截器 |
| | | // */ |
| | | //@Slf4j |
| | | //@Component |
| | | //public class ResponseParamFilter implements GlobalFilter, Ordered { |
| | | // |
| | | // @Override |
| | | // public int getOrder() { |
| | | // return -1; |
| | | // } |
| | | // |
| | | // @Override |
| | | // public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
| | | // try { |
| | | // ServerHttpRequest request = exchange.getRequest(); |
| | | // String uri = request.getURI().getRawPath(); |
| | | // ServerHttpResponse originalResponse = exchange.getResponse(); |
| | | // DataBufferFactory bufferFactory = originalResponse.bufferFactory(); |
| | | // HttpStatus statusCode = originalResponse.getStatusCode(); |
| | | // if(statusCode == HttpStatus.OK){ |
| | | // ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) { |
| | | // @Override |
| | | // public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { |
| | | // if (body instanceof Flux) { |
| | | // Flux<? extends DataBuffer> fluxBody = Flux.from(body); |
| | | // return super.writeWith(fluxBody.map(dataBuffer -> { |
| | | // byte[] content = new byte[dataBuffer.readableByteCount()]; |
| | | // dataBuffer.read(content); |
| | | // DataBufferUtils.release(dataBuffer);//释放掉内存 Release memory |
| | | // // 构建日志 build Journal |
| | | // StringBuffer sb = new StringBuffer(); |
| | | // sb.append(" 响应操作 -> " + uri + " || 响应参数-> {} \n"); |
| | | // |
| | | // List<Object> rspArgs = new ArrayList<>(); |
| | | // rspArgs.add(originalResponse.getStatusCode()); |
| | | // String data = new String(content, StandardCharsets.UTF_8);//data |
| | | // sb.append(data); |
| | | // log.info(sb.toString(), rspArgs.toArray()); |
| | | // return bufferFactory.wrap(content); |
| | | // })); |
| | | // } else { |
| | | // log.error("响应操作 -> " + uri + " || 响应参数-> {} \n", getStatusCode()); |
| | | // } |
| | | // return super.writeWith(body); |
| | | // } |
| | | // }; |
| | | // return chain.filter(exchange.mutate().response(decoratedResponse).build()); |
| | | // } |
| | | // return chain.filter(exchange);//降级处理返回数据 Downgrade processing returns data |
| | | // }catch (Exception e){ |
| | | // log.error("gateway log exception.\n" + e); |
| | | // return chain.filter(exchange); |
| | | // } |
| | | // } |
| | | //} |
| | | package cn.mb.cloud.gateway.filter; |
| | | |
| | | import com.alibaba.fastjson.JSON; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.reactivestreams.Publisher; |
| | | 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.DataBufferFactory; |
| | | import org.springframework.core.io.buffer.DataBufferUtils; |
| | | import org.springframework.http.HttpStatus; |
| | | import org.springframework.http.server.reactive.ServerHttpRequest; |
| | | import org.springframework.http.server.reactive.ServerHttpResponse; |
| | | import org.springframework.http.server.reactive.ServerHttpResponseDecorator; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.server.ServerWebExchange; |
| | | import reactor.core.publisher.Flux; |
| | | import reactor.core.publisher.Mono; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 日志拦截器 |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class ResponseParamFilter implements GlobalFilter, Ordered { |
| | | |
| | | @Override |
| | | public int getOrder() { |
| | | return -1; |
| | | } |
| | | |
| | | @Override |
| | | public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { |
| | | try { |
| | | ServerHttpRequest request = exchange.getRequest(); |
| | | String uri = request.getURI().getRawPath(); |
| | | ServerHttpResponse originalResponse = exchange.getResponse(); |
| | | DataBufferFactory bufferFactory = originalResponse.bufferFactory(); |
| | | HttpStatus statusCode = originalResponse.getStatusCode(); |
| | | if(statusCode == HttpStatus.OK){ |
| | | ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) { |
| | | @Override |
| | | public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) { |
| | | if (body instanceof Flux) { |
| | | Flux<? extends DataBuffer> fluxBody = Flux.from(body); |
| | | return super.writeWith(fluxBody.map(dataBuffer -> { |
| | | byte[] content = new byte[dataBuffer.readableByteCount()]; |
| | | dataBuffer.read(content); |
| | | DataBufferUtils.release(dataBuffer);//释放掉内存 Release memory |
| | | // 构建日志 build Journal |
| | | StringBuffer sb = new StringBuffer(); |
| | | sb.append(" 响应操作 -> " + uri + " || 响应参数-> {} \n"); |
| | | |
| | | List<Object> rspArgs = new ArrayList<>(); |
| | | rspArgs.add(originalResponse.getStatusCode()); |
| | | String data = new String(content, StandardCharsets.UTF_8);//data |
| | | sb.append(data); |
| | | log.info(sb.toString(), rspArgs.toArray()); |
| | | return bufferFactory.wrap(content); |
| | | })); |
| | | } else { |
| | | log.error("响应操作 -> " + uri + " || 响应参数-> {} \n", getStatusCode()); |
| | | } |
| | | return super.writeWith(body); |
| | | } |
| | | }; |
| | | return chain.filter(exchange.mutate().response(decoratedResponse).build()); |
| | | } |
| | | return chain.filter(exchange);//降级处理返回数据 Downgrade processing returns data |
| | | }catch (Exception e){ |
| | | log.error("gateway log exception.\n" + e); |
| | | return chain.filter(exchange); |
| | | } |
| | | } |
| | | } |