apache / apisix-java-plugin-runner

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

Some questions about `upstreamHeaders` acquisition #204

Closed sav7ng closed 1 year ago

sav7ng commented 1 year ago

Issue description

The postFilter has taken effect, but now I encounter another problem, that is, the token in the request of the client cannot be obtained in the upstreamHeaders. I wonder if I understand it wrong?

request parameters:

curl --location --request GET 'http://10.0.10.165/boom-center-user-service/test/leaf' \
--header 'tenant-id: 1' \
--header 'Member-Token: BOOMeyJ0eXBlIjoiSldUIiwiYWxnIjoiSFMyNTYifQ.eyJpc3MiOiJCT09NIiwiaWF0IjoxNjY1NTQ1MzY2LCJwbGF0Zm9ybSI6MSwiYXZhdGFyIjoicyIsIkFDQ09VTlRfSUQiOjE0NjI2ODY3ODgwNTEyNjc1ODUsImlkIjoxNTYwODczNjA2MDE4MjM2NDE3LCJuYW1lIjoicyIsImV4cCI6MTMwNTE0NTY0MTg2fQ.waaLOKbByRStWme9K8sHBPTIejSC84Hh9dNIGxXlKlw'

filter code:

@Override
public void postFilter(PostRequest request, PostResponse response, PluginFilterChain chain) {
    Map<String, String> upstreamHeaders = request.getUpstreamHeaders();
    log.debug("postFilter test: upstreamHeaders {}", JSONUtil.toJsonStr(upstreamHeaders));
    log.debug("postFilter test: response {}", JSONUtil.toJsonStr(response));
    response.setHeader("new-token", "savingrun");
    chain.postFilter(request, response);
}

log:

2022/10/18 16:03:26 [warn] 14817#14817: *3556373 [lua] init.lua:913: 2022-10-18 16:03:26.695 DEBUG 14822 --- [ntLoopGroup-2-3] c.b.a.f.AuthGlobalFilter                 : postFilter test: upstreamHeaders {"Transfer-Encoding":"chunked","Keep-Alive":"timeout=3","Connection":"keep-alive","Date":"Tue, 18 Oct 2022 08:03:18 GMT","Content-Type":"application/json"}
2022-10-18 16:03:26.695 DEBUG 14822 --- [ntLoopGroup-2-3] c.b.a.f.AuthGlobalFilter                 : postFilter test: response {}
2022-10-18 16:03:26.695 DEBUG 14822 --- [ntLoopGroup-2-3] i.n.h.l.LoggingHandler                   : [id: 0x98af5762] WRITE: 92B

Environment

tzssangglass commented 1 year ago

if you mean Member-Token , it is headers of client request headers, not upstream headers, client request headers only can be fetched in filter function by request.getHeaders.

sav7ng commented 1 year ago

@tzssangglass Thank you for your patience. I use beforeBodyWrite to process the upstream response and return the token response, so that APISIX can get it