我有一个方法通过HTTP Put接收输入流并将其转换为byte []并将其发送到另一个名为verifysignature的方法。我一直有一个奇怪的问题。代码没问题,但是消息摘要不匹配。经过调试后,我发现如果我的inpustream只有一行文本但是当有多行文件时它会失败。
Http PUT的请求体看起来像:
{
"Url":"http://live.dev:3000/access_tokens",
"AuthorizationUrl":"http://live.dev:3000/client_access_tokens",
"Cert":"test"
}
响应PUT请求的方法:
public @ResponseBody Map<String,String> createAuthorizationServer(HttpServletRequest request)throws IOException {
// This method converts inputstream to byte[]
byte[] inputStream = toBodyBytes(request.getInputStream());
X509Certificate signingCert = null;
//..... stuff
// Here I am using the byte array
signingCert = engine.verifySignature(signature,inputStream);
当请求正文中的有效负载在一行中时,这可以正常工作:
"test:;sample"
但是当它有多行时失败,例如:
"test:;"
"sample"
有人可以对此嗤之以鼻吗?
toBodyBytes
方法供您参考:
private static byte[] toBodyBytes(InputStream inputStream) throws IOException {
final int MAX_PAYLOAD_SIZE = 10240;
byte[] buf = new byte[MAX_PAYLOAD_SIZE];
// protect against OutOfMemoryError in case of misconfiguration (accidentally filtering uploads)
int bodySize = read(inputStream, buf, 0, buf.length);
checkState(bodySize < MAX_PAYLOAD_SIZE, "Looking for signature on upload payload?");
return copyOf(buf, bodySize);
}
谢谢!