当前位置:网站首页>getReader() has already been called for this request
getReader() has already been called for this request
2022-06-27 01:11:00 【Small target youth】
Problem site :

reason :
HttpServletRequest Of getInputStream() and getReader() Can only be read once .
because We use @RequestBody annotation , Read body Parameters ; and also Wrote interceptors , We also need to post request ,body Take out the data .
because @RequestBody It is also read in the form of stream , After a stream reading, it's gone .
Solution :
Filters take precedence over interceptors , Let's write a filter , Inside the filter Stream data copy One for use , That is to say, copy .
Just use our replicated stream data on the interceptor .
BodyWrapperFilter.java
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/**
* @Author: JCccc
* @Date: 2022-6-12 10:35
* @Description:
*/
public class BodyWrapperFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
if(servletRequest instanceof HttpServletRequest) {
requestWrapper = new CustomHttpServletRequestWrapper((HttpServletRequest) servletRequest);
}
if(requestWrapper == null) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
filterChain.doFilter(requestWrapper, servletResponse);
}
}
}CustomHttpServletRequestWrapper.java
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.StandardCharsets;
/**
* @Author: JCccc
* @Date: 2022-6-12 10:36
* @Description: Rewrite your own RequestWrapper take out body Use it for yourself
*/
public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {
private byte[] body;
public CustomHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
BufferedReader reader = request.getReader();
try (StringWriter writer = new StringWriter()) {
int read;
char[] buf = new char[1024 * 8];
while ((read = reader.read(buf)) != -1) {
writer.write(buf, 0, read);
}
this.body = writer.getBuffer().toString().getBytes();
}
}
public String getBody(){
return new String(body, StandardCharsets.UTF_8);
}
@Override
public ServletInputStream getInputStream() {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body);
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() {
return byteArrayInputStream.read();
}
};
}
@Override
public BufferedReader getReader() {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
}
WebApplicationConfig.java
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Author: JCccc
* @Date: 2022-6-23 10:52
* @Description:
*/
@Configuration
public class WebApplicationConfig {
@Bean
BodyWrapperFilter getBodyWrapperFilter(){
return new BodyWrapperFilter();
}
@Bean("bodyWrapperFilter")
public FilterRegistrationBean<BodyWrapperFilter> checkUserFilter(BodyWrapperFilter bodyWrapperFilter) {
FilterRegistrationBean<BodyWrapperFilter> registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(bodyWrapperFilter);
registrationBean.addUrlPatterns("/*");
registrationBean.setOrder(1);
registrationBean.setAsyncSupported(true);
return registrationBean;
}
} And then in the interceptor , Take out if we want to body, We use it this way instead :
CustomHttpServletRequestWrapper wrapper = (CustomHttpServletRequestWrapper) request; String nowParams = wrapper.getBody();
effect :
well , That's it .
边栏推荐
- IIS deploy static web site and FTP service
- memcached基础
- 架构实战营模块五作业
- 理想L9产品力分析:售价45.98万,采用四缸发动机,续航1315公里
- Lambda expression
- IIS 部署静态网站和 FTP 服务
- ESP32-SOLO开发教程,解决CONFIG_FREERTOS_UNICORE问题
- Lambda表达式
- JSON parsing, esp32 easy access to time, temperature and weather
- Employment prospect of GIS and remote sensing specialty and ranking selection of universities in 2022
猜你喜欢

Statistical Hypothesis Testing

Custom class loader encrypts and decrypts classes

Processing of slice loss in ArcGIS mosaic dataset

Basic introduction to C program structure Preview

Live review | Ziya &ccf TF: Discussion on software supply chain risk management technology under cloud native scenario

2022年地理信息系统与遥感专业就业前景与升学高校排名选择

Gaussian and Summary Stats

How to use ch423? Cheap domestic IO expansion chip

Central Limit Theorem

Summary of working at home during the epidemic | community essay solicitation
随机推荐
buuctf-pwn write-ups (6)
接口测试框架实战(一) | Requests 与接口请求构造
Kept to implement redis autofailover (redisha) 11
Lambda expression
玩转OLED,U8g2动画,增长数字和随机三角形等
MySQL之账号管理、建库以及四大引擎+案例
Account management, database building and four engines + cases of MySQL
07 | workflow design: how to design a reasonable multi person development mode?
CEC-I 中华学习机使用说明与问答
JSON解析,ESP32轻松获取时间气温和天气
Review the old and know the new -- constant renewal at normal temperature
Record a bug caused by a line break
Memcached foundation 2
接口测试框架实战(一) | Requests 与接口请求构造
Implementation of ARP module in LwIP
其他服务注册与发现
Law of Large Numbers
美团:踩雷好几年,才总结出的数据治理避坑攻略
Central Limit Theorem
flutter系列之:flutter中的flow