Title | 톰캣과 스프링에서 서브 도메인과 세션을 공유하기 In Tomcat and Spring, sharing sessions with sub-domains. | ||||||
Writer | 이지섭 | Write Date | Aug 25 2018 | Modify Date | Aug 9 2024 | View Count | 12176 |
In Tomcat, sharing sessions with subdomains for identical application.
Below the directory, %TOMCAT_HOME%/webapps/ROOT make the below folder, META-INF and create context.xml file in that folder.
%TOMCAT_HOME%/webapps/ROOT/META-INF/context.xml
context.xml file content : <?xml version="1.0" encoding="UTF-8"?>
<Context sessionCookieDomain=".jisblee.me">
<CookieProcessor className="org.apache.tomcat.util.http.LegacyCookieProcessor" />
</Context>
Input desirable domain like sessionCookieDomain=".domainName", and designate cookie processor to LegacyCookieProcessor. Especially, there is no need to import specific library.
LegacyCookieProcessor class is in the /lib/tomcat-coyote.jar file.
The file encoding is UTF-8.
This configuration can be writable to below file, %TOMCAT_HOME%/conf/context.xml But it is enough to configure only to ROOT application.
So, make the below file and apply only to ROOT application. %TOMCAT_HOME%/webapps/ROOT/META-INF/context.xml
When configure like this, Sessions are shared with jisblee.me and www.jisblee.me domains.
For reference, in web application the folders, WEB-INF and META-INF, exactly below ROOT, isn't accessable by web browser.
And in the spring setting, the session cookie domain is set. package com....configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer;
@Configuration
public class SessionCookie {
@Bean
public CookieSerializer cookieSerializer() {
DefaultCookieSerializer serializer = new DefaultCookieSerializer();
serializer.setDomainName("jisblee.me");
serializer.setCookieName("JSESSIONID");
serializer.setCookiePath("/");
//serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$");
return serializer;
}
}
This is XML Configuration. <bean class="org.springframework.session.web.http.DefaultCookieSerializer"> Note that this approach does not work with tomcat 10.1.
The LegacyCookieProcessor class has been deprecated in tomcat 10.1.
Version 10.0 of tomcat will be.
[Web page referenced] http://kwonnam.pe.kr/wiki/java/tomcat https://tomcat.apache.org/tomcat-8.5-doc/config/context.html https://stackoverflow.com/questions/42524002/an-invalid-domain-was-specified-for-this-cookie https://tomcat.apache.org/tomcat-8.5-doc/config/cookie-processor.html https://stackoverflow.com/questions/35076622/how-to-configure-spring-sesssion-custom-cookie-by-xml | |||||||