apache / apisix-java-plugin-runner

APISIX Plugin Runner in Java
https://apisix.apache.org/
Apache License 2.0
130 stars 96 forks source link

Java plugin how to rewrite request body ,apisix 3.6.0 can Support rewrite request body in external plugin:#9990 #270

Open hcute opened 11 months ago

hcute commented 11 months ago

Issue description

Java plugin how to rewrite request body ,apisxi can Support rewrite request body in external plugin:#9990

Environment

hcute commented 11 months ago

1, goto https://github.com/api7/ext-plugin-proto download source mvn install 0.6.1-RELEASE 2, change https://github.com/apache/apisix-java-plugin-runner/apisix-plugin-runner pom.xml

io.github.api7 A6 0.6.1-RELEASE

3, change source 3.1 HttpResponse add property private String reqBody 3.2 HttpResponse add method
public void changeBody(String body) { actionType = ActionType.Rewrite; this.reqBody = body; } 3.3 HttpResponse change method buildRewriteResp int reqBodyIndex = -1; if (StringUtils.hasText(reqBody)) { byte[] bytes = reqBody.getBytes(StandardCharsets.UTF_8); reqBodyIndex = Rewrite.createBodyVector(builder, bytes); }

   if (-1 != reqBodyIndex) {
        Rewrite.addBody(builder, reqBodyIndex);
    }

3.4 HttpRequest add method public void changeBody(String body) { this.response.changeBody(body); } 4 , run sample //加密方式为空不解密 if(StringUtil.isNotEmpty(oauth2ClientSecurityDTO.getEncryptType())) { EncryptStrategy encryptStrategy = securityStrategyFactory.getEncryptStrategy(oauth2ClientSecurityDTO.getEncryptType()); if (encryptStrategy == null) { logger.error("DecryptError: decrypt type is not support"); response.setStatusCode(993); chain.filter(request,response); } String decrypt = encryptStrategy.decrypt(body, oauth2ClientSecurityDTO); logger.warn("解密后数据: {}", decrypt); String s1 = JSONUtil.toJsonStr(JSONUtil.parseObj(decrypt)); logger.warn("解密后json格式化后: {}",s1); request.setBody(s1); chain.filter(request,response); }else { logger.error("DecryptError: decrypt type is must input"); response.setStatusCode(994); chain.filter(request,response); }

  1. result

