Login

Language :
Title톰캣과 스프링에서 서브 도메인과 세션을 공유하기
In Tomcat and Spring, sharing sessions with sub-domains.
Writer이지섭Write DateAug 25 2018Modify DateAug 9 2024View Count10636

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">
  <property name="cookieName" value="JSESSIONID"></property>
    <property name="cookiePath" value="/"></property>
  <property name="domainName" value="jisblee.me"></property>
   <!-- <property name="domainNamePattern" value="^.+?\.(\w+\.[a-z]+)$"></property> -->
</bean>
 
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://stackoverflow.com/questions/38696081/how-to-change-cookie-processor-to-legacycookieprocessor-in-tomcat-8

  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

Comment

Name               Password 
Content
Check Password.

Please enter your password when registering your comment.