Merge pull request #16513 from vpavic

* pr/16513:
  Polish "Improve DefaultCookieSerializer auto-configuration"
  Improve DefaultCookieSerializer auto-configuration
pull/16502/head
Stephane Nicoll 6 years ago
commit 0875a07fe5

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -56,6 +56,7 @@ import org.springframework.core.type.AnnotationMetadata;
import org.springframework.session.ReactiveSessionRepository; import org.springframework.session.ReactiveSessionRepository;
import org.springframework.session.Session; import org.springframework.session.Session;
import org.springframework.session.SessionRepository; 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.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.CookieSerializer; import org.springframework.session.web.http.CookieSerializer;
import org.springframework.session.web.http.DefaultCookieSerializer; import org.springframework.session.web.http.DefaultCookieSerializer;
@ -91,8 +92,8 @@ public class SessionAutoConfiguration {
@Bean @Bean
@Conditional(DefaultCookieSerializerCondition.class) @Conditional(DefaultCookieSerializerCondition.class)
public DefaultCookieSerializer cookieSerializer( public DefaultCookieSerializer cookieSerializer(ServerProperties serverProperties,
ServerProperties serverProperties) { ObjectProvider<SpringSessionRememberMeServices> springSessionRememberMeServices) {
Cookie cookie = serverProperties.getServlet().getSession().getCookie(); Cookie cookie = serverProperties.getServlet().getSession().getCookie();
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer(); DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull(); PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
@ -103,6 +104,9 @@ public class SessionAutoConfiguration {
map.from(cookie::getSecure).to(cookieSerializer::setUseSecureCookie); map.from(cookie::getSecure).to(cookieSerializer::setUseSecureCookie);
map.from(cookie::getMaxAge).to((maxAge) -> cookieSerializer map.from(cookie::getMaxAge).to((maxAge) -> cookieSerializer
.setCookieMaxAge((int) maxAge.getSeconds())); .setCookieMaxAge((int) maxAge.getSeconds()));
springSessionRememberMeServices.ifAvailable((
rememberMeServices) -> cookieSerializer.setRememberMeRequestAttribute(
SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR));
return cookieSerializer; return cookieSerializer;
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.session.MapSessionRepository; import org.springframework.session.MapSessionRepository;
import org.springframework.session.SessionRepository; import org.springframework.session.SessionRepository;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession; 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.CookieHttpSessionIdResolver;
import org.springframework.session.web.http.DefaultCookieSerializer; import org.springframework.session.web.http.DefaultCookieSerializer;
import org.springframework.session.web.http.HeaderHttpSessionIdResolver; import org.springframework.session.web.http.HeaderHttpSessionIdResolver;
@ -245,6 +246,19 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
context.getBeansOfType(DefaultCookieSerializer.class)).isEmpty()); context.getBeansOfType(DefaultCookieSerializer.class)).isEmpty());
} }
@Test
public void autoConfiguredCookieSerializerIsConfiguredWithRememberMeRequestAttribute() {
this.contextRunner
.withUserConfiguration(SpringSessionRememberMeServicesConfiguration.class)
.run((context) -> {
DefaultCookieSerializer cookieSerializer = context
.getBean(DefaultCookieSerializer.class);
assertThat(cookieSerializer).hasFieldOrPropertyWithValue(
"rememberMeRequestAttribute",
SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR);
});
}
@Configuration @Configuration
@EnableSpringHttpSession @EnableSpringHttpSession
static class SessionRepositoryConfiguration { static class SessionRepositoryConfiguration {
@ -309,4 +323,16 @@ public class SessionAutoConfigurationTests extends AbstractSessionAutoConfigurat
} }
@Configuration
@EnableSpringHttpSession
static class SpringSessionRememberMeServicesConfiguration
extends SessionRepositoryConfiguration {
@Bean
public SpringSessionRememberMeServices rememberMeServices() {
return new SpringSessionRememberMeServices();
}
}
} }

Loading…
Cancel
Save