This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

Writing in the front

In the previous article, we talked about how to get the parameters of the get request in the filter. We also gave some examples of the method in use, if you are interested, you can take a look.

How to get the parameter value in SpringBoot filter

Today we are going to look at the same series, which is how to get the parameters of the POST request in the filter.

Application scenarios

If we get the parameters of the POST request, we usually want to modify the parameters in the filter, or in the interceptor, or even in the parser, or add some parameters to the original parameters.

So now we’re going to use the next solution, let’s look at it.

The solution

We are generally through inheritance HttpServletRequestWrapper classes, to achieve the parameters of the specific processing methods.

Suppose we now want to get the data for the POST request in the filter.

To create a requestWrapper. Java class, we will paste the code directly.

@Data public class RequestWrapper extends HttpServletRequestWrapper { private byte[] body; public RequestWrapper(HttpServletRequest request) throws IOException { super(request); body = this.toByteArray(request.getInputStream()); } private byte[] toByteArray(ServletInputStream inputStream) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); byte[] buffer = new byte[1024 * 4]; int n = 0; while ((n = inputStream.read(buffer)) ! = -1) { out.write(buffer, 0, n); } return out.toByteArray(); } @Override public BufferedReader getReader() { return new BufferedReader(new InputStreamReader(getInputStream())); } @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(); }}; }}Copy the code

In the above class, inherited the HttpServletRequestWrapper class, inheritance override the parent class getReader, getInputStream method, actually we see from literal meaning also can see related meaning.

GetReader () : Reads the data and returns a BufferReader class.

GetInputStream () : Gets the file input stream.

With this class, we can successfully extract the parameter values of the Post request. Let’s see.

In the filter method, we can get the Request object, so we won’t post too much code, just highlight the code.

RequestWrapper requestWrapper = new RequestWrapper(req);
byte[] bytes = requestWrapper.getBody();
Map<String, String> map = ByteUtils.changeByteToMap(bytes);
Copy the code

I’m using a tool class here, and I’ll share it with you later, but I won’t repeat it in this article, so go ahead and try it out.