简体   繁体   English

修改/替换拦截器内的 ClientHttpResponse 主体(ClientHttpRequestInterceptor)

[英]Modify/Replace ClientHttpResponse body inside interceptor (ClientHttpRequestInterceptor)

I'm adding message level encryption (MLE) to an existing code base for outgoing requests.我正在将消息级加密 (MLE) 添加到用于传出请求的现有代码库中。 To do this, I simply wrote an interceptor that will catch outgoing requests, encrypt their bodies, and then send the request out.为此,我简单地编写了一个拦截器,它将捕获传出的请求,加密它们的主体,然后将请求发送出去。 The response we get is also encrypted, and must be decrypted.我们得到的响应也是加密的,必须解密。 This all is working fine for me.这一切对我来说都很好。 The only problem I'm having is that I must replace the ClientHttpResponse encrypted body with the now decrypted JSON.我遇到的唯一问题是我必须用现在解密的 JSON 替换ClientHttpResponse加密的主体。 How can I do this?我怎样才能做到这一点? I don't see any methods that will let me alter the response body.我没有看到任何可以让我更改响应正文的方法。 Thanks in advance.提前致谢。

@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution)
        throws IOException {
    ClientHttpResponse response;
    String bodyStr = new String(body);

    // Encrypt the body and send
    bodyStr = encrypt(bodyStr);
    try {
        response = execution.execute(request, bodyStr.getBytes());
    } catch (Exception e) {
        throw e;
    }

    // Decrypt the response body
    String decryptedResponseBody = decrypt(response.getBody());

    // Set the response body to the decrypted data (JSON)
    // response.setBody(decryptedResponseBody)?????????

    return response;
}

You will need to create an implementation of ClientHttpResponse which is not too hard since there are only a few methods to override, I added an example of how you would fix this.您将需要创建一个 ClientHttpResponse 的实现,这并不难,因为只有几个方法可以覆盖,我添加了一个示例来说明如何解决此问题。 I hope this helps.我希望这有帮助。 I would suggest adding a named bean for this type of request, you don't want to have all your resttemplates being encrypted/decrypted.我建议为这种类型的请求添加一个命名的 bean,你不希望所有的 resttemplates 都被加密/解密。

restTemplate.getInterceptors().add( (ClientHttpRequestInterceptor)  
(request, body, execution) -> {
        ClientHttpResponse response;

        String bodyStr = new String(body);

        // Encrypt the body and send
        bodyStr = encrypt(bodyStr);

        try {
            response = execution.execute(request, bodyStr.getBytes());
            String text = IOUtils.toString(response.getBody(), StandardCharsets.UTF_8.name());

            // Decrypt the response body
            String decryptedResponseBody = decrypt(text);
            
        } catch (Exception e) {
            throw e;
        }

        InputStream inputStream = inputStream = new ByteArrayInputStream(decryptedResponseBody.getBytes());

        return new ClientHttpResponse() {

            @Override
            public HttpHeaders getHeaders() {
                return response.getHeaders();
            }

            @Override
            public InputStream getBody() throws IOException {
                return inputStream;
            }

            @Override
            public HttpStatus getStatusCode() throws IOException {
                return response.getStatusCode();
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return response.getRawStatusCode();
            }

            @Override
            public String getStatusText() throws IOException {
                return response.getStatusText();
            }

            @Override
            public void close() {
                response.close();
            }
        };
}))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM