如何从Spring Boot过滤器的servletResponse中获取响应主体

huangapple go评论72阅读模式
英文:

How to get Response Body from servletResponse in Spring Boot Filter

问题

我想从 ServletResponse 中获取 Response Body,在将其传递给客户端之前,从中创建一个哈希签名。我无法找到实现方法。为了从 ServletRequest 中获取请求正文,我已经按照以下方式进行了实现:

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class RequestWrapper extends HttpServletRequestWrapper {

    private final String body;

    public RequestWrapper(HttpServletRequest request) throws IOException {
        super(request);

        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader bufferedReader = null;
        try {
            InputStream inputStream = request.getInputStream();
            if (inputStream != null) {
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                char[] charBuffer = new char[128];
                int bytesRead = -1;
                while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                    stringBuilder.append(charBuffer, 0, bytesRead);
                }
            } else {
                stringBuilder.append("");
            }
        } catch (IOException ex) {
            throw ex;
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException ex) {
                    throw ex;
                }
            }
        }
        body = stringBuilder.toString();
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
        ServletInputStream servletInputStream = new ServletInputStream() {
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener listener) {

            }
        };
        return servletInputStream;
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(this.getInputStream()));
    }

    public String getBody() {
        return this.body;
    }
}

我如何在 ServletResponse 中实现类似的功能?

英文:

I want to get Response Body from ServletResponse to make a hash signature from it before passing to the client. I couldn't get the way to do it. For getting the request body from ServletRequest I have implemented like the following:

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class RequestWrapper extends HttpServletRequestWrapper{
private final String body;
public RequestWrapper(HttpServletRequest request) throws IOException 
{
//So that other request method behave just like before
super(request);
StringBuilder stringBuilder = new StringBuilder();
BufferedReader bufferedReader = null;
try {
InputStream inputStream = request.getInputStream();
if (inputStream != null) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
char[] charBuffer = new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
stringBuilder.append(charBuffer, 0, bytesRead);
}
} else {
stringBuilder.append("");
}
} catch (IOException ex) {
throw ex;
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException ex) {
throw ex;
}
}
}
//Store request pody content in 'body' variable 
body = stringBuilder.toString();
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes());
ServletInputStream servletInputStream = new ServletInputStream() {
public int read() throws IOException {
return byteArrayInputStream.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener listener) {
}
};
return servletInputStream;
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(this.getInputStream()));
}
//Use this method to read the request body N times
public String getBody() {
return this.body;
}
}

How can I do similar to ServletResponse?

答案1

得分: 14

我在过滤器中实现了ContentCachingResponseWrapper,用于从ServletResponse中获取响应正文,如下所示:

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    ContentCachingResponseWrapper responseCacheWrapperObject = new ContentCachingResponseWrapper((HttpServletResponse) servletResponse);
    filterChain.doFilter(servletRequest, responseCacheWrapperObject);

    byte[] responseArray = responseCacheWrapperObject.getContentAsByteArray();
    String responseStr = new String(responseArray, responseCacheWrapperObject.getCharacterEncoding());
    //....使用 responseStr 进行签名操作

    responseCacheWrapperObject.copyBodyToResponse();
}
英文:

I implemented the ContentCachingResponseWrapper in the Filter to get response body from ServletResponse as follow:

    @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ContentCachingResponseWrapper responseCacheWrapperObject = new ContentCachingResponseWrapper((HttpServletResponse) servletResponse);
filterChain.doFilter(servletRequest, responseCacheWrapperObject);
byte[] responseArray = responseCacheWrapperObject.getContentAsByteArray();
String responseStr = new String(responseArray, responseCacheWrapperObject.getCharacterEncoding());
//....use responsestr to make the signature
responseCacheWrapperObject.copyBodyToResponse();
}

答案2

得分: -2

尝试使用类似以下的代码:

public void handleException(Throwable e, HttpServletRequest request, HttpServletResponse response, int status) {
    CustomErrorResponseException errorResponse = new CustomErrorResponseException();

    errorResponse.setError(e.getClass().getSimpleName());
    errorResponse.setStatus(status);
    errorResponse.setMessage(e.getLocalizedMessage());
    errorResponse.setPath(request.getRequestURI());
    errorResponse.setTimestamp(LocalDateTime.now().toString());

    response.setStatus(status);
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    try {
        response.getWriter().write(jsonbMapper.serialize(errorResponse));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

在过滤器中,只需传递异常、请求、响应以及所需的状态即可。

英文:

try to use something like:

public void handleException(Throwable e, HttpServletRequest request, HttpServletResponse response, int status) {
CustomErrorResponseException errorResponse = new CustomErrorResponseException();
errorResponse.setError(e.getClass().getSimpleName());
errorResponse.setStatus(status);
errorResponse.setMessage(e.getLocalizedMessage());
errorResponse.setPath(request.getRequestURI());
errorResponse.setTimestamp(LocalDateTime.now().toString());
response.setStatus(status);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
try {
response.getWriter().write(jsonbMapper.serialize(errorResponse));
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

And in filter just pass Exception, request, response and status you want to.

huangapple
  • 本文由 发表于 2020年9月17日 17:01:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63934694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定