|
@@ -22,7 +22,10 @@ import com.chelvc.framework.base.context.LoggingContextHolder;
|
|
import com.chelvc.framework.base.context.Result;
|
|
import com.chelvc.framework.base.context.Result;
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
import com.chelvc.framework.base.context.SessionContextHolder;
|
|
import com.chelvc.framework.base.util.HttpUtils;
|
|
import com.chelvc.framework.base.util.HttpUtils;
|
|
|
|
+import com.chelvc.framework.common.util.CodecUtils;
|
|
|
|
+import com.chelvc.framework.common.util.FileUtils;
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
|
+import com.chelvc.framework.common.util.StringUtils;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.web.filter.OncePerRequestFilter;
|
|
import org.springframework.web.filter.OncePerRequestFilter;
|
|
@@ -38,11 +41,38 @@ public class RequestInvokeInterceptor extends OncePerRequestFilter {
|
|
/**
|
|
/**
|
|
* 缓存数据流对象
|
|
* 缓存数据流对象
|
|
*/
|
|
*/
|
|
- static class CachingInputStream extends ServletInputStream {
|
|
|
|
|
|
+ public static class CachingInputStream extends ServletInputStream {
|
|
|
|
+ private final byte[] bytes;
|
|
private final InputStream delegate;
|
|
private final InputStream delegate;
|
|
|
|
|
|
private CachingInputStream(byte[] bytes) {
|
|
private CachingInputStream(byte[] bytes) {
|
|
- this.delegate = new ByteArrayInputStream(bytes);
|
|
|
|
|
|
+ this.delegate = new ByteArrayInputStream(this.bytes = bytes);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取字节数组
|
|
|
|
+ *
|
|
|
|
+ * @return 字节数组
|
|
|
|
+ */
|
|
|
|
+ public byte[] bytes() {
|
|
|
|
+ return this.bytes(false);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取字节数组
|
|
|
|
+ *
|
|
|
|
+ * @param decode 是否解码(Base64)
|
|
|
|
+ * @return 字节数组
|
|
|
|
+ */
|
|
|
|
+ public byte[] bytes(boolean decode) {
|
|
|
|
+ if (this.bytes.length == 0) {
|
|
|
|
+ return ObjectUtils.EMPTY_BYTE_ARRAY;
|
|
|
|
+ } else if (decode) {
|
|
|
|
+ return CodecUtils.decodeBase64(this.bytes);
|
|
|
|
+ }
|
|
|
|
+ byte[] buffer = new byte[this.bytes.length];
|
|
|
|
+ System.arraycopy(this.bytes, 0, buffer, 0, this.bytes.length);
|
|
|
|
+ return buffer;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -117,21 +147,49 @@ public class RequestInvokeInterceptor extends OncePerRequestFilter {
|
|
/**
|
|
/**
|
|
* 请求缓存包装对象
|
|
* 请求缓存包装对象
|
|
*/
|
|
*/
|
|
- static class CachingRequestWrapper extends HttpServletRequestWrapper {
|
|
|
|
- private final byte[] body;
|
|
|
|
|
|
+ public static class CachingRequestWrapper extends HttpServletRequestWrapper {
|
|
|
|
+ private String body;
|
|
|
|
+ private final byte[] bytes;
|
|
private final Map<String, String[]> parameters;
|
|
private final Map<String, String[]> parameters;
|
|
|
|
|
|
- private CachingRequestWrapper(HttpServletRequest request) {
|
|
|
|
|
|
+ private CachingRequestWrapper(HttpServletRequest request) throws IOException {
|
|
super(request);
|
|
super(request);
|
|
if (request instanceof CachingRequestWrapper) {
|
|
if (request instanceof CachingRequestWrapper) {
|
|
this.body = ((CachingRequestWrapper) request).body;
|
|
this.body = ((CachingRequestWrapper) request).body;
|
|
|
|
+ this.bytes = ((CachingRequestWrapper) request).bytes;
|
|
this.parameters = ((CachingRequestWrapper) request).parameters;
|
|
this.parameters = ((CachingRequestWrapper) request).parameters;
|
|
} else {
|
|
} else {
|
|
// Servlet 规范规定如果调用了getInputStream()或getReader()方法,则getParameter*()方法的行为是未定义的
|
|
// Servlet 规范规定如果调用了getInputStream()或getReader()方法,则getParameter*()方法的行为是未定义的
|
|
// Tomcat、Jetty、Undertow容器均支持在调用getParameter*()方法后能正常调用getInputStream()方法
|
|
// Tomcat、Jetty、Undertow容器均支持在调用getParameter*()方法后能正常调用getInputStream()方法
|
|
this.parameters = request.getParameterMap();
|
|
this.parameters = request.getParameterMap();
|
|
- this.body = HttpUtils.getBody(request);
|
|
|
|
|
|
+ if (HttpUtils.isBodyMethod(request)) {
|
|
|
|
+ try (InputStream input = request.getInputStream()) {
|
|
|
|
+ if (input instanceof CachingInputStream) {
|
|
|
|
+ this.bytes = ((CachingInputStream) input).bytes;
|
|
|
|
+ } else {
|
|
|
|
+ this.bytes = FileUtils.getBytes(input);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (this.bytes.length == 0) {
|
|
|
|
+ this.body = StringUtils.EMPTY;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ this.body = StringUtils.EMPTY;
|
|
|
|
+ this.bytes = ObjectUtils.EMPTY_BYTE_ARRAY;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取请求体
|
|
|
|
+ *
|
|
|
|
+ * @return 请求体字符串
|
|
|
|
+ */
|
|
|
|
+ public String getBody() {
|
|
|
|
+ if (this.body == null) {
|
|
|
|
+ this.body = new String(this.bytes);
|
|
}
|
|
}
|
|
|
|
+ return this.body;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -173,17 +231,19 @@ public class RequestInvokeInterceptor extends OncePerRequestFilter {
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public ServletInputStream getInputStream() throws IOException {
|
|
public ServletInputStream getInputStream() throws IOException {
|
|
- return new CachingInputStream(this.body);
|
|
|
|
|
|
+ return new CachingInputStream(this.bytes);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
|
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
|
|
throws ServletException, IOException {
|
|
throws ServletException, IOException {
|
|
- if (HttpUtils.isBodyMethod(request) && !HttpUtils.isMultipartBody(request)) {
|
|
|
|
|
|
+ // 如果不是文件上传请求则对原生请求进行缓存包装处理
|
|
|
|
+ if (!HttpUtils.isMultipartBody(request)) {
|
|
request = new CachingRequestWrapper(request);
|
|
request = new CachingRequestWrapper(request);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // 执行请求处理链
|
|
try {
|
|
try {
|
|
chain.doFilter(request, response);
|
|
chain.doFilter(request, response);
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|