BAEL-7200

fixed formatting
This commit is contained in:
parthiv39731 2023-11-27 10:54:21 +05:30
parent 8d440a419b
commit d58102e32c
6 changed files with 36 additions and 27 deletions

View File

@ -17,6 +17,7 @@ import java.nio.charset.StandardCharsets;
@RestControllerAdvice @RestControllerAdvice
@Profile("aspectExample") @Profile("aspectExample")
public class EscapeHtmlAspect implements RequestBodyAdvice { public class EscapeHtmlAspect implements RequestBodyAdvice {
private static final Logger logger = LoggerFactory.getLogger(EscapeHtmlAspect.class); private static final Logger logger = LoggerFactory.getLogger(EscapeHtmlAspect.class);
@Override @Override
@ -26,7 +27,8 @@ public class EscapeHtmlAspect implements RequestBodyAdvice {
} }
@Override @Override
public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException { public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
logger.info("beforeBodyRead called"); logger.info("beforeBodyRead called");
InputStream inputStream = inputMessage.getBody(); InputStream inputStream = inputMessage.getBody();
return new HttpInputMessage() { return new HttpInputMessage() {
@ -43,13 +45,15 @@ public class EscapeHtmlAspect implements RequestBodyAdvice {
} }
@Override @Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
// Return the modified object after reading the body // Return the modified object after reading the body
return body; return body;
} }
@Override @Override
public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) { public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType,
Class<? extends HttpMessageConverter<?>> converterType) {
//return the original body //return the original body
return body; return body;
} }
@ -68,7 +72,7 @@ public class EscapeHtmlAspect implements RequestBodyAdvice {
String input = stringBuilder.toString(); String input = stringBuilder.toString();
// Escape HTML characters // Escape HTML characters
return input.replaceAll("&", "&amp;") return input.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;") .replaceAll("<", "&lt;")
.replaceAll(">", "&gt;"); .replaceAll(">", "&gt;");
} }
} }

View File

@ -1,6 +1,7 @@
package com.baeldung.modifyrequest.config; package com.baeldung.modifyrequest.config;
import com.baeldung.modifyrequest.interceptor.EscapeHtmlRequestInterceptor; import com.baeldung.modifyrequest.interceptor.EscapeHtmlRequestInterceptor;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -17,7 +18,7 @@ public class WebMvcConfiguration implements WebMvcConfigurer {
public void addInterceptors(InterceptorRegistry registry) { public void addInterceptors(InterceptorRegistry registry) {
logger.info("addInterceptors() called"); logger.info("addInterceptors() called");
registry.addInterceptor(new EscapeHtmlRequestInterceptor()) registry.addInterceptor(new EscapeHtmlRequestInterceptor())
.addPathPatterns("/save"); .addPathPatterns("/save");
WebMvcConfigurer.super.addInterceptors(registry); WebMvcConfigurer.super.addInterceptors(registry);
} }

View File

@ -27,10 +27,9 @@ public class EscapeHtmlRequestWrapper extends HttpServletRequestWrapper {
String input = stringBuilder.toString(); String input = stringBuilder.toString();
// Escape HTML characters // Escape HTML characters
return input.replaceAll("&", "&amp;") return input.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;") .replaceAll("<", "&lt;")
.replaceAll(">", "&gt;") .replaceAll(">", "&gt;")
//.replaceAll("\"", "&quot;") .replaceAll("'", "&#39;");
.replaceAll("'", "&#39;");
} }
@Override @Override

View File

@ -44,9 +44,11 @@ public class EscapeHtmlAspectIntegrationTest {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save")) mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody))) .content(objectMapper.writeValueAsString(requestBody)))
.andExpect(MockMvcResultMatchers.status().isCreated()) .andExpect(MockMvcResultMatchers.status()
.andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString(expectedResponseBody))); .isCreated())
.andExpect(MockMvcResultMatchers.content()
.json(objectMapper.writeValueAsString(expectedResponseBody)));
} }
} }

View File

@ -31,21 +31,23 @@ public class EscapeHtmlFilterIntegrationTest {
@Test @Test
void givenFilter_whenEscapeHtmlFilter_thenEscapeHtml() throws Exception { void givenFilter_whenEscapeHtmlFilter_thenEscapeHtml() throws Exception {
Map<String, String> requestBody = Map.of( Map<String, String> requestBody = Map.of(
"name", "James Cameron", "name", "James Cameron",
"email", "<script>alert()</script>james@gmail.com" "email", "<script>alert()</script>james@gmail.com"
); );
Map<String, String> expectedResponseBody = Map.of( Map<String, String> expectedResponseBody = Map.of(
"name", "James Cameron", "name", "James Cameron",
"email", "&lt;script&gt;alert()&lt;/script&gt;james@gmail.com" "email", "&lt;script&gt;alert()&lt;/script&gt;james@gmail.com"
); );
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save")) mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody))) .content(objectMapper.writeValueAsString(requestBody)))
.andExpect(MockMvcResultMatchers.status().isCreated()) .andExpect(MockMvcResultMatchers.status()
.andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString(expectedResponseBody))); .isCreated())
.andExpect(MockMvcResultMatchers.content()
.json(objectMapper.writeValueAsString(expectedResponseBody)));
} }
} }

View File

@ -33,14 +33,15 @@ public class EscapeHtmlInterceptorIntegrationTest {
@Test @Test
void givenInterceptor_whenEscapeHtmlInterceptor_thenEscapeHtml() throws Exception { void givenInterceptor_whenEscapeHtmlInterceptor_thenEscapeHtml() throws Exception {
Map<String, String> requestBody = Map.of( Map<String, String> requestBody = Map.of(
"name", "James Cameron", "name", "James Cameron",
"email", "<script>alert()</script>james@gmail.com" "email", "<script>alert()</script>james@gmail.com"
); );
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save")) mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
.contentType(MediaType.APPLICATION_JSON) .contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody))) .content(objectMapper.writeValueAsString(requestBody)))
.andExpect(MockMvcResultMatchers.status().is4xxClientError()); .andExpect(MockMvcResultMatchers.status()
.is4xxClientError());
} }
} }