87 lines
2.2 KiB
Java
87 lines
2.2 KiB
Java
package com.bb.config;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.ByteArrayInputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import jakarta.servlet.ReadListener;
|
|
import jakarta.servlet.ServletInputStream;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import jakarta.servlet.http.HttpServletRequestWrapper;
|
|
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
import com.bb.util.AESUtil;
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
@Slf4j
|
|
public class RequestDecryptWrapper extends HttpServletRequestWrapper {
|
|
|
|
private final Charset encoding;
|
|
private String decodingBody;
|
|
private byte[] rawData;
|
|
|
|
public RequestDecryptWrapper(HttpServletRequest request) {
|
|
super(request);
|
|
String charEncoding = request.getCharacterEncoding();
|
|
this.encoding = ObjectUtils.isEmpty(charEncoding) ? StandardCharsets.UTF_8 : Charset.forName(charEncoding);
|
|
|
|
try {
|
|
InputStream inputStream = request.getInputStream();
|
|
rawData = IOUtils.toByteArray(inputStream);
|
|
|
|
if (ObjectUtils.isEmpty(rawData)) {
|
|
return;
|
|
}
|
|
|
|
AESUtil aesUtil = new AESUtil();
|
|
this.decodingBody = aesUtil.decrypt(new String(rawData, StandardCharsets.UTF_8));
|
|
} catch (IOException e) {
|
|
log.error(e.getMessage());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ServletInputStream getInputStream() {
|
|
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(decodingBody == null ? "".getBytes(encoding) : decodingBody.getBytes(encoding));
|
|
return new ServletInputStream() {
|
|
|
|
@Override
|
|
public boolean isFinished() {
|
|
// TODO Auto-generated method stub
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean isReady() {
|
|
// TODO Auto-generated method stub
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public void setReadListener(ReadListener listener) {
|
|
// TODO Auto-generated method stub
|
|
|
|
}
|
|
|
|
@Override
|
|
public int read() throws IOException {
|
|
// TODO Auto-generated method stub
|
|
return byteArrayInputStream.read();
|
|
}
|
|
|
|
};
|
|
}
|
|
|
|
@Override
|
|
public BufferedReader getReader() {
|
|
return new BufferedReader(new InputStreamReader(this.getInputStream()));
|
|
}
|
|
}
|