53
53
import org .springframework .context .annotation .Import ;
54
54
import org .springframework .context .annotation .ImportSelector ;
55
55
import org .springframework .core .type .AnnotationMetadata ;
56
+ import org .springframework .security .web .authentication .RememberMeServices ;
56
57
import org .springframework .session .ReactiveSessionRepository ;
57
58
import org .springframework .session .Session ;
58
59
import org .springframework .session .SessionRepository ;
61
62
import org .springframework .session .web .http .CookieSerializer ;
62
63
import org .springframework .session .web .http .DefaultCookieSerializer ;
63
64
import org .springframework .session .web .http .HttpSessionIdResolver ;
64
- import org .springframework .util .ClassUtils ;
65
65
66
66
/**
67
67
* {@link EnableAutoConfiguration Auto-configuration} for Spring Session.
83
83
@ AutoConfigureBefore (HttpHandlerAutoConfiguration .class )
84
84
public class SessionAutoConfiguration {
85
85
86
- private static final String REMEMBER_ME_SERVICES_CLASS = "org.springframework.security.web.authentication.RememberMeServices" ;
87
-
88
86
@ Configuration (proxyBeanMethods = false )
89
87
@ ConditionalOnWebApplication (type = Type .SERVLET )
90
88
@ Import ({ ServletSessionRepositoryValidator .class , SessionRepositoryFilterConfiguration .class })
91
89
static class ServletSessionConfiguration {
92
90
93
91
@ Bean
94
92
@ Conditional (DefaultCookieSerializerCondition .class )
95
- DefaultCookieSerializer cookieSerializer (ServerProperties serverProperties ) {
93
+ DefaultCookieSerializer cookieSerializer (ServerProperties serverProperties ,
94
+ ObjectProvider <CookieSerializerCustomizer > cookieSerializerCustomizers ) {
96
95
Cookie cookie = serverProperties .getServlet ().getSession ().getCookie ();
97
96
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer ();
98
97
PropertyMapper map = PropertyMapper .get ().alwaysApplyingWhenNonNull ();
@@ -102,12 +101,22 @@ DefaultCookieSerializer cookieSerializer(ServerProperties serverProperties) {
102
101
map .from (cookie ::getHttpOnly ).to (cookieSerializer ::setUseHttpOnlyCookie );
103
102
map .from (cookie ::getSecure ).to (cookieSerializer ::setUseSecureCookie );
104
103
map .from (cookie ::getMaxAge ).to ((maxAge ) -> cookieSerializer .setCookieMaxAge ((int ) maxAge .getSeconds ()));
105
- if (ClassUtils .isPresent (REMEMBER_ME_SERVICES_CLASS , getClass ().getClassLoader ())) {
106
- new RememberMeServicesCookieSerializerCustomizer ().apply (cookieSerializer );
107
- }
104
+ cookieSerializerCustomizers .orderedStream ().forEach ((customizer ) -> customizer .customize (cookieSerializer ));
108
105
return cookieSerializer ;
109
106
}
110
107
108
+ @ Configuration (proxyBeanMethods = false )
109
+ @ ConditionalOnClass (RememberMeServices .class )
110
+ static class RememberMeServicesConfiguration {
111
+
112
+ @ Bean
113
+ CookieSerializerCustomizer rememberMeServicesCookieSerializerCustomizer () {
114
+ return (cookieSerializer ) -> cookieSerializer
115
+ .setRememberMeRequestAttribute (SpringSessionRememberMeServices .REMEMBER_ME_LOGIN_ATTR );
116
+ }
117
+
118
+ }
119
+
111
120
@ Configuration (proxyBeanMethods = false )
112
121
@ ConditionalOnMissingBean (SessionRepository .class )
113
122
@ Import ({ ServletSessionRepositoryImplementationValidator .class ,
@@ -133,18 +142,6 @@ static class ReactiveSessionRepositoryConfiguration {
133
142
134
143
}
135
144
136
- /**
137
- * Customization for {@link SpringSessionRememberMeServices} that is only instantiated
138
- * when Spring Security is on the classpath.
139
- */
140
- static class RememberMeServicesCookieSerializerCustomizer {
141
-
142
- void apply (DefaultCookieSerializer cookieSerializer ) {
143
- cookieSerializer .setRememberMeRequestAttribute (SpringSessionRememberMeServices .REMEMBER_ME_LOGIN_ATTR );
144
- }
145
-
146
- }
147
-
148
145
/**
149
146
* Condition to trigger the creation of a {@link DefaultCookieSerializer}. This kicks
150
147
* in if either no {@link HttpSessionIdResolver} and {@link CookieSerializer} beans
0 commit comments