网关
网关运行分析报告
网关运行分析报告 - 2025-02-15
网关运行分析报告 - 2025-02-22
网关运行分析报告 - 2025-02-28
1.shenyu网关内外网使用
2.shenyu网关的具体使用
记一次网关线上问题之icsp访问ERP
再次思考多套ak/sk同时访问同一资源路径问题
网关接入说明
网关接入说明补充
网关分配各系统命名
网关BUG及二开
网关管理端访问地址
修改requestBody与responseBody
shenyu工程理解
获取requestBody异步问题
shenyu工程部署
排查sign插件报错500问题
shenyu数据结构设计
shenyu网关请求过程
shenyu自定义插件
记一次网关线上问题----网关无法对外提供服务
网关插件更新报错问题
网关中grayTag的使用
Exceeded limit on max bytes to buffer : 262144
网关中Divide插件中Selector中Handler中配置丢失问题
网关请求下游系统时长记录
通用测试:获取网关的sign值
网关分发主数据设想方案
铁骑主数据分发机制完善
27.GTMS&ITMS与gateway的关系
28.ZPI与gateway的关系
26.网关验签场景
29.PC端空值,服务端正常请求
30.网关requestMaxSize值
31.跨系统跨语言日志链路追踪
32.网关异步分发讨论
33.网关LoggingConsole丢失日志排查
34.网关升级shenyu-admin
35.网关异步分发插件 - 接入文档
本文档使用「觅思文档专业版」发布
-
+
首页
修改requestBody与responseBody
今天遇到个需求,业务系统(简称ABC)需要对requestBody加密与responseBody解密。网关实现如下 ## 1.获取requestBody ``` @Override protected Mono<Void> doExecute(ServerWebExchange exchange, ShenyuPluginChain chain, SelectorData selectorData, RuleData ruleData) { List<HttpMessageReader<?>> MESSAGE_READERS = HandlerStrategies.builder().build().messageReaders(); ServerRequest serverRequest = ServerRequest.create(exchange, MESSAGE_READERS); Mono<String> mono = serverRequest.bodyToMono(String.class).switchIfEmpty(Mono.defer(() -> Mono.just(""))).flatMap(originalBody -> { LOGGER.info("get body data success data:{}", originalBody); //process entity String modify = "lxl"; return Mono.just(modify); }); } ``` ## 2.获取responseBody ``` @Override protected Mono<Void> doExecute(ServerWebExchange exchange, ShenyuPluginChain chain, SelectorData selectorData, RuleData ruleData) { ShenyuContext shenyuContext = (ShenyuContext)exchange.getAttribute("context"); assert shenyuContext != null; BankResponseSelectorHandle selectorDataHandle = BankResponsePluginSelectorDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(selectorData)); return chain.execute(exchange.mutate().response(new ModifyResponseDecorator(exchange, selectorDataHandle)).build()); } ...... ...... ...... static class ModifyResponseDecorator extends ServerHttpResponseDecorator { private final ServerWebExchange exchange; private final BankResponseSelectorHandle selectorHandle; ModifyResponseDecorator(ServerWebExchange exchange, BankResponseSelectorHandle selectorHandle) { super(exchange.getResponse()); this.exchange = exchange; this.selectorHandle = selectorHandle; } @NonNull public Mono<Void> writeWith(@NonNull Publisher<? extends DataBuffer> body) { ClientResponse clientResponse = this.buildModifiedResponse(body); Mono<byte[]> modifiedBody = clientResponse.bodyToMono(byte[].class).flatMap((originalBody) -> { return Mono.just(this.modifyBody(originalBody)); }); return ResponseUtils.writeWith(clientResponse, this.exchange, modifiedBody, byte[].class); } private ClientResponse buildModifiedResponse(Publisher<? extends DataBuffer> body) { HttpHeaders httpHeaders = new HttpHeaders(); httpHeaders.addAll(this.getHeaders()); ClientResponse clientResponse = ResponseUtils.buildClientResponse(this.getDelegate(), body); HttpStatus statusCode = clientResponse.statusCode(); this.getDelegate().getHeaders().clear(); this.getDelegate().getHeaders().putAll(httpHeaders); int rowStatusCode = clientResponse.rawStatusCode(); ExchangeStrategies strategies = clientResponse.strategies(); return ClientResponse.create(statusCode, strategies).rawStatusCode(rowStatusCode).headers((headers) -> { headers.addAll(httpHeaders); }).cookies((cookies) -> { cookies.addAll(this.getCookies()); }).body(Flux.from(body)).build(); } private byte[] modifyBody(byte[] responseBody) { try { String bodyStr = this.modifyBody(new String(responseBody, StandardCharsets.UTF_8)); bodyStr = modifyResponseBody(bodyStr); LOGGER.info("the body string {}", bodyStr); return bodyStr.getBytes(StandardCharsets.UTF_8); } catch (Exception var3) { LOGGER.error("modify response error", var3); throw new ShenyuException(String.format("response modify failure. %s", var3.getLocalizedMessage())); } } private String modifyBody(String jsonValue) { DocumentContext context = JsonPath.parse(jsonValue); return context.jsonString(); } private String modifyResponseBody(String responseBody) { JSONObject joEnc = JSONObject.parseObject(responseBody); String recv = joEnc.getString("data"); try { recv = RC4Util.decryRC4String( recv, this.selectorHandle.getRc4Key(), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } JSONObject jo = JSONObject.parseObject(recv); boolean signState = MD5SignUtil.checkSign(this.selectorHandle.getMd5Key(), jo); if (signState){ LOGGER.info("回报验签: {}", signState); }else { throw new ShenyuException("回报验签验证失败!"); } return recv; } } ```
李贤利
2023年7月12日 16:51
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
Word文件
PDF文档
PDF文档(打印)
分享
链接
类型
密码
更新密码
有效期