본문 바로가기
카테고리 없음

[스프링부트] 2.4.x Embed Tomcat 로그 중 Cookie 관련 오류 발생시

by simongs 2021. 4. 1.

문제현황

  • Spring Boot 2.4.4
  • Embed Tomcat 9.0
  • Web Page 테스트 중 아래와 같은 에러메시지 출력
[2021/04/01 22:50:58.204][ajp-nio-127.0.0.1-8009-exec-1][ERROR][[dispatcherServlet]:175] Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception
java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
    at org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:197)
    at org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:123)
    at org.apache.catalina.connector.Response.generateCookieString(Response.java:993)
    at org.apache.catalina.connector.Response.addCookie(Response.java:945)
    at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:385)

문제원인

예전 시스템의 쿠키 정보는 비표준 구분자로 구분된 정보를 쿠키에 저장했다.
여기에서 표준은 ;(세미콜론)을 의미하고 비표준은 ,(컴마)를 의미한다.
그래서 기존 비표준 구분자의 쿠키 파싱을 지원하고자 별도의 Bean을 등록한다.

해결방안

  • 아래 WebServerFactoryCustomizer

3.14. Use Tomcat’s LegacyCookieProcessor

By default, the embedded Tomcat used by Spring Boot does not support "Version 0" of the Cookie format, so you may see the following error:

java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value

If at all possible, you should consider updating your code to only store values compliant with later Cookie specifications. If, however, you cannot change the way that cookies are written, you can instead configure Tomcat to use a LegacyCookieProcessor. To switch to the LegacyCookieProcessor, use an WebServerFactoryCustomizer bean that adds a TomcatContextCustomizer, as shown in the following example:

@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
    return (factory) -> factory
            .addContextCustomizers((context) -> context.setCookieProcessor(new LegacyCookieProcessor()));
}

Reference

댓글