当前位置:网站首页>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() {
}
}
边栏推荐
- sqli-labs less3/4打靶笔记
- php-fpm
- 【SQL】first_value 应用场景 - 首单 or 复购
- torch分布式训练
- 最新版redis6.3.2下载安装
- [Mini Program Project Development--Jingdong Mall] Classification Navigation Area of uni-app
- npm run serve启动报错npm ERR Missing script “serve“
- 信息安全必备神器之kali
- Request请求体重新封装,解决请求体只能获取一次的问题
- uni-app installs components using npm commands
猜你喜欢
随机推荐
CTF misc-audio and video steganography
CTF之misc-内存分析(Volatility)
Arrays工具类的使用
npm安装和npm安装——保存
kali is an essential artifact for information security
PHP-fpm
TypeError The view function did not return a valid response. The function either returned None 的解决
【SQL】first_value 应用场景 - 首单 or 复购
3分钟告诉你如何成为一名黑客|零基础到黑客入门指南,你只需要掌握这五点能力
C# WPF中监听窗口大小变化事件
php-fpm
SSTI靶场
CTF之misc-流量分析
最新Redistemplate配置及使用,附带操作工具类,测试类
MySQL存储引擎
CTFSHOW命令执行【web29-web124】未完待续
uni-app:uni-icons的使用及如何自定义图标
C#预定义数据类型简介
POI工具类
利用自定义注解,统计方法执行时间