parent
2b717426a7
commit
4150ad8787
@ -0,0 +1,106 @@ |
|||||||
|
package com.unionmed.framework.spring.mvc.filter; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON; |
||||||
|
import com.alibaba.fastjson.JSONObject; |
||||||
|
import com.unionmed.framework.http.HttpHeaders; |
||||||
|
import com.unionmed.framework.spring.mvc.response.BodyCryptUtils; |
||||||
|
import com.unionmed.framework.spring.mvc.response.RequestResponseBodyCryptProperties; |
||||||
|
import com.unionmed.framework.util.ObjectUtils; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
import org.apache.commons.io.IOUtils; |
||||||
|
|
||||||
|
import javax.servlet.ReadListener; |
||||||
|
import javax.servlet.ServletInputStream; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletRequestWrapper; |
||||||
|
import java.io.*; |
||||||
|
import java.nio.charset.Charset; |
||||||
|
import java.util.Enumeration; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author ianChen |
||||||
|
* @date 2023/6/27 16:49 |
||||||
|
*/ |
||||||
|
@Slf4j |
||||||
|
public class RepeatHttpServletRequestWrapper extends HttpServletRequestWrapper { |
||||||
|
|
||||||
|
private ServletInputStream is; |
||||||
|
|
||||||
|
public RepeatHttpServletRequestWrapper(RequestResponseBodyCryptProperties requestResponseBodyCryptProperties, HttpServletRequest request) throws IOException { |
||||||
|
super(request); |
||||||
|
try { |
||||||
|
String body; |
||||||
|
if (ObjectUtils.startWithIgnore(request.getHeader(HttpHeaders.CONTENT_TYPE), HttpHeaders.FORM_DATA)) { |
||||||
|
is = request.getInputStream(); |
||||||
|
body = "StreamBody"; |
||||||
|
} else { |
||||||
|
body = IOUtils.toString(request.getInputStream(), Charset.defaultCharset()); |
||||||
|
if (ObjectUtils.notEmpty(body) && requestResponseBodyCryptProperties.isEnabled() && ObjectUtils.equalsIgnore(request.getHeader(HttpHeaders.X_DATA_CRYPT_E), HttpHeaders.X_DATA_CRYPT_E_VALUE_TRUE)) { |
||||||
|
if (log.isDebugEnabled()) |
||||||
|
log.debug("InputMessage Body: {}", body); |
||||||
|
body = BodyCryptUtils.decrypt(requestResponseBodyCryptProperties.getSk(), body); |
||||||
|
} |
||||||
|
is = new RepeatServletInputStreamWrapper(new ByteArrayInputStream(body.getBytes(Charset.defaultCharset()))); |
||||||
|
} |
||||||
|
if (log.isDebugEnabled()) { |
||||||
|
log.debug("URI: {}, Method: {}", request.getRequestURI(), request.getMethod()); |
||||||
|
Map<String, Object> headers = new HashMap<>(); |
||||||
|
String headerKey; |
||||||
|
Enumeration<String> headerNames = request.getHeaderNames(); |
||||||
|
while (headerNames.hasMoreElements()) { |
||||||
|
headers.put(headerKey = headerNames.nextElement(), request.getHeader(headerKey)); |
||||||
|
} |
||||||
|
log.debug("Headers: {}", new JSONObject(headers).toJSONString()); |
||||||
|
log.debug("RequestParameter: {}", request.getParameterMap() == null ? "" : JSON.toJSONString(request.getParameterMap())); |
||||||
|
log.debug("RequestBody: {}", body); |
||||||
|
} |
||||||
|
} catch (IOException ex) { |
||||||
|
log.error("获取Request Body数据异常", ex); |
||||||
|
is = request.getInputStream(); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public ServletInputStream getInputStream() throws IOException { |
||||||
|
return is; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public BufferedReader getReader() throws IOException { |
||||||
|
return new BufferedReader(new InputStreamReader(getInputStream())); |
||||||
|
} |
||||||
|
|
||||||
|
class RepeatServletInputStreamWrapper extends ServletInputStream { |
||||||
|
|
||||||
|
private final InputStream is; |
||||||
|
|
||||||
|
public RepeatServletInputStreamWrapper(InputStream is) { |
||||||
|
this.is = is; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isFinished() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public boolean isReady() { |
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public void setReadListener(ReadListener readListener) { |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int read() throws IOException { |
||||||
|
return this.is.read(); |
||||||
|
} |
||||||
|
|
||||||
|
public synchronized void reset() throws IOException { |
||||||
|
this.is.reset(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
package com.unionmed.framework.spring.mvc.filter; |
||||||
|
|
||||||
|
import com.unionmed.framework.spring.mvc.response.RequestResponseBodyCryptProperties; |
||||||
|
import org.springframework.web.filter.OncePerRequestFilter; |
||||||
|
|
||||||
|
import javax.servlet.FilterChain; |
||||||
|
import javax.servlet.ServletException; |
||||||
|
import javax.servlet.http.HttpServletRequest; |
||||||
|
import javax.servlet.http.HttpServletResponse; |
||||||
|
import java.io.IOException; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author ianChen |
||||||
|
* @date 2023/6/27 16:47 |
||||||
|
*/ |
||||||
|
public class RequestFilterWrapper extends OncePerRequestFilter { |
||||||
|
|
||||||
|
private RequestResponseBodyCryptProperties requestResponseBodyCryptProperties; |
||||||
|
|
||||||
|
public RequestFilterWrapper(RequestResponseBodyCryptProperties requestResponseBodyCryptProperties) { |
||||||
|
this.requestResponseBodyCryptProperties = requestResponseBodyCryptProperties; |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { |
||||||
|
filterChain.doFilter(new RepeatHttpServletRequestWrapper(requestResponseBodyCryptProperties, httpServletRequest), httpServletResponse); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue