From b4ad1e9f2d9f609f9b748fb72f4c4cc167b04237 Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Mon, 2 Jan 2023 13:33:43 +0330 Subject: [PATCH 1/8] #BAEL-2862: remove SecurityAutoConfiguration class and extra annotation --- .../java/com/baeldung/web/log/app/Application.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/Application.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/Application.java index 40f5341e29..24174815fa 100644 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/Application.java +++ b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/Application.java @@ -1,19 +1,18 @@ package com.baeldung.web.log.app; -import javax.servlet.ServletRegistration; - import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.PropertySource; -import com.baeldung.web.log.config.CustomeRequestLoggingFilter; - -@EnableAutoConfiguration @ComponentScan("com.baeldung.web.log") @PropertySource("application-log.properties") -@SpringBootApplication +@SpringBootApplication(exclude = { + SecurityAutoConfiguration.class, + ManagementWebSecurityAutoConfiguration.class +}) public class Application { public static void main(final String[] args) { From d251343e2e2d1995c6e4f8fa18b62a1aeaaa348c Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Mon, 2 Jan 2023 14:10:57 +0330 Subject: [PATCH 2/8] #BAEL-2862: add log level for CommonsRequestLoggingFilter --- .../spring-boot-runtime/src/main/resources/logback.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/spring-boot-modules/spring-boot-runtime/src/main/resources/logback.xml b/spring-boot-modules/spring-boot-runtime/src/main/resources/logback.xml index 7d900d8ea8..4552282d23 100644 --- a/spring-boot-modules/spring-boot-runtime/src/main/resources/logback.xml +++ b/spring-boot-modules/spring-boot-runtime/src/main/resources/logback.xml @@ -7,6 +7,10 @@ + + + + From 9b206922fd24e4d04bd911fa8ac1583e5c7062ee Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Mon, 2 Jan 2023 14:11:40 +0330 Subject: [PATCH 3/8] #BAEL-2862: remove interceptor --- .../log/app/TaxiFareRequestInterceptor.java | 44 ------------------- .../web/log/config/TaxiFareMVCConfig.java | 19 -------- 2 files changed, 63 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java delete mode 100644 spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java deleted file mode 100644 index 2ea0204dc3..0000000000 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/TaxiFareRequestInterceptor.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.web.log.app; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.stereotype.Component; -import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; -import org.springframework.web.util.ContentCachingRequestWrapper; - -import com.baeldung.web.log.util.RequestLoggingUtil; - -@Component -public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { - - private final static Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class); - - @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String postData; - HttpServletRequest requestCacheWrapperObject = null; - try { - // Uncomment to produce the stream closed issue - // postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream()); - - // To overcome request stream closed issue - requestCacheWrapperObject = new ContentCachingRequestWrapper(request); - requestCacheWrapperObject.getParameterMap(); - } catch (Exception exception) { - exception.printStackTrace(); - } finally { - postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject); - LOGGER.info("REQUEST DATA: " + postData); - } - return true; - } - - @Override - public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { - LOGGER.info("RESPONSE: " + response.getStatus()); - } - -} diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java deleted file mode 100644 index fda8a845e9..0000000000 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/TaxiFareMVCConfig.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.web.log.config; - -import com.baeldung.web.log.app.TaxiFareRequestInterceptor; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -@Configuration -public class TaxiFareMVCConfig implements WebMvcConfigurer { - - @Autowired - private TaxiFareRequestInterceptor taxiFareRequestInterceptor; - - @Override - public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/taxifare/*/"); - } -} From 32763ab63d9169ecfae4ba7540aa94d435f8ef40 Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Mon, 2 Jan 2023 14:12:00 +0330 Subject: [PATCH 4/8] #BAEL-2862: update test case --- .../TaxiFareControllerIntegrationTest.java | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java b/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java index 97d669d3fa..b98c77ad2b 100644 --- a/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java +++ b/spring-boot-modules/spring-boot-runtime/src/test/java/com/baeldung/web/controller/TaxiFareControllerIntegrationTest.java @@ -1,23 +1,19 @@ package com.baeldung.web.controller; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; - +import com.baeldung.web.log.app.Application; +import com.baeldung.web.log.data.TaxiRide; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.context.annotation.Configuration; -import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.boot.test.web.server.LocalServerPort; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.security.config.annotation.web.builders.HttpSecurity; -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import com.baeldung.web.log.app.Application; -import com.baeldung.web.log.data.TaxiRide; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; @RunWith(SpringRunner.class) -@SpringBootTest(classes = { Application.class, TaxiFareControllerIntegrationTest.SecurityConfig.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@SpringBootTest(classes = { Application.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class TaxiFareControllerIntegrationTest { @LocalServerPort @@ -25,8 +21,6 @@ public class TaxiFareControllerIntegrationTest { @Test public void givenRequest_whenFetchTaxiFareRateCard_thanOK() { - - System.out.println(port); String URL = "http://localhost:" + port + "/spring-rest"; TestRestTemplate testRestTemplate = new TestRestTemplate(); TaxiRide taxiRide = new TaxiRide(true, 10l); @@ -37,16 +31,4 @@ public class TaxiFareControllerIntegrationTest { assertThat(fare, equalTo("200")); } - @Configuration - static class SecurityConfig extends WebSecurityConfigurerAdapter { - @Override - protected void configure(HttpSecurity http) throws Exception { - System.out.println("security being set"); - http - .authorizeRequests() - .anyRequest().permitAll() - .and() - .csrf().disable(); - } - } } \ No newline at end of file From c22c1a9cab7c5ea222246258e78744404b9afa34 Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Mon, 2 Jan 2023 14:12:36 +0330 Subject: [PATCH 5/8] #BAEL-2862: remove Spring related class --- .../log/config/CustomWebAppInitializer.java | 27 ------------------- .../config/CustomeRequestLoggingFilter.java | 12 --------- 2 files changed, 39 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomWebAppInitializer.java delete mode 100644 spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomWebAppInitializer.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomWebAppInitializer.java deleted file mode 100644 index 0f19c6dc48..0000000000 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomWebAppInitializer.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.web.log.config; - -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletRegistration; - -import org.springframework.web.context.ContextLoaderListener; -import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; -import org.springframework.web.servlet.DispatcherServlet; -import org.springframework.web.WebApplicationInitializer; - -public class CustomWebAppInitializer implements WebApplicationInitializer { - - @Override - public void onStartup(ServletContext container) throws ServletException { - - AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); - context.setConfigLocation("com.baeldung.web.log"); - container.addListener(new ContextLoaderListener(context)); - - ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(context)); - dispatcher.setLoadOnStartup(1); - dispatcher.addMapping("/"); - - container.addFilter("customRequestLoggingFilter", CustomeRequestLoggingFilter.class).addMappingForServletNames(null, false, "dispatcher"); - } -} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java deleted file mode 100644 index 3accb0a06e..0000000000 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/CustomeRequestLoggingFilter.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.baeldung.web.log.config; - -import org.springframework.web.filter.CommonsRequestLoggingFilter; - -public class CustomeRequestLoggingFilter extends CommonsRequestLoggingFilter { - - public CustomeRequestLoggingFilter() { - super.setIncludeQueryString(true); - super.setIncludePayload(true); - super.setMaxPayloadLength(10000); - } -} From f23e73e5d31852a4a0c45997e0749e51e3915139 Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Mon, 2 Jan 2023 14:12:57 +0330 Subject: [PATCH 6/8] #BAEL-2862: remove extra space --- .../com/baeldung/web/log/config/RequestLoggingFilterConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java index 85728729d5..a0622d45d2 100644 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java +++ b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/config/RequestLoggingFilterConfig.java @@ -14,7 +14,7 @@ public class RequestLoggingFilterConfig { filter.setIncludePayload(true); filter.setMaxPayloadLength(10000); filter.setIncludeHeaders(false); - filter.setAfterMessagePrefix("REQUEST DATA : "); + filter.setAfterMessagePrefix("REQUEST DATA: "); return filter; } } From e188b24dbbeb90c636ea657f29a61ed5f4e50946 Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Mon, 2 Jan 2023 14:13:11 +0330 Subject: [PATCH 7/8] #BAEL-2862: remove unused class --- .../web/log/util/RequestLoggingUtil.java | 38 ------------------- 1 file changed, 38 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java deleted file mode 100644 index 70c4eaee90..0000000000 --- a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/util/RequestLoggingUtil.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baeldung.web.log.util; - -import java.io.IOException; -import java.io.InputStream; -import java.io.StringWriter; - -import javax.servlet.http.HttpServletRequest; - -import org.apache.commons.io.IOUtils; -import org.springframework.web.util.ContentCachingRequestWrapper; -import org.springframework.web.util.WebUtils; - -public class RequestLoggingUtil { - - public static String getStringFromInputStream(InputStream is) { - StringWriter writer = new StringWriter(); - String encoding = "UTF-8"; - try { - IOUtils.copy(is, writer, encoding); - } catch (IOException e) { - e.printStackTrace(); - } - return writer.toString(); - } - - public static String readPayload(final HttpServletRequest request) throws IOException { - String payloadData = null; - ContentCachingRequestWrapper contentCachingRequestWrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class); - if (null != contentCachingRequestWrapper) { - byte[] buf = contentCachingRequestWrapper.getContentAsByteArray(); - if (buf.length > 0) { - payloadData = new String(buf, 0, buf.length, contentCachingRequestWrapper.getCharacterEncoding()); - } - } - return payloadData; - } - -} From 311f190327f9402ed9d061d9df295b1d2bf6652a Mon Sep 17 00:00:00 2001 From: h_sharifi Date: Mon, 2 Jan 2023 14:13:48 +0330 Subject: [PATCH 8/8] #BAEL-2862: add request wrapper & filter --- .../web/log/app/CachedHttpServletRequest.java | 30 +++++++++++++ .../web/log/app/CachedServletInputStream.java | 45 +++++++++++++++++++ .../web/log/app/RequestCachingFilter.java | 33 ++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/CachedHttpServletRequest.java create mode 100644 spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/CachedServletInputStream.java create mode 100644 spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/RequestCachingFilter.java diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/CachedHttpServletRequest.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/CachedHttpServletRequest.java new file mode 100644 index 0000000000..51bbd51ea8 --- /dev/null +++ b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/CachedHttpServletRequest.java @@ -0,0 +1,30 @@ +package com.baeldung.web.log.app; + +import org.springframework.util.StreamUtils; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.io.*; + +public class CachedHttpServletRequest extends HttpServletRequestWrapper { + + private byte[] cachedPayload; + + public CachedHttpServletRequest(HttpServletRequest request) throws IOException { + super(request); + InputStream requestInputStream = request.getInputStream(); + this.cachedPayload = StreamUtils.copyToByteArray(requestInputStream); + } + + @Override + public ServletInputStream getInputStream() { + return new CachedServletInputStream(this.cachedPayload); + } + + @Override + public BufferedReader getReader() { + ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.cachedPayload); + return new BufferedReader(new InputStreamReader(byteArrayInputStream)); + } +} diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/CachedServletInputStream.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/CachedServletInputStream.java new file mode 100644 index 0000000000..673d04876b --- /dev/null +++ b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/CachedServletInputStream.java @@ -0,0 +1,45 @@ +package com.baeldung.web.log.app; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; + +public class CachedServletInputStream extends ServletInputStream { + + private final static Logger LOGGER = LoggerFactory.getLogger(CachedServletInputStream.class); + private InputStream cachedInputStream; + + public CachedServletInputStream(byte[] cachedBody) { + this.cachedInputStream = new ByteArrayInputStream(cachedBody); + } + + @Override + public boolean isFinished() { + try { + return cachedInputStream.available() == 0; + } catch (IOException exp) { + LOGGER.error(exp.getMessage()); + } + return false; + } + + @Override + public boolean isReady() { + return true; + } + + @Override + public void setReadListener(ReadListener readListener) { + throw new UnsupportedOperationException(); + } + + @Override + public int read() throws IOException { + return cachedInputStream.read(); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/RequestCachingFilter.java b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/RequestCachingFilter.java new file mode 100644 index 0000000000..e0928550fc --- /dev/null +++ b/spring-boot-modules/spring-boot-runtime/src/main/java/com/baeldung/web/log/app/RequestCachingFilter.java @@ -0,0 +1,33 @@ +package com.baeldung.web.log.app; + +import org.apache.commons.io.IOUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.stereotype.Component; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +@Order(value = Ordered.HIGHEST_PRECEDENCE) +@Component +@WebFilter(filterName = "RequestCachingFilter", urlPatterns = "/*") +public class RequestCachingFilter extends OncePerRequestFilter { + + private final static Logger LOGGER = LoggerFactory.getLogger(RequestCachingFilter.class); + + @Override + protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, + FilterChain filterChain) throws ServletException, IOException { + CachedHttpServletRequest cachedHttpServletRequest = new CachedHttpServletRequest(request); + LOGGER.info("REQUEST DATA: " + IOUtils.toString(cachedHttpServletRequest.getInputStream(), StandardCharsets.UTF_8)); + filterChain.doFilter(cachedHttpServletRequest, response); + } +} \ No newline at end of file