diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java index 9fd5b3a140..4f95f8bad3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java @@ -56,6 +56,7 @@ import org.springframework.core.type.AnnotationMetadata; import org.springframework.session.ReactiveSessionRepository; import org.springframework.session.Session; import org.springframework.session.SessionRepository; +import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices; import org.springframework.session.web.http.CookieHttpSessionIdResolver; import org.springframework.session.web.http.CookieSerializer; import org.springframework.session.web.http.DefaultCookieSerializer; @@ -90,8 +91,8 @@ public class SessionAutoConfiguration { @Bean @Conditional(DefaultCookieSerializerCondition.class) - public DefaultCookieSerializer cookieSerializer( - ServerProperties serverProperties) { + public DefaultCookieSerializer cookieSerializer(ServerProperties serverProperties, + ObjectProvider springSessionRememberMeServices) { Cookie cookie = serverProperties.getServlet().getSession().getCookie(); DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); @@ -102,6 +103,9 @@ public class SessionAutoConfiguration { map.from(cookie::getSecure).to(cookieSerializer::setUseSecureCookie); map.from(cookie::getMaxAge).to((maxAge) -> cookieSerializer .setCookieMaxAge((int) maxAge.getSeconds())); + springSessionRememberMeServices.ifAvailable(( + rememberMeServices) -> cookieSerializer.setRememberMeRequestAttribute( + SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR)); return cookieSerializer; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java index 15dce5c457..c07e6bceda 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/SessionAutoConfigurationTests.java @@ -34,6 +34,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.session.MapSessionRepository; import org.springframework.session.SessionRepository; import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession; +import org.springframework.session.security.web.authentication.SpringSessionRememberMeServices; import org.springframework.session.web.http.CookieHttpSessionIdResolver; import org.springframework.session.web.http.DefaultCookieSerializer; import org.springframework.session.web.http.HeaderHttpSessionIdResolver; @@ -245,6 +246,18 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat context.getBeansOfType(DefaultCookieSerializer.class)).isEmpty()); } + @Test + public void autoConfiguredCookieSerializerIsConfiguredWithRememberMeRequestAttribute() { + this.contextRunner.withBean(SpringSessionRememberMeServicesConfiguration.class) + .run((context) -> { + DefaultCookieSerializer cookieSerializer = context + .getBean(DefaultCookieSerializer.class); + assertThat(cookieSerializer).hasFieldOrPropertyWithValue( + "rememberMeRequestAttribute", + SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR); + }); + } + @Configuration(proxyBeanMethods = false) @EnableSpringHttpSession static class SessionRepositoryConfiguration { @@ -309,4 +322,16 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat } + @Configuration(proxyBeanMethods = false) + @EnableSpringHttpSession + static class SpringSessionRememberMeServicesConfiguration + extends SessionRepositoryConfiguration { + + @Bean + public SpringSessionRememberMeServices rememberMeServices() { + return new SpringSessionRememberMeServices(); + } + + } + }