2023-10-19 20:40:26.803 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.DecryptRequestFilter : 解密前数据: 0424fd893147f186c12582e80c57296fefdf4ded77a5e8bb408a38a0fe486e886c811818f23032152d78f0eb96357f041be430cc4b2c08b99a18007e4bb96d43eb2e243e9939a9e2421a78cc75673eb10906b24b5e1aa4a802e7d0c6ad2a8331786950f4b97f4fc2be2da4b65dd6a86a4ebac867451bf72e2cb1df3f801c6d64bc0a2061c96c231f4b08fc2a2a4530828690f7a0e47968c7ffc7027a426a33bf469304683ae44cb18bef4da1e1dae5f18a772ae3893ce8f29ef810d9bfbc5e761c98340f0c105a4ade497e83be60e411fce9b5e7bfa7a5197eec75b585629c3fa2fe23c64fef2ced92b7388e839b22382076bd9a39e80d0e2c21426597ac1ced260fc87fcd1d2684153ac6fcceac4dc3c842de2f96b3c56190d74748aa9bc5faf0072aa3684163ccc50b7b2f4f6089daf8f2562a43ad0f0ee87b6bcf56166ea3fbe95ac1f21d87a195b059fc68243eb99c0d77de12c3cccfe17e263205e93e5a0dc8f9e82161f12c59b46daaf31e1a7a76cef408360d6134220857e2ac0073abda67cb83fe544f263067 2023-10-19 20:40:27.046 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.DecryptRequestFilter : 解密后数据: { "MSGNO":"7399", "AAB300":"浙江省博物馆", "AAZ501":"330100D1560000050029C10B2EA7DA04", "AAZ507":"", "AFLAG":"", "SJS1":"", "SJS2":"", "SJS3":"", "SJS4":"", "AAB301":"330100", "TERMINAL_NUM":"000000000000", "SCENE":"0", "SCENETYPE":"001" } 2023-10-19 20:40:27.048 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.DecryptRequestFilter : 解密后json格式化后: {"MSGNO":"7399","AAB300":"浙江省博物馆","AAZ501":"330100D1560000050029C10B2EA7DA04","AAZ507":"","AFLAG":"","SJS1":"","SJS2":"","SJS3":"","SJS4":"","AAB301":"330100","TERMINAL_NUM":"000000000000","SCENE":"0","SCENETYPE":"001"} 2023-10-19 20:40:27.049 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.SignVerifyFilter : 1-签名304402204666282dda0df0b45be5af3210c1173de5e1eaacf2bc800357860328e17243d70220065993906b565750c51fe6dcd5a7762015a44b6c280f03407ce928f4a4f2c661 签名串:AAB300=浙江省博物馆&AAB301=330100&AAZ501=330100D1560000050029C10B2EA7DA04&MSGNO=7399&SCENE=0&SCENETYPE=001&TERMINAL_NUM=000000000000&clientId=test&requestId=1697703080843&timestamp=1697703080843 2023-10-19 20:40:27.088 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.SignVerifyFilter : 2-待签名字符串AAB300=浙江省博物馆&AAB301=330100&AAZ501=330100D1560000050029C10B2EA7DA04&MSGNO=7399&SCENE=0&SCENETYPE=001&TERMINAL_NUM=000000000000&clientId=test&requestId=1697703080843&timestamp=1697703080843 2023-10-19 20:40:27.099 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.SignVerifyFilter : 验证签名成功 2023-10-19 20:40:27.102 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.SignVerifyFilter : 1-签名304402204666282dda0df0b45be5af3210c1173de5e1eaacf2bc800357860328e17243d70220065993906b565750c51fe6dcd5a7762015a44b6c280f03407ce928f4a4f2c661 签名串:AAB300=浙江省博物馆&AAB301=330100&AAZ501=330100D1560000050029C10B2EA7DA04&MSGNO=7399&SCENE=0&SCENETYPE=001&TERMINAL_NUM=000000000000&clientId=test&requestId=1697703080843&timestamp=1697703080843 2023-10-19 20:40:27.102 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.SignVerifyFilter : 2-待签名字符串AAB300=浙江省博物馆&AAB301=330100&AAZ501=330100D1560000050029C10B2EA7DA04&MSGNO=7399&SCENE=0&SCENETYPE=001&TERMINAL_NUM=000000000000&clientId=test&requestId=1697703080843&timestamp=1697703080843 2023-10-19 20:40:27.103 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.SignVerifyFilter : 验证签名成功 2023-10-19 20:40:42.577 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.EncryptedResponseFilter : 上游加密前数据: 加密前数据: {"code":0,"data":{"code":"200","msg":"成功","data":""} 2023-10-19 20:40:42.808 WARN 67088 --- [ntLoopGroup-5-3] c.e.h.a.p.EncryptedResponseFilter : 加密后数据: 04eb52f3dca16088e3ed32ec3d0c2e4aa26800f190efc8fd602129c080b09e678761424404018ab7602de82f24f3ca2ccac3f67c798b02e6b38117c5eb806b427bd1bf913fb5698a1e2ce6975b4b6f485b5642bb547c63b634c7f6668e0bfc801b88a482ece9ec029493a1a5ff76c860c15919d773821e31ddb44c73eb56c3ae1b1f5bed971ee09e8abd4bb837619697e19a7ee3d57aebb485ff6eb8a4a85461703aea1e475e4b508474b06b6b6678cce0003557fe6536784e24d964d7fe8a64a6c059e

monkeyDluffy6017 commented 11 months ago

What is the purpose of this issue?

hcute commented 11 months ago

According to the above code modifications, there were no issues with local testing, but when packaged and deployed to an online environment, an error occurred and no HTTP body was received. Do you have any good suggestions

hcute commented 11 months ago

本地环境运行没问题,但是部署到正式环境 重写请求体收不到 报错 Caused by: java.net.SocketTimeoutException: null at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.fillReadBuffer(NioEndpoint.java:1317) at org.apache.tomcat.util.net.NioEndpoint$NioSocketWrapper.read(NioEndpoint.java:1234) at org.apache.coyote.http11.Http11InputBuffer.fill(Http11InputBuffer.java:785) at org.apache.coyote.http11.Http11InputBuffer.access$400(Http11InputBuffer.java:41) at org.apache.coyote.http11.Http11InputBuffer$SocketInputBuffer.doRead(Http11InputBuffer.java:1185) at org.apache.coyote.http11.filters.IdentityInputFilter.doRead(IdentityInputFilter.java:97) at org.apache.coyote.http11.Http11InputBuffer.doRead(Http11InputBuffer.java:244) at org.apache.coyote.Request.doRead(Request.java:619) at org.apache.catalina.connector.InputBuffer.realReadBytes(InputBuffer.java:313) ... 51 common frames omitted

Liu Wei @.***> 于2023年10月20日周五 17:00写道:

What is the purpose of this issue?

— Reply to this email directly, view it on GitHub https://github.com/apache/apisix-java-plugin-runner/issues/270#issuecomment-1772351093, or unsubscribe https://github.com/notifications/unsubscribe-auth/AN3H66JY4OET4UB72LLZPWLYAI4UJAVCNFSM6AAAAAA6G5X3IWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONZSGM2TCMBZGM . You are receiving this because you authored the thread.Message ID: @.***>

gaoxingliang commented 10 months ago

@hcute Nice code. I merged it into my repo and help me work around and implement a similar encrypt-decrypt feature. My working code is: https://github.com/gaoxingliang/apisix-java-plugin-runner/tree/encrypt-response/runner-starter/src/main/java/org/apache/apisix/plugin/runner/filter

and the read me is: https://github.com/gaoxingliang/apisix-java-plugin-runner/blob/encrypt-response/readme-new.md

Hope this works for you too. I have to merge some code fix from others. Of course, it's not a beautiful work but no other choice until offical code released.