SpringBoot项目拦截器获取Post方法的请求body实现
短信预约 -IT技能 免费直播动态提醒
1). 存在问题
流只能读取一次
2). 目标
多次读取流
3). 解决方法
创建包装类
4). RequestWrapper
package com.mazaiting.redeye.wrapper;
import com.mazaiting.redeye.utils.StreamUtil;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
@Slf4j
public class RequestWrapper extends HttpServletRequestWrapper {
private static final Logger mLogger = LoggerFactory.getLogger(RequestWrapper.class);
private String mBody;
public RequestWrapper(HttpServletRequest request) {
super(request);
// 将body数据存储起来
mBody = getBody(request);
}
private String getBody(HttpServletRequest request) {
try {
return StreamUtil.getString(request.getInputStream());
} catch (IOException e) {
mLogger.debug(e.getMessage());
throw new RuntimeException(e);
}
}
public String getBody() {
return mBody;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
@Override
public ServletInputStream getInputStream() throws IOException {
// 创建字节数组输入流
final ByteArrayInputStream bais = new ByteArrayInputStream(mBody.getBytes(Charset.defaultCharset()));
return new ServletInputStream() {
@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 bais.read();
}
};
}
}
5).设置过滤器(必须)
@Component
@WebFilter(filterName = "HttpServletRequestFilter", urlPatterns = "/")
@Order(10000)
public class HttpServletRequestFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if(servletRequest instanceof HttpServletRequest) {
requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
}
//获取请求中的流如何,将取出来的字符串,再次转换成流,然后把它放入到新request对象中
// 在chain.doFiler方法中传递新的request对象
if(null == requestWrapper) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
filterChain.doFilter(requestWrapper, servletResponse);
}
}
@Override
public void destroy() {
}
}
6). 使用
public class SessionInterceptor implements HandlerInterceptor {
private static final Logger mLogger = LoggerFactory.getLogger(SessionInterceptor.class);
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
mLogger.debug("SessionInterceptor");
// 获取地址
String url = request.getRequestURL().toString();
mLogger.debug("url: " + url);
// 获取 session
HttpSession session = request.getSession();
String id = session.getId();
mLogger.debug("sessionId: " + id);
String requestMethod = request.getMethod();
mLogger.debug("requestMethod: " + requestMethod);
String servletPath = request.getServletPath();
mLogger.debug("servletPath: " + servletPath);
if (isJson(request)) {
String body = new RequestWrapper(request).getBody();
mLogger.debug("body: " + body);
}
return true;
}
private boolean isJson(HttpServletRequest request) {
if (request.getContentType() != null) {
return request.getContentType().equals(MediaType.APPLICATION_JSON_VALUE) ||
request.getContentType().equals(MediaType.APPLICATION_JSON_UTF8_VALUE);
}
return false;
}
}
到此这篇关于SpringBoot项目拦截器获取Post方法的请求body实现的文章就介绍到这了,更多相关SpringBoot获取Post方法的请求body内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341