본문 바로가기

Web/spring

[Spring framework Web MVC docs] 1.2. Filters

728x90

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#filters

 

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

 

해석이 틀렸다면 알려주세요 😀

 


 

Spring-web module 은 몇 가지의 필터를 제공한다.

 

 

1.2.1. Form Data

브라우저는 HTTP GET 또는 POST 를 통해 양식 데이터를 제출 할 수 있지만, 클라이언트도 PUT, PATCH, DELETE 를 사용할 수 있다.

Servlet API 에는 ServletRequest,getParameter*() 메소드를 이용하여 오직 POST 로 양식 필드에 액세스 지원만 하지만

Spring-web 모듈을 사용하면 FormContentFilter application/x-www-form-urlencoded 콘텐츠 유형으로 PUT, PATCH 및 DELETE 요청을 가로채고 요청 본문에서 양식 데이터를 읽고 ServletRequest를 래핑하여 FormContentFilter를 제공한다.

 

<form method="post>
	<input type="hidden" name="_method" value="put"/>
</form>

이렇게 작성하면, Spring-web 에서 put 으로 받아들일 수 있다

 

 

 

1.2.2. Forwarded Headers

Request 이 proxy(load balancers) 를 통과하면서 host, port, scheme 가 변경 될 수 있기 때문에 클라이언트가 봤을 때 올바른 host, port, scheme 의 링크를 만드는게 어렵다

 

RFC 7239는 프록시가 원래 요청에 대한 정보를 제공하는 데 사용할 수 있는 전달된 HTTP 헤더를 정의할 수 있다.

X-Forwarded-Host,

X-Forwarded-Port,

X-Forwarded-Proto,

X-Forwarded-Ssl,

X-Forwarded-Prefix를 비롯한 다른 비표준 헤더도 있다.

 

ForwardedHeaderFilter는

a) 전달된 헤더를 기반으로 호스트, 포트 및 체계를 변경하고

b) 추가 영향을 제거하기 위해 이러한 헤더를 제거하기 위해 요청을 수정하는 

서블릿 필터이다.

필터는 요청 래핑에 의존하므로 원래 요청이 아닌 수정된 요청과 함께 작동해야 하는 RequestContextFilter와 같은 다른 필터보다 순서가 먼저여야한다.

 

 

Header 가 의도한대로 프록시에 추가 되었는지, 또는 악의적인 클라이언트에 의해 추가 되었는지 애플리케이션이 알 수 없기 때문에 전달된 헤더에 대한 security considerations 가 있다.

외부에서 들어오는 신뢰할 수 있는 Forwarded header 를 제거하도록 boundary of trust 에 있는 프록시를 구성해야한다

removeOnly=true로 ForwardedHeaderFilter를 구성할 수도 있으나, 이 경우 헤더를 제거하지만 사용하지는 않는다

 

비동기 요청 및 오류 발송을 지원하려면 이 필터를 DispatcherType.ASYNC 및 DispatcherType.ERROR로 매핑한다

Spring Framework의 AbstractAnnotationConfigDispatcherServletInitializer(Servlet Config 참조)를 사용하는 경우 모든 디스패치 유형에 대해 모든 필터가 자동으로 등록된다. 그러나 web.xml을 통해 필터를 등록하거나 FilterRegistrationBean을 통해 Spring Boot에서 필터를 등록하는 경우 DispatcherType.REQUEST 외에도 DispatcherType.ASYNC 및 DispatcherType.ERROR를 포함해야한다.

 

 

 

1.2.3. Shallow ETag

ShallowEtagHeaderFilter 필터는 응답에 작성된 콘텐츠를 캐싱하고 여기에서 MD5 해시를 계산하여 "얕은" ETag를 생성한다

클라이언트가 다음 번에 전송할 때 동일한 작업을 수행하지만 계산된 값을 If-None-Match 요청 헤더와 비교하고 둘이 같으면 304(NOT_MODIFIED)를 반환해준다.

이 strategy는 네트워크 대역폭을 절약하지만 각 요청에 대해 전체 응답을 계산해야 하므로 CPU는 절약하지 않습니다. 앞에서 설명한 컨트롤러 수준의 다른 전략은 계산을 피할 수 있다. See HTTP Caching.

 

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

 

이 필터에는 writeWeakETag다음과 유사한 약한 ETag를 작성하도록 필터를 구성하는 매개변수가 있습니다 ( RFC 7232 섹션 2.3W/"02a2d595e6ed9a0b24f027f2b63b134d6" 에 정의됨 ).

 

비동기 요청 을 지원 DispatcherType.ASYNC하려면 필터가 마지막 비동기 디스패치의 끝까지 ETag를 지연시키고 성공적으로 생성할 수 있도록 이 필터를 매핑해야한다

Spring Framework AbstractAnnotationConfigDispatcherServletInitializer( Servlet Config 참조 )를 사용하는 경우 모든 디스패치 유형에 대해 모든 필터가 자동으로 등록된다. 그러나 web.xml또는 Spring Boot에서 필터를 등록하는 FilterRegistrationBean경우 반드시 DispatcherType.ASYNC 를 포함해야한다

 

 

1.2.4. CORS

 

Spring MVC provides fine-grained support for CORS configuration through annotations on controllers. However, when used with Spring Security, we advise relying on the built-in CorsFilter that must be ordered ahead of Spring Security’s chain of filters.

https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-cors

728x90