parent
8d440a419b
commit
d58102e32c
|
@ -17,6 +17,7 @@ import java.nio.charset.StandardCharsets;
|
|||
@RestControllerAdvice
|
||||
@Profile("aspectExample")
|
||||
public class EscapeHtmlAspect implements RequestBodyAdvice {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(EscapeHtmlAspect.class);
|
||||
|
||||
@Override
|
||||
|
@ -26,7 +27,8 @@ public class EscapeHtmlAspect implements RequestBodyAdvice {
|
|||
}
|
||||
|
||||
@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");
|
||||
InputStream inputStream = inputMessage.getBody();
|
||||
return new HttpInputMessage() {
|
||||
|
@ -43,13 +45,15 @@ public class EscapeHtmlAspect implements RequestBodyAdvice {
|
|||
}
|
||||
|
||||
@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 body;
|
||||
}
|
||||
|
||||
@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 body;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.baeldung.modifyrequest.config;
|
||||
|
||||
import com.baeldung.modifyrequest.interceptor.EscapeHtmlRequestInterceptor;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
|
|
@ -29,7 +29,6 @@ public class EscapeHtmlRequestWrapper extends HttpServletRequestWrapper {
|
|||
return input.replaceAll("&", "&")
|
||||
.replaceAll("<", "<")
|
||||
.replaceAll(">", ">")
|
||||
//.replaceAll("\"", """)
|
||||
.replaceAll("'", "'");
|
||||
}
|
||||
|
||||
|
|
|
@ -46,7 +46,9 @@ public class EscapeHtmlAspectIntegrationTest {
|
|||
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(requestBody)))
|
||||
.andExpect(MockMvcResultMatchers.status().isCreated())
|
||||
.andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString(expectedResponseBody)));
|
||||
.andExpect(MockMvcResultMatchers.status()
|
||||
.isCreated())
|
||||
.andExpect(MockMvcResultMatchers.content()
|
||||
.json(objectMapper.writeValueAsString(expectedResponseBody)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,9 @@ public class EscapeHtmlFilterIntegrationTest {
|
|||
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(requestBody)))
|
||||
.andExpect(MockMvcResultMatchers.status().isCreated())
|
||||
.andExpect(MockMvcResultMatchers.content().json(objectMapper.writeValueAsString(expectedResponseBody)));
|
||||
.andExpect(MockMvcResultMatchers.status()
|
||||
.isCreated())
|
||||
.andExpect(MockMvcResultMatchers.content()
|
||||
.json(objectMapper.writeValueAsString(expectedResponseBody)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,6 +41,7 @@ public class EscapeHtmlInterceptorIntegrationTest {
|
|||
mockMvc.perform(MockMvcRequestBuilders.post(URI.create("/save"))
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(requestBody)))
|
||||
.andExpect(MockMvcResultMatchers.status().is4xxClientError());
|
||||
.andExpect(MockMvcResultMatchers.status()
|
||||
.is4xxClientError());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue