如何更改过滤器中的servletRequest内容长度

How to change the servletRequest content length in the filter

本文关键字:servletRequest 何更改 过滤器      更新时间:2023-09-26

我正在尝试加密从web应用程序发送到服务的POST正文请求。

并且加密是在过滤器中完成的。

我面临内容长度的问题,内容长度是在JavaScript中为post调用设置的,在加密完成时,在过滤器中,内容长度会发生变化(长度会增加),当post数据传输到服务时,数据会被截断为最初(加密前)的数据长度,这会使数据不可用。

加密代码:

public void jsonEncrypt(String passPhrase, int iterationCount, int keySize, String salt, String initializationVector) throws IOException { 
    String json = new String(body, StandardCharsets.UTF_8); 
    AesUtil aesUtil = new AesUtil(keySize, iterationCount); 
    encryptJsonText = aesUtil.encrypt(salt, initializationVector, passPhrase, json); 
    byte[] b = encryptJsonText.getBytes(); 
    body = b; 
}

我可以修复这个问题,我覆盖getContentLength()方法来返回新的长度。通过此操作,我可以加密请求主体并将其发送到我的服务,在那里我可以解密主体并使用它。