26
26
27
27
import org .springframework .boot .autoconfigure .AutoConfigurations ;
28
28
import org .springframework .boot .autoconfigure .web .ServerProperties ;
29
- import org .springframework .boot .autoconfigure .web .servlet .ServletWebServerFactoryAutoConfiguration ;
30
29
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
31
30
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
32
31
import org .springframework .boot .web .servlet .FilterRegistrationBean ;
33
- import org .springframework .boot .web .servlet .context .AnnotationConfigServletWebServerApplicationContext ;
34
32
import org .springframework .context .annotation .Bean ;
35
33
import org .springframework .context .annotation .Configuration ;
36
34
import org .springframework .session .MapSessionRepository ;
37
35
import org .springframework .session .SessionRepository ;
38
36
import org .springframework .session .config .annotation .web .http .EnableSpringHttpSession ;
39
37
import org .springframework .session .web .http .CookieHttpSessionIdResolver ;
40
38
import org .springframework .session .web .http .DefaultCookieSerializer ;
39
+ import org .springframework .session .web .http .HeaderHttpSessionIdResolver ;
40
+ import org .springframework .session .web .http .HttpSessionIdResolver ;
41
41
import org .springframework .session .web .http .SessionRepositoryFilter ;
42
42
import org .springframework .test .util .ReflectionTestUtils ;
43
43
44
44
import static org .assertj .core .api .Assertions .assertThat ;
45
+ import static org .mockito .Mockito .mock ;
45
46
46
47
/**
47
48
* Tests for {@link SessionAutoConfiguration}.
@@ -165,25 +166,83 @@ public void filterDispatcherTypesCanBeCustomized() {
165
166
}
166
167
167
168
@ Test
168
- public void sessionCookieConfigurationIsPickedUp () {
169
- WebApplicationContextRunner webRunner = new WebApplicationContextRunner (
170
- AnnotationConfigServletWebServerApplicationContext ::new )
171
- .withConfiguration (AutoConfigurations
172
- .of (ServletWebServerFactoryAutoConfiguration .class ))
173
- .withUserConfiguration (SessionRepositoryConfiguration .class )
174
- .withPropertyValues ("server.port=0" ,
175
- "server.servlet.session.cookie.name=testname" );
176
- webRunner .run ((context ) -> {
177
- SessionRepositoryFilter <?> filter = context
178
- .getBean (SessionRepositoryFilter .class );
179
- CookieHttpSessionIdResolver sessionIdResolver = (CookieHttpSessionIdResolver ) ReflectionTestUtils
180
- .getField (filter , "httpSessionIdResolver" );
181
- DefaultCookieSerializer cookieSerializer = (DefaultCookieSerializer ) ReflectionTestUtils
182
- .getField (sessionIdResolver , "cookieSerializer" );
183
- String cookieName = (String ) ReflectionTestUtils .getField (cookieSerializer ,
184
- "cookieName" );
185
- assertThat (cookieName ).isEqualTo ("testname" );
186
- });
169
+ public void sessionCookieConfigurationIsAppliedToAutoConfiguredCookieSerializer () {
170
+ this .contextRunner .withUserConfiguration (SessionRepositoryConfiguration .class )
171
+ .withPropertyValues ("server.servlet.session.cookie.name=sid" ,
172
+ "server.servlet.session.cookie.domain=spring" ,
173
+ "server.servlet.session.cookie.path=/test" ,
174
+ "server.servlet.session.cookie.httpOnly=false" ,
175
+ "server.servlet.session.cookie.secure=false" ,
176
+ "server.servlet.session.cookie.maxAge=10s" )
177
+ .run ((context ) -> {
178
+ DefaultCookieSerializer cookieSerializer = context
179
+ .getBean (DefaultCookieSerializer .class );
180
+ assertThat (cookieSerializer ).hasFieldOrPropertyWithValue ("cookieName" ,
181
+ "sid" );
182
+ assertThat (cookieSerializer ).hasFieldOrPropertyWithValue ("domainName" ,
183
+ "spring" );
184
+ assertThat (cookieSerializer ).hasFieldOrPropertyWithValue ("cookiePath" ,
185
+ "/test" );
186
+ assertThat (cookieSerializer )
187
+ .hasFieldOrPropertyWithValue ("useHttpOnlyCookie" , false );
188
+ assertThat (cookieSerializer )
189
+ .hasFieldOrPropertyWithValue ("useSecureCookie" , false );
190
+ assertThat (cookieSerializer )
191
+ .hasFieldOrPropertyWithValue ("cookieMaxAge" , 10 );
192
+ });
193
+ }
194
+
195
+ @ Test
196
+ public void autoConfiguredCookieSerializerIsUsedBySessionRepositoryFilter () {
197
+ this .contextRunner .withUserConfiguration (SessionRepositoryConfiguration .class )
198
+ .withPropertyValues ("server.port=0" ).run ((context ) -> {
199
+ SessionRepositoryFilter <?> filter = context
200
+ .getBean (SessionRepositoryFilter .class );
201
+ CookieHttpSessionIdResolver sessionIdResolver = (CookieHttpSessionIdResolver ) ReflectionTestUtils
202
+ .getField (filter , "httpSessionIdResolver" );
203
+ DefaultCookieSerializer cookieSerializer = (DefaultCookieSerializer ) ReflectionTestUtils
204
+ .getField (sessionIdResolver , "cookieSerializer" );
205
+ assertThat (cookieSerializer )
206
+ .isSameAs (context .getBean (DefaultCookieSerializer .class ));
207
+ });
208
+ }
209
+
210
+ @ Test
211
+ public void autoConfiguredCookieSerializerBacksOffWhenUserConfiguresACookieSerializer () {
212
+ this .contextRunner
213
+ .withUserConfiguration (UserProvidedCookieSerializerConfiguration .class )
214
+ .run ((context ) -> {
215
+ assertThat (context ).hasSingleBean (DefaultCookieSerializer .class );
216
+ assertThat (context ).hasBean ("myCookieSerializer" );
217
+ });
218
+ }
219
+
220
+ @ Test
221
+ public void cookiesSerializerIsAutoConfiguredWhenUserConfiguresCookieHttpSessionIdResolver () {
222
+ this .contextRunner
223
+ .withUserConfiguration (
224
+ UserProvidedCookieHttpSessionStrategyConfiguration .class )
225
+ .run ((context ) -> assertThat (
226
+ context .getBeansOfType (DefaultCookieSerializer .class ))
227
+ .isNotEmpty ());
228
+ }
229
+
230
+ @ Test
231
+ public void autoConfiguredCookieSerializerBacksOffWhenUserConfiguresHeaderHttpSessionIdResolver () {
232
+ this .contextRunner
233
+ .withUserConfiguration (
234
+ UserProvidedHeaderHttpSessionStrategyConfiguration .class )
235
+ .run ((context ) -> assertThat (
236
+ context .getBeansOfType (DefaultCookieSerializer .class )).isEmpty ());
237
+ }
238
+
239
+ @ Test
240
+ public void autoConfiguredCookieSerializerBacksOffWhenUserConfiguresCustomHttpSessionIdResolver () {
241
+ this .contextRunner
242
+ .withUserConfiguration (
243
+ UserProvidedCustomHttpSessionStrategyConfiguration .class )
244
+ .run ((context ) -> assertThat (
245
+ context .getBeansOfType (DefaultCookieSerializer .class )).isEmpty ());
187
246
}
188
247
189
248
@ Configuration
@@ -202,4 +261,52 @@ static class ServerPropertiesConfiguration {
202
261
203
262
}
204
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
+
300
+ @ Configuration
301
+ @ EnableSpringHttpSession
302
+ static class UserProvidedCustomHttpSessionStrategyConfiguration
303
+ extends SessionRepositoryConfiguration {
304
+
305
+ @ Bean
306
+ public HttpSessionIdResolver httpSessionStrategy () {
307
+ return mock (HttpSessionIdResolver .class );
308
+ }
309
+
310
+ }
311
+
205
312
}
0 commit comments