2323
2424import javax .annotation .PostConstruct ;
2525
26- import org .springframework .beans .BeansException ;
2726import org .springframework .beans .factory .ObjectProvider ;
28- import org .springframework .beans .factory .config .BeanPostProcessor ;
2927import org .springframework .boot .WebApplicationType ;
3028import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
3129import org .springframework .boot .autoconfigure .AutoConfigureBefore ;
5553import org .springframework .context .annotation .Import ;
5654import org .springframework .context .annotation .ImportSelector ;
5755import org .springframework .core .type .AnnotationMetadata ;
58- import org .springframework .security .web .authentication .RememberMeServices ;
5956import org .springframework .session .ReactiveSessionRepository ;
6057import org .springframework .session .Session ;
6158import org .springframework .session .SessionRepository ;
6461import org .springframework .session .web .http .CookieSerializer ;
6562import org .springframework .session .web .http .DefaultCookieSerializer ;
6663import org .springframework .session .web .http .HttpSessionIdResolver ;
64+ import org .springframework .util .ClassUtils ;
6765import org .springframework .util .StringUtils ;
6866
6967/**
8785@ AutoConfigureBefore (HttpHandlerAutoConfiguration .class )
8886public class SessionAutoConfiguration {
8987
88+ private static final String REMEMBER_ME_SERVICES_CLASS = "org.springframework.security.web.authentication.RememberMeServices" ;
89+
9090 @ Configuration
9191 @ ConditionalOnWebApplication (type = Type .SERVLET )
9292 @ Import ({ ServletSessionRepositoryValidator .class ,
@@ -107,6 +107,11 @@ public DefaultCookieSerializer cookieSerializer(
107107 map .from (cookie ::getSecure ).to (cookieSerializer ::setUseSecureCookie );
108108 map .from (cookie ::getMaxAge ).to ((maxAge ) -> cookieSerializer
109109 .setCookieMaxAge ((int ) maxAge .getSeconds ()));
110+ if (ClassUtils .isPresent (REMEMBER_ME_SERVICES_CLASS ,
111+ getClass ().getClassLoader ())) {
112+ new RememberMeServicesCookieSerializerCustomizer ()
113+ .apply (cookieSerializer );
114+ }
110115 return cookieSerializer ;
111116 }
112117
@@ -118,33 +123,6 @@ static class ServletSessionRepositoryConfiguration {
118123
119124 }
120125
121- @ Configuration
122- @ ConditionalOnClass (RememberMeServices .class )
123- static class RememberMeServicesConfiguration {
124-
125- @ Bean
126- public BeanPostProcessor rememberMeServicesBeanPostProcessor (
127- ObjectProvider <SpringSessionRememberMeServices > springSessionRememberMeServices ) {
128- return new BeanPostProcessor () {
129-
130- @ Override
131- public Object postProcessBeforeInitialization (Object bean ,
132- String beanName ) throws BeansException {
133- if (bean instanceof DefaultCookieSerializer ) {
134- DefaultCookieSerializer cookieSerializer = (DefaultCookieSerializer ) bean ;
135- springSessionRememberMeServices
136- .ifAvailable ((rememberMeServices ) -> cookieSerializer
137- .setRememberMeRequestAttribute (
138- SpringSessionRememberMeServices .REMEMBER_ME_LOGIN_ATTR ));
139- }
140- return bean ;
141- }
142-
143- };
144- }
145-
146- }
147-
148126 }
149127
150128 @ Configuration
@@ -162,6 +140,19 @@ static class ReactiveSessionRepositoryConfiguration {
162140
163141 }
164142
143+ /**
144+ * Customization log for {@link SpringSessionRememberMeServices} that is only
145+ * instantiated when Spring Security is on the classpath.
146+ */
147+ static class RememberMeServicesCookieSerializerCustomizer {
148+
149+ public void apply (DefaultCookieSerializer cookieSerializer ) {
150+ cookieSerializer .setRememberMeRequestAttribute (
151+ SpringSessionRememberMeServices .REMEMBER_ME_LOGIN_ATTR );
152+ }
153+
154+ }
155+
165156 /**
166157 * Condition to trigger the creation of a {@link DefaultCookieSerializer}. This kicks
167158 * in if either no {@link HttpSessionIdResolver} and {@link CookieSerializer} beans
0 commit comments