https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-handlermapping-path
Web on Servlet Stack
Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, "Spring Web MVC," comes from the name of its source module (spring-webmvc), but it is more commonl
docs.spring.io
틀린 해석이나 내용이 있다면 알려주세요 😎
1.1.6. Path Matching
Servlet API는 전체 Request path를 requestURI로 노출하고 이를 Servlet이 매핑되는 방식에 따라 값이 달라지도록 contextPath, servletPath 및 pathInfo로 세분화한다
Spring MVC는 매핑 핸들러에서 사용할 lookup root를 결정하고, 해당되는 경우에 contextPath 및 모든 servletMapping 접두사를 제외한다
servletPath 및 pathInfo가 decoding 되어 lookup path를 파생시키기 위해 전체 requestURI와 직접 비교할 수 없고, requestURI를 decoding 해야한다.
그런데 경로에 "/" 또는 ";" 인코딩 예약 문자가 포함되어 문제가 발생할 수 있다.
이는 디코딩 후 경로의 구조 변경으로 보안 문제를 일으킬 수 있다.
또한 Servlet Container 는 servletPath를 다양한 수준으로 정규화할 수 있어 requestURI에 대한 startsWith 비교를 수행할 수 없다
접두사 기반 servletPath mapping type과 함께 servletPath에 의존성을 피해야한다.
DispatcherServlet이 "/" 기본 서블릿으로 매핑되거나 "/*" 접두사가 없는 경우 이를 감지하고 servletPath 및 pathInfo를 사용하지않도록 할 수 있다
Servlet Container에서 동일 서블릿 매핑 유형을 assuming하면 MVC구성의 경로 일치로 UrlPathHelper에 alwaysUseFullPath=true 로 동등한 결과를 얻을 수 있다.
기본 서블릿 매핑 "/"는 좋은 선택이지만, controller mapping과 비교할 수 있도록 하려면 requestURI를 디코딩해야하는 문제가 있다
경로구조를 변경하는 예약된 문자를 디코딩할 수 있기 때문에 좋지않다. 문자가 잘못 디코딩되면 Spring Security HTTP 방화벽에서 거부하거나, alwaysUseFullPath=false로 구성된다
컨트롤러 매핑은 인코딩된 경로와 일치해야한다.
DispatcherServlet은 URL 은 다른 Servlet과 공유해야 하며 접두사로 매핑해야 할 수도 있다.
이러한 문제 때문에 PathPatternParser 및 분석된 패턴을 AntPathMatcher와 일치하는 문자열 경로로 대치하여 사용하여 해결한다
PathPatternParser 는 6.0 부터 기본적 활성화 되어있다.
AntPathMatcher는 디코딩 된 조회 경로 또는 인코딩 컨트롤러 매핑이 필요하지만 PathPattern을 사용하면 한번에 하나의 세그먼트인 RequestPath 경로의 표현과 일치할 수 있다
경로 구조 변경할 위험 없이 경로 세그먼트 값을 개별적으로 처리할 수 있다
구문이 분석된 PathPattern은 Servlet path mapping되어 접두사가 단순하게 유지되고 servletPath접두사 매핑의 사용을 지원한다
Web on Servlet Stack
Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, "Spring Web MVC," comes from the name of its source module (spring-webmvc), but it is more commonl
docs.spring.io
1.1.7. Interception
모든 HandlerMapping 구현은 특성 요청에 특정 기능을 적용하려는 경우(checking for a principal) 에 핸들러에서 인터셉트를 할 수 있다
인터셉터는 모든 종류의 pre-processing , post-processing 수행을 할 수 있도록 세가지 메서드 이용하여 구현한다
- preHandle(..): Before the actual handler is run
- postHandle(..): After the handler is run
- afterCompletion(..): After the complete request has finished
prehandler 는 return bool 로 실행 체인의 처리를 중단하거나 계속할 수 있다
true 일 경우 인터셉터 하여 다음 작업을 진행할 수 있도록 핸들링한다
false 일 경우 DispatcherServlet 가 자체 요청 처리한다
HandlerMapping개별 구현 에서 setter를 사용하여 직접 등록할 수도 있다 .
https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-config-interceptors
Web on Servlet Stack
Spring Web MVC is the original web framework built on the Servlet API and has been included in the Spring Framework from the very beginning. The formal name, "Spring Web MVC," comes from the name of its source module (spring-webmvc), but it is more commonl
docs.spring.io
postHandler method는 응답이 HandlerAdapter 내에서 postHandle 앞에 쓰여지고 있거나 commit되는 @ResponseBody, ResoponseEntity method 에서 쓰기에는 유용하지 않다
헤더를 추가한다거나 하는 응답 변경에 사용하기에는 느리기 때문에
ResponseBodyAdvice를 구현하여 이를 ControllerAdvice bean에 선언하거나 RequestMappingHandlerAdapter에서 직접 구성하는게 좋다
'Web > spring' 카테고리의 다른 글
[Spring framework Web MVC docs] 1.2. Filters (0) | 2023.02.11 |
---|---|
[Spring framework Web MVC docs] 1.12. MVC Config (0) | 2023.02.11 |
[Spring framework testing] 5. Spring TestContext Framework (2) (0) | 2023.02.09 |
[Spring framework testing] 5. Spring TestContext Framework (1) (0) | 2023.02.09 |
[Spring security] Architecture (0) | 2023.02.08 |