23
23
24
24
import javax .annotation .PostConstruct ;
25
25
26
- import org .springframework .beans .BeansException ;
27
26
import org .springframework .beans .factory .ObjectProvider ;
28
- import org .springframework .beans .factory .config .BeanPostProcessor ;
29
27
import org .springframework .boot .WebApplicationType ;
30
28
import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
31
29
import org .springframework .boot .autoconfigure .AutoConfigureBefore ;
55
53
import org .springframework .context .annotation .Import ;
56
54
import org .springframework .context .annotation .ImportSelector ;
57
55
import org .springframework .core .type .AnnotationMetadata ;
58
- import org .springframework .security .web .authentication .RememberMeServices ;
59
56
import org .springframework .session .ReactiveSessionRepository ;
60
57
import org .springframework .session .Session ;
61
58
import org .springframework .session .SessionRepository ;
64
61
import org .springframework .session .web .http .CookieSerializer ;
65
62
import org .springframework .session .web .http .DefaultCookieSerializer ;
66
63
import org .springframework .session .web .http .HttpSessionIdResolver ;
64
+ import org .springframework .util .ClassUtils ;
67
65
import org .springframework .util .StringUtils ;
68
66
69
67
/**
87
85
@ AutoConfigureBefore (HttpHandlerAutoConfiguration .class )
88
86
public class SessionAutoConfiguration {
89
87
88
+ private static final String REMEMBER_ME_SERVICES_CLASS = "org.springframework.security.web.authentication.RememberMeServices" ;
89
+
90
90
@ Configuration
91
91
@ ConditionalOnWebApplication (type = Type .SERVLET )
92
92
@ Import ({ ServletSessionRepositoryValidator .class ,
@@ -107,6 +107,11 @@ public DefaultCookieSerializer cookieSerializer(
107
107
map .from (cookie ::getSecure ).to (cookieSerializer ::setUseSecureCookie );
108
108
map .from (cookie ::getMaxAge ).to ((maxAge ) -> cookieSerializer
109
109
.setCookieMaxAge ((int ) maxAge .getSeconds ()));
110
+ if (ClassUtils .isPresent (REMEMBER_ME_SERVICES_CLASS ,
111
+ getClass ().getClassLoader ())) {
112
+ new RememberMeServicesCookieSerializerCustomizer ()
113
+ .apply (cookieSerializer );
114
+ }
110
115
return cookieSerializer ;
111
116
}
112
117
@@ -118,33 +123,6 @@ static class ServletSessionRepositoryConfiguration {
118
123
119
124
}
120
125
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
-
148
126
}
149
127
150
128
@ Configuration
@@ -162,6 +140,19 @@ static class ReactiveSessionRepositoryConfiguration {
162
140
163
141
}
164
142
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
+
165
156
/**
166
157
* Condition to trigger the creation of a {@link DefaultCookieSerializer}. This kicks
167
158
* in if either no {@link HttpSessionIdResolver} and {@link CookieSerializer} beans
0 commit comments