반응형

스프링 인터셉터 - 요청 로그


LogInterceptor - 요청 로그 인터셉터

  • String uuid = UUID.randomUUID().toString()
    • 요청 로그를 구분하기 위한 uuid를 생성한다.
  • request.setAttribute(LOG_ID, uuid)
    • 서블릿 필터의 경우 지역변수로 해결이 가능하지만, 스프링 인터셉터는 호출 시점이 완전히 분리되어 있다.
      따라서 preHandle에서 지정한 값을 postHandle, afterCompletion에서 함께 사용하려면 어딘가에 담아두어야 한다. LogInterceptor도 싱글톤 처럼 사용되기 때문에 멤버 변수를 사용하면 위험하다. 따라서 request에 담아두었다. 이 값은 afterCompletion에서 request.getAttribute(LOG_ID)로 찾아서 사용한다.
  • return true
    • true면 정상 호출이다. 다음 인터셉터나 컨트롤러가 호출된다.

HandlerMethod
핸들러 정보는 어떤 핸들러 매핑을 사용하는가에 따라 달라진다. 스프링을 사용하면 일반적으로 @Controller, @RequestMapping을 활용한 핸들러 매핑을 사용하는데, 이 경우 핸들러 정보로 HandlerMethod가 넘어온다.


ResourceHttpReuqestHandler
@Controller가 아니라 /resouces/static와 같은 정적 리소스가 호출되는 경우
ResourceHttpRequestHandler가 핸들러 정보로 넘어오기 때문에 타입에 따라서 처리가 필요하다.


postHandle, afterCompletion
종료 로그를 postHandle이 아니라 afterCompletion에서 실행한 이유는, 예외가 발생한 경우 postHandle가 호출되지 않기 때문이다.
afterCompletion은 예외가 발생해도 호출 되는 것을 보장한다.

 

WebConfig - 인터셉터 등록

인터셉터와 필터가 중복되지 않도록 필터를 등록하기 위한 logFilter()@Bean은 주석처리하자.

WebMvcConfigure가 제공하는 addInterceptors()를 사용해서 인터셉터를 등록할 수 있다.

  • registry.addInterceptor(new LogInterceptor()) : 인터셉터를 등록한다.
  • order(1) : 인터셉터의 호출 순서를 지정한다. 낮을 수록 먼저 호출된다.
  • addPathPatterns("/**") : 인터 셉터를 적용할 URL 패턴을 지정한다.
  • excludePathPatterns("/css/**", "/*.ico", "/error") : 인터셉터에서 제외할 패턴을 지정한다.


필터와 비교해보면 인터셉터는 addPathPatters, excludePathPatters로 매우 정밀하게 URL 패턴을 지정할 수 있다.


실행 로그

 

스프링의 URL 경로
스프링이 제공하는 URL 경로는 서블릿 기술이 제공하는 URL 경로와 완전히 다르다. 더욱 자세하고, 세밀하게 설정할 수 있다.

자세한 내용은 다음을 참고하자.

PathPattern 공식 문서

? 한 문자 일치
* 경로(/) 안에서 0개 이상의 문자 일치
** 경로 끝까지 0개 이상의 경로(/) 일치
{spring} 경로(/)와 일치하고 spring이라는 변수로 캡처
{spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring"
{spring:[a-z]+} regexp [a-z]+ 와 일치하고, "spring" 경로 변수로 캡처
{*spring} 경로가 끝날 때 까지 0개 이상의 경로(/)와 일치하고 spring이라는 변수로 캡처


/pages/t?st.html — matches /pages/test.html, /pages/tXst.html but not /pages/
toast.html
/resources/*.png — matches all .png files in the resources directory
/resources/** — matches all files underneath the /resources/ path, including /
resources/image.png and /resources/css/spring.css
/resources/{*path} — matches all files underneath the /resources/ path and
captures their relative path in a variable named "path"; /resources/image.png
will match with "path" → "/image.png", and /resources/css/spring.css will match
with "path" → "/css/spring.css"
/resources/{filename:\\w+}.dat will match /resources/spring.dat and assign the
value "spring" to the filename variable

링크 : https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/pattern/PathPattern.html

 

반응형