HandlerInterceptor preHandle 에는 어떤 객체가 파라미터로 넘어오는가?

JinHyeong bak
8 min readJan 11, 2022

--

Default

debug:
org.apache.catalina.connector.RequestFacade


request = {RequestFacade@13088}
request = {Request@13094}
coyoteRequest = {Request@13095} "R( /interactivity)"
cookies = null
formats = {SimpleDateFormat[3]@13096}
attributes = {ConcurrentHashMap@13097} size = 14
sslAttributesParsed = false
locales = {ArrayList@13098} size = 0
notes = {HashMap@13099} size = 0
authType = null
internalDispatcherType = {DispatcherType@13100} "REQUEST"
inputBuffer = {InputBuffer@13101}
inputStream = {CoyoteInputStream@13102}
reader = {CoyoteReader@13103}
usingInputStream = false
usingReader = false
userPrincipal = null
parametersParsed = true
cookiesParsed = true
cookiesConverted = false
secure = false
subject = null
postData = null
parameterMap = {ParameterMap@13104} size = 0
parts = null
partsParseException = null
session = null
requestDispatcherPath = {MessageBytes@13105} "/interactivity"
requestedSessionCookie = false
requestedSessionId = null
requestedSessionURL = false
requestedSessionSSL = false
localesParsed = false
localPort = -1
remoteAddr = null
remoteHost = null
remotePort = -1
localAddr = null
localName = null
asyncContext = null
asyncSupported = {Boolean@13106} true
applicationRequest = {RequestFacade@13088}
connector = {Connector@13107} "Connector[HTTP/1.1-8080]"
filterChain = {ApplicationFilterChain@13108}
mappingData = {MappingData@13109}
applicationMapping = {ApplicationMapping@13110}
facade = {RequestFacade@13088}
response = {Response@13111}
URIConverter = {B2CConverter@13112}

Request body logging 하기 위하여 CommonsRequestLoggingFilter 를 추가한 경우

filter추가:

filter log는

2022–01–11 22:11:07.798 DEBUG 36450 — — [nio-8080-exec-1] o.s.w.f.CommonsRequestLoggingFilter : Before request [POST /interactivity]

interceptor 에 넘어오는 객체는

request = {ContentCachingRequestWrapper@13078} 
cachedContent = {ByteArrayOutputStream@14974} ""
contentCacheLimit = {Integer@14975} 10000
inputStream = null
reader = null
request = {RequestFacade@14921}
org.springframework.web.filter.CommonsRequestLoggingFilter
org.springframework.web.filter.AbstractRequestLoggingFilter
org.springframework.web.util.ContentCachingRequestWrapper

Limitation

  • ContentCachingRequestWrapper class only supports the following:
Content-Type:application/x-www-form-urlencoded
Method-Type:POST

application/x-www-form-urlencoded 만 지원하는가? 그렇지는 않는거 같은데 무슨 의미지? request.getParameterMap(); 메서드등을 지원하지 않는다는걸까?

inputStream 객체는 org.springframework.web.util.ContentCachingRequestWrapper.ContentCachingInputStream#ContentCachingInputStream 객체이다.

getInputStream에서 여러번 읽을 수 있을 줄 알았는데 그렇지는 않네.

뭐가 잘 못 된거지? inputStream 호출 후에 org.springframework.web.util.ContentCachingRequestWrapper#getContentAsByteArray 로 가져다 사용할 경우 아니면 prehandle 단계에서는 안되는건가? 뭐여 어따 쓰라는거여.

걍 CommonsRequestLoggingFilter 랑 같이 사용되는 클래스인가 보다. interceptor도 필요없는거 같은디.

protected String getMessagePayload(HttpServletRequest request) {
ContentCachingRequestWrapper wrapper =
WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
if (wrapper != null) {
byte[] buf = wrapper.getContentAsByteArray();
if (buf.length > 0) {
int length = Math.min(buf.length, getMaxPayloadLength());
try {
return new String(buf, 0, length, wrapper.getCharacterEncoding());
}
catch (UnsupportedEncodingException ex) {
return "[unknown]";
}
}
}
return null;
}

org.springframework.web.filter.CommonsRequestLoggingFilter#beforeRequest 에서 request payload 가 안 남는거는 아직 inputStream이 호출이 안되서 org.springframework.web.util.ContentCachingRequestWrapper#cachedContent 에 cache 가 안되었기 때문이다.

@Override
protected void beforeRequest(HttpServletRequest request, String message) {
logger.debug(message);
}

Q. HandlerInterceptorAdapter, HandlerInterceptor 차이는 무엇인가?

Related Information

https://www.baeldung.com/spring-http-logging

https://www.baeldung.com/spring-mvc-handlerinterceptor

--

--