Last active
December 25, 2023 08:31
-
-
Save kamlesh0606/fa94281dd2e64908b035 to your computer and use it in GitHub Desktop.
Request and Response Payload Or Body Log
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import java.util.Enumeration; | |
| import javax.servlet.FilterChain; | |
| import javax.servlet.ServletException; | |
| import javax.servlet.ServletRequest; | |
| import javax.servlet.ServletResponse; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletResponse; | |
| import org.apache.log4j.Logger; | |
| import org.springframework.web.filter.GenericFilterBean; | |
| public class RestSecurityFilter extends GenericFilterBean { | |
| private static final Logger logger = Logger.getLogger(RestSecurityFilter.class); | |
| private static boolean filterLogger = true; | |
| public static void setFilterLogger(boolean filterLogger) { | |
| RestSecurityFilter.filterLogger = filterLogger; | |
| } | |
| @Override | |
| public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { | |
| if (filterLogger) { | |
| AuthenticationRequestWrapper authenticationRequestWrapper = new AuthenticationRequestWrapper((HttpServletRequest) request); | |
| AuthenticationResponseWrapper authenticationResponseWrapper = new AuthenticationResponseWrapper((HttpServletResponse) response); | |
| String headersString = ""; | |
| Enumeration<String> e = authenticationRequestWrapper.getHeaderNames(); | |
| while (e.hasMoreElements()) { | |
| String headers = e.nextElement(); | |
| if (headers != null) { | |
| headersString += headers + " :: " + authenticationRequestWrapper.getHeader(headers) + " , "; | |
| } | |
| } | |
| boolean reqb = isContentJson(authenticationRequestWrapper.getContentType()); | |
| logger.info("\n -----------------REST Request Detail-------------------------" + " \n RequestURI :: " | |
| + authenticationRequestWrapper.getRequestURI() + " \n REMOTE ADDRESS :: " + authenticationRequestWrapper.getRemoteAddr() | |
| + " \n HEADERS :: [ " + headersString + " ] " + " \n REQUEST BODY Size :: " + authenticationRequestWrapper.payload.length() | |
| + " bytes" + " \n REQUEST BODY :: " + ((reqb) ? authenticationRequestWrapper.payload : "") + " \n HTTP METHOD :: " | |
| + authenticationRequestWrapper.getMethod() + " \n ContentType :: " + authenticationRequestWrapper.getContentType()); | |
| chain.doFilter(authenticationRequestWrapper, authenticationResponseWrapper); | |
| boolean resb = isContentJson(authenticationResponseWrapper.getContentType()); | |
| logger.info("\n -----------------REST Response Detail-------------------------" + " \n Response BODY Size :: " | |
| + authenticationResponseWrapper.getContent().length() + " bytes" + " \n Response BODY :: " | |
| + ((resb) ? authenticationResponseWrapper.getContent() : "") + " \n Content Type :: " | |
| + authenticationResponseWrapper.getContentType()); | |
| } else { | |
| chain.doFilter(request, response); | |
| } | |
| } | |
| private boolean isContentJson(String contentType) { | |
| if (contentType != null) { | |
| return contentType.startsWith("application/json"); | |
| } | |
| return false; | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.BufferedReader; | |
| import java.io.ByteArrayInputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.io.InputStreamReader; | |
| import javax.servlet.ServletInputStream; | |
| import javax.servlet.http.HttpServletRequest; | |
| import javax.servlet.http.HttpServletRequestWrapper; | |
| import org.apache.log4j.Logger; | |
| import org.springframework.security.core.AuthenticationException; | |
| public class AuthenticationRequestWrapper extends HttpServletRequestWrapper { | |
| private static final Logger logger = Logger.getLogger(AuthenticationRequestWrapper.class); | |
| public final String payload; | |
| public AuthenticationRequestWrapper(HttpServletRequest request) throws AuthenticationException { | |
| super(request); | |
| // read the original payload into the payload variable | |
| StringBuilder stringBuilder = new StringBuilder(); | |
| BufferedReader bufferedReader = null; | |
| try { | |
| // read the payload into the StringBuilder | |
| 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 { | |
| // make an empty string since there is no payload | |
| stringBuilder.append(""); | |
| } | |
| } catch (IOException ex) { | |
| logger.error("Error reading the request payload", ex); | |
| throw new AuthenticationException("Error reading the request payload", ex) { }; | |
| } finally { | |
| if (bufferedReader != null) { | |
| try { | |
| bufferedReader.close(); | |
| } catch (IOException iox) { | |
| // ignore | |
| } | |
| } | |
| } | |
| payload = stringBuilder.toString(); | |
| } | |
| @Override | |
| public ServletInputStream getInputStream() throws IOException { | |
| final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(payload.getBytes()); | |
| ServletInputStream inputStream = new ServletInputStream() { | |
| public int read() throws IOException { | |
| return byteArrayInputStream.read(); | |
| } | |
| }; | |
| return inputStream; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.io.OutputStream; | |
| import java.io.OutputStreamWriter; | |
| import java.io.PrintWriter; | |
| import javax.servlet.ServletOutputStream; | |
| import javax.servlet.http.HttpServletResponse; | |
| import javax.servlet.http.HttpServletResponseWrapper; | |
| import org.apache.commons.io.output.TeeOutputStream; | |
| import org.apache.log4j.Logger; | |
| public class AuthenticationResponseWrapper extends HttpServletResponseWrapper { | |
| private static final Logger logger = Logger.getLogger(AuthenticationResponseWrapper.class); | |
| TeeServletOutputStream teeStream; | |
| PrintWriter teeWriter; | |
| ByteArrayOutputStream bos; | |
| public AuthenticationResponseWrapper(HttpServletResponse response) { | |
| super(response); | |
| } | |
| public String getContent() throws IOException { | |
| return bos.toString(); | |
| } | |
| @Override | |
| public PrintWriter getWriter() throws IOException { | |
| if (this.teeWriter == null) { | |
| this.teeWriter = new PrintWriter(new OutputStreamWriter(getOutputStream())); | |
| } | |
| return this.teeWriter; | |
| } | |
| @Override | |
| public ServletOutputStream getOutputStream() throws IOException { | |
| if (teeStream == null) { | |
| bos = new ByteArrayOutputStream(); | |
| teeStream = new TeeServletOutputStream(getResponse().getOutputStream(), bos); | |
| } | |
| return teeStream; | |
| } | |
| @Override | |
| public void flushBuffer() throws IOException { | |
| if (teeStream != null) { | |
| teeStream.flush(); | |
| System.err.println("teeStream flush"); | |
| } | |
| if (this.teeWriter != null) { | |
| this.teeWriter.flush(); | |
| System.err.println("teeWriter flush"); | |
| } | |
| } | |
| public class TeeServletOutputStream extends ServletOutputStream { | |
| private final TeeOutputStream targetStream; | |
| public TeeServletOutputStream(OutputStream one, OutputStream two) { | |
| targetStream = new TeeOutputStream(one, two); | |
| } | |
| @Override | |
| public void write(int arg0) throws IOException { | |
| this.targetStream.write(arg0); | |
| } | |
| public void flush() throws IOException { | |
| super.flush(); | |
| this.targetStream.flush(); | |
| } | |
| public void close() throws IOException { | |
| super.close(); | |
| this.targetStream.close(); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| http://www.wetfeetblog.com/servlet-filer-to-log-request-and-response-details-and-payload/431 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank your for this example, but with spring boot 3, i have this error
Cannot invoke "java.io.ByteArrayOutputStream.toString()" because "this.bos" is null