38
38
import org .springframework .session .config .annotation .web .http .EnableSpringHttpSession ;
39
39
import org .springframework .session .web .http .CookieHttpSessionIdResolver ;
40
40
import org .springframework .session .web .http .DefaultCookieSerializer ;
41
+ import org .springframework .session .web .http .HeaderHttpSessionIdResolver ;
41
42
import org .springframework .session .web .http .SessionRepositoryFilter ;
42
43
import org .springframework .test .util .ReflectionTestUtils ;
43
44
@@ -185,6 +186,65 @@ public void sessionCookieConfigurationIsPickedUp() {
185
186
});
186
187
}
187
188
189
+ @ Test
190
+ public void autoConfiguredCookieSerializerConfiguration () {
191
+ this .contextRunner .withUserConfiguration (SessionRepositoryConfiguration .class )
192
+ .withPropertyValues ("server.servlet.session.cookie.name=sid" ,
193
+ "server.servlet.session.cookie.domain=spring" ,
194
+ "server.servlet.session.cookie.path=/test" ,
195
+ "server.servlet.session.cookie.httpOnly=false" ,
196
+ "server.servlet.session.cookie.secure=false" ,
197
+ "server.servlet.session.cookie.maxAge=10s" )
198
+ .run ((context ) -> {
199
+ DefaultCookieSerializer cookieSerializer = context
200
+ .getBean (DefaultCookieSerializer .class );
201
+ assertThat (cookieSerializer ).hasFieldOrPropertyWithValue ("cookieName" ,
202
+ "sid" );
203
+ assertThat (cookieSerializer ).hasFieldOrPropertyWithValue ("domainName" ,
204
+ "spring" );
205
+ assertThat (cookieSerializer ).hasFieldOrPropertyWithValue ("cookiePath" ,
206
+ "/test" );
207
+ assertThat (cookieSerializer )
208
+ .hasFieldOrPropertyWithValue ("useHttpOnlyCookie" , false );
209
+ assertThat (cookieSerializer )
210
+ .hasFieldOrPropertyWithValue ("useSecureCookie" , false );
211
+ assertThat (cookieSerializer )
212
+ .hasFieldOrPropertyWithValue ("cookieMaxAge" , 10 );
213
+ });
214
+ }
215
+
216
+ @ Test
217
+ public void userProvidedCookieSerializerConfiguration () {
218
+ this .contextRunner
219
+ .withUserConfiguration (UserProvidedCookieSerializerConfiguration .class )
220
+ .withPropertyValues ("server.servlet.session.cookie.name=sid" )
221
+ .run ((context ) -> {
222
+ DefaultCookieSerializer cookieSerializer = context
223
+ .getBean (DefaultCookieSerializer .class );
224
+ assertThat (cookieSerializer ).hasFieldOrPropertyWithValue ("cookieName" ,
225
+ "SESSION" );
226
+ });
227
+ }
228
+
229
+ @ Test
230
+ public void userProvidedCookieHttpSessionStrategyConfiguration () {
231
+ this .contextRunner
232
+ .withUserConfiguration (
233
+ UserProvidedCookieHttpSessionStrategyConfiguration .class )
234
+ .run ((context ) -> assertThat (
235
+ context .getBeansOfType (DefaultCookieSerializer .class ))
236
+ .isNotEmpty ());
237
+ }
238
+
239
+ @ Test
240
+ public void userProvidedHeaderHttpSessionStrategyConfiguration () {
241
+ this .contextRunner
242
+ .withUserConfiguration (
243
+ UserProvidedHeaderHttpSessionStrategyConfiguration .class )
244
+ .run ((context ) -> assertThat (
245
+ context .getBeansOfType (DefaultCookieSerializer .class )).isEmpty ());
246
+ }
247
+
188
248
@ Configuration
189
249
@ EnableSpringHttpSession
190
250
static class SessionRepositoryConfiguration {
@@ -201,4 +261,40 @@ static class ServerPropertiesConfiguration {
201
261
202
262
}
203
263
264
+ @ Configuration
265
+ @ EnableSpringHttpSession
266
+ static class UserProvidedCookieSerializerConfiguration
267
+ extends SessionRepositoryConfiguration {
268
+
269
+ @ Bean
270
+ public DefaultCookieSerializer myCookieSerializer () {
271
+ return new DefaultCookieSerializer ();
272
+ }
273
+
274
+ }
275
+
276
+ @ Configuration
277
+ @ EnableSpringHttpSession
278
+ static class UserProvidedCookieHttpSessionStrategyConfiguration
279
+ extends SessionRepositoryConfiguration {
280
+
281
+ @ Bean
282
+ public CookieHttpSessionIdResolver httpSessionStrategy () {
283
+ return new CookieHttpSessionIdResolver ();
284
+ }
285
+
286
+ }
287
+
288
+ @ Configuration
289
+ @ EnableSpringHttpSession
290
+ static class UserProvidedHeaderHttpSessionStrategyConfiguration
291
+ extends SessionRepositoryConfiguration {
292
+
293
+ @ Bean
294
+ public HeaderHttpSessionIdResolver httpSessionStrategy () {
295
+ return HeaderHttpSessionIdResolver .xAuthToken ();
296
+ }
297
+
298
+ }
299
+
204
300
}
0 commit comments