This version is still in development and is not considered stable yet. For the latest stable version, please use Spring Session 3.4.0!spring-doc.cn

Spring Session - Custom Cookie

This guide describes how to configure Spring Session to use custom cookies with Java Configuration. The guide assumes you have already set up Spring Session in your project using your chosen data store. For example, HttpSession with Redis.spring-doc.cn

You can find the completed guide in the Custom Cookie sample application.

Once you have set up Spring Session, you can customize how the session cookie is written by exposing a CookieSerializer as a Spring bean. Spring Session comes with DefaultCookieSerializer. Exposing the DefaultCookieSerializer as a Spring bean augments the existing configuration when you use configurations like @EnableRedisHttpSession. The following example shows how to customize Spring Session’s cookie:spring-doc.cn

	@Bean
	public CookieSerializer cookieSerializer() {
		DefaultCookieSerializer serializer = new DefaultCookieSerializer();
		serializer.setCookieName("JSESSIONID"); (1)
		serializer.setCookiePath("/"); (2)
		serializer.setDomainNamePattern("^.+?\\.(\\w+\\.[a-z]+)$"); (3)
		return serializer;
	}
1 We customize the name of the cookie to be JSESSIONID.
2 We customize the path of the cookie to be / (rather than the default of the context root).
3 We customize the domain name pattern (a regular expression) to be ^.?\\.(\\w\\.[a-z]+)$. This allows sharing a session across domains and applications. If the regular expression does not match, no domain is set and the existing domain is used. If the regular expression matches, the first grouping is used as the domain. This means that a request to child.example.com sets the domain to example.com. However, a request to localhost:8080/ or 192.168.1.100:8080/ leaves the cookie unset and, thus, still works in development without any changes being necessary for production.
You should only match on valid domain characters, since the domain name is reflected in the response. Doing so prevents a malicious user from performing such attacks as HTTP Response Splitting.

The following configuration options are available:spring-doc.cn

  • cookieName: The name of the cookie to use. Default: SESSION.spring-doc.cn

  • useSecureCookie: Specifies whether a secure cookie should be used. Default: Use the value of HttpServletRequest.isSecure() at the time of creation.spring-doc.cn

  • cookiePath: The path of the cookie. Default: The context root.spring-doc.cn

  • cookieMaxAge: Specifies the max age of the cookie to be set at the time the session is created. Default: -1, which indicates the cookie should be removed when the browser is closed.spring-doc.cn

  • jvmRoute: Specifies a suffix to be appended to the session ID and included in the cookie. Used to identify which JVM to route to for session affinity. With some implementations (that is, Redis) this option provides no performance benefit. However, it can help with tracing logs of a particular user.spring-doc.cn

  • domainName: Allows specifying a specific domain name to be used for the cookie. This option is simple to understand but often requires a different configuration between development and production environments. See domainNamePattern as an alternative.spring-doc.cn

  • domainNamePattern: A case-insensitive pattern used to extract the domain name from the HttpServletRequest#getServerName(). The pattern should provide a single grouping that is used to extract the value of the cookie domain. If the regular expression does not match, no domain is set and the existing domain is used. If the regular expression matches, the first grouping is used as the domain.spring-doc.cn

  • sameSite: The value for the SameSite cookie directive. To disable the serialization of the SameSite cookie directive, you may set this value to null. Default: Laxspring-doc.cn

You should only match on valid domain characters, since the domain name is reflected in the response. Doing so prevents a malicious user from performing such attacks as HTTP Response Splitting.

This section describes how to work with the custom-cookie sample application.spring-doc.cn

You can run the sample by obtaining the source code and invoking the following command:spring-doc.cn

$ ./gradlew :spring-session-sample-javaconfig-custom-cookie:tomcatRun
For the sample to work, you must install Redis 2.8+ on localhost and run it with the default port (6379). Alternatively, you can update the RedisConnectionFactory to point to a Redis server. Another option is to use Docker to run Redis on localhost. See Docker Redis repository for detailed instructions.

You should now be able to access the application at localhost:8080/spring-doc.cn

Now you can use the application. Fill out the form with the following information:spring-doc.cn

Now click the Set Attribute button. You should now see the values displayed in the table.spring-doc.cn

If you look at the cookies for the application, you can see the cookie is saved to the custom name of JSESSIONID.spring-doc.cn