当前位置:网站首页>The Request request body is repackaged to solve the problem that the request body can only be obtained once
The Request request body is repackaged to solve the problem that the request body can only be obtained once
2022-07-30 06:39:00 【Weizhi】
RequestRequest body repackaging,Solve the problem that the request body can only be obtained once
问题
在mvc架构中,Interfaces are often intercepted to do some permission checks,A common practice is to add interceptors or filters,which will be obtained in advancepost请求体,获取之后,controllerThe problem that the parameters on the method can no longer be obtained.
解决方案
定义一个ServletInputStreamWrapper
ServletInputStreamWrapper is to re-wrap the request body into HttpServletRequestWrapper中
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import java.io.IOException;
public class ServletInputStreamWrapper extends ServletInputStream {
private byte[] data;
private int idx = 0;
public ServletInputStreamWrapper(byte[] data) {
if (data == null) {
data = new byte[0];
}
this.data = data;
}
public int read() throws IOException {
return this.idx == this.data.length ? -1 : this.data[this.idx++] & 255;
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
}
定义一个过滤器,并对request进行包装
The purpose here is to repackage the new onerequest,And to be passed into the new request,因此,Using filters is the best option,Interceptors are more cumbersome to do this.实现 getInputStream() 、getContentLength() 、getContentLengthLong() 方法.在getInputStream()The method return is what we defined in the previous stepServletInputStreamWrapper,The new request body is passed in the constructorbyte[]即可.
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
/** * 过滤器 */
@Component
@WebFilter(filterName = "outAuthFilter", urlPatterns = {
"/outer/*"})
public class OutAuthFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
String requestBody = "";
try {
InputStream stream = request.getInputStream();
if (stream != null) {
requestBody = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
}
} catch (IOException e) {
//requestBody里面没有数据
System.out.println("requestBody里面没有数据");
return;
}
String newRequestBody ="{\"id\":\"1\",\"text\":\"新包装的JSON数据\"}";
final byte[] reqBodyBytes = newRequestBody.getBytes();
//对request进行重新包装
HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request) {
@Override
public ServletInputStream getInputStream() throws IOException {
return new ServletInputStreamWrapper(reqBodyBytes);
}
@Override
public int getContentLength() {
return reqBodyBytes.length;
}
@Override
public long getContentLengthLong() {
return reqBodyBytes.length;
}
};
// 将新requestThe wrapper object is passed into the filter chain
filterChain.doFilter(requestWrapper, servletResponse);
}
@Override
public void destroy() {
}
}
边栏推荐
猜你喜欢
awd——waf部署
![[Mini Program Project Development--Jingdong Mall] Classification Navigation Area of uni-app](/img/cb/b0b79444dc90980cd2220ff9e68549.png)
[Mini Program Project Development--Jingdong Mall] Classification Navigation Area of uni-app
![Art-template 中文文档[详细篇]](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
Art-template 中文文档[详细篇]

Application Practice | Application Practice of Apache Doris in Baidu Intelligent Cloud Billing System

torch distributed training

网上说的挖矿究竟是什么? 挖矿系统开发详解介绍
![[Net Ding Cup 2020 Qinglong Group] AreUSerialz](/img/f2/9aef8b8317eff31af2979b3a45b54c.png)
[Net Ding Cup 2020 Qinglong Group] AreUSerialz

vulnhub-XXE ctf安全真题

sqli-labs靶场 SQL注入学习 Less-1

FastAPI 快速入门
随机推荐
jsx的实现
最新Redistemplate配置及使用,附带操作工具类,测试类
php-fpm
简述SSRF
使用PyQt5为YoloV5添加界面(一)
Defense Ideas for a Type of SMS Vulnerability
JDBC programming of MySQL database
mysql处理insert冲突的解决方案
async/await用法详解
Application Practice | Application Practice of Apache Doris in Baidu Intelligent Cloud Billing System
MongoDB快速入门与基本使用
CTF之misc-图片隐写
Flink PostgreSQL CDC配置和常见问题
sqli-labs less3/4 Targeting Notes
div设置一个最小高度和最大高度,但是中间可以靠内容撑开
php漏洞全解
volatility内存取证----命令演示
js方法 reduce 用法
npm安装和npm安装——保存
uncategorized SQLException; SQL state [null]; error code [0]; sql injection violation, syntax error