17
17
package org .springframework .boot .autoconfigure .web .reactive ;
18
18
19
19
import java .time .Duration ;
20
- import java .util .function .Supplier ;
21
20
22
21
import org .apache .commons .logging .Log ;
23
22
import org .apache .commons .logging .LogFactory ;
37
36
import org .springframework .boot .autoconfigure .validation .ValidationAutoConfiguration ;
38
37
import org .springframework .boot .autoconfigure .validation .ValidatorAdapter ;
39
38
import org .springframework .boot .autoconfigure .web .ConditionalOnEnabledResourceChain ;
39
+ import org .springframework .boot .autoconfigure .web .ServerProperties ;
40
40
import org .springframework .boot .autoconfigure .web .WebProperties ;
41
41
import org .springframework .boot .autoconfigure .web .WebProperties .Resources ;
42
42
import org .springframework .boot .autoconfigure .web .format .DateTimeFormatters ;
43
43
import org .springframework .boot .autoconfigure .web .format .WebConversionService ;
44
- import org .springframework .boot .autoconfigure .web .reactive .WebFluxProperties .Cookie ;
45
44
import org .springframework .boot .autoconfigure .web .reactive .WebFluxProperties .Format ;
46
- import org .springframework .boot .autoconfigure .web .reactive .WebFluxProperties .SameSite ;
47
45
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
48
- import org .springframework .boot .context .properties .PropertyMapper ;
49
46
import org .springframework .boot .convert .ApplicationConversionService ;
50
47
import org .springframework .boot .web .codec .CodecCustomizer ;
51
48
import org .springframework .boot .web .reactive .filter .OrderedHiddenHttpMethodFilter ;
57
54
import org .springframework .core .annotation .Order ;
58
55
import org .springframework .format .FormatterRegistry ;
59
56
import org .springframework .format .support .FormattingConversionService ;
60
- import org .springframework .http .ResponseCookie .ResponseCookieBuilder ;
61
57
import org .springframework .http .codec .ServerCodecConfigurer ;
62
58
import org .springframework .util .ClassUtils ;
63
- import org .springframework .util .StringUtils ;
64
59
import org .springframework .validation .Validator ;
65
60
import org .springframework .web .filter .reactive .HiddenHttpMethodFilter ;
66
61
import org .springframework .web .reactive .config .DelegatingWebFluxConfiguration ;
83
78
import org .springframework .web .server .i18n .AcceptHeaderLocaleContextResolver ;
84
79
import org .springframework .web .server .i18n .FixedLocaleContextResolver ;
85
80
import org .springframework .web .server .i18n .LocaleContextResolver ;
86
- import org .springframework .web .server .session .CookieWebSessionIdResolver ;
87
81
import org .springframework .web .server .session .DefaultWebSessionManager ;
88
82
import org .springframework .web .server .session .InMemoryWebSessionStore ;
89
83
import org .springframework .web .server .session .WebSessionIdResolver ;
108
102
@ ConditionalOnClass (WebFluxConfigurer .class )
109
103
@ ConditionalOnMissingBean ({ WebFluxConfigurationSupport .class })
110
104
@ AutoConfigureAfter ({ ReactiveWebServerFactoryAutoConfiguration .class , CodecsAutoConfiguration .class ,
111
- ReactiveMultipartAutoConfiguration .class , ValidationAutoConfiguration .class })
105
+ ReactiveMultipartAutoConfiguration .class , ValidationAutoConfiguration .class ,
106
+ WebSessionIdResolverAutoConfiguration .class })
112
107
@ AutoConfigureOrder (Ordered .HIGHEST_PRECEDENCE + 10 )
113
108
public class WebFluxAutoConfiguration {
114
109
@@ -240,19 +235,22 @@ private void customizeResourceHandlerRegistration(ResourceHandlerRegistration re
240
235
* Configuration equivalent to {@code @EnableWebFlux}.
241
236
*/
242
237
@ Configuration (proxyBeanMethods = false )
243
- @ EnableConfigurationProperties (WebProperties .class )
238
+ @ EnableConfigurationProperties ({ WebProperties .class , ServerProperties . class } )
244
239
public static class EnableWebFluxConfiguration extends DelegatingWebFluxConfiguration {
245
240
246
241
private final WebFluxProperties webFluxProperties ;
247
242
248
243
private final WebProperties webProperties ;
249
244
245
+ private final ServerProperties serverProperties ;
246
+
250
247
private final WebFluxRegistrations webFluxRegistrations ;
251
248
252
249
public EnableWebFluxConfiguration (WebFluxProperties webFluxProperties , WebProperties webProperties ,
253
- ObjectProvider <WebFluxRegistrations > webFluxRegistrations ) {
250
+ ServerProperties serverProperties , ObjectProvider <WebFluxRegistrations > webFluxRegistrations ) {
254
251
this .webFluxProperties = webFluxProperties ;
255
252
this .webProperties = webProperties ;
253
+ this .serverProperties = serverProperties ;
256
254
this .webFluxRegistrations = webFluxRegistrations .getIfUnique ();
257
255
}
258
256
@@ -313,54 +311,12 @@ public LocaleContextResolver localeContextResolver() {
313
311
@ ConditionalOnMissingBean (name = WebHttpHandlerBuilder .WEB_SESSION_MANAGER_BEAN_NAME )
314
312
public WebSessionManager webSessionManager (ObjectProvider <WebSessionIdResolver > webSessionIdResolver ) {
315
313
DefaultWebSessionManager webSessionManager = new DefaultWebSessionManager ();
316
- Duration timeout = this .webFluxProperties .getSession ().getTimeout ();
314
+ Duration timeout = this .serverProperties . getReactive () .getSession ().getTimeout ();
317
315
webSessionManager .setSessionStore (new MaxIdleTimeInMemoryWebSessionStore (timeout ));
318
- webSessionManager . setSessionIdResolver ( webSessionIdResolver .getIfAvailable ( cookieWebSessionIdResolver ()) );
316
+ webSessionIdResolver .ifAvailable ( webSessionManager :: setSessionIdResolver );
319
317
return webSessionManager ;
320
318
}
321
319
322
- private Supplier <WebSessionIdResolver > cookieWebSessionIdResolver () {
323
- return () -> {
324
- CookieWebSessionIdResolver resolver = new CookieWebSessionIdResolver ();
325
- String cookieName = this .webFluxProperties .getSession ().getCookie ().getName ();
326
- if (StringUtils .hasText (cookieName )) {
327
- resolver .setCookieName (cookieName );
328
- }
329
- resolver .addCookieInitializer (this ::initializeCookie );
330
- return resolver ;
331
- };
332
- }
333
-
334
- private void initializeCookie (ResponseCookieBuilder builder ) {
335
- Cookie cookie = this .webFluxProperties .getSession ().getCookie ();
336
- PropertyMapper map = PropertyMapper .get ().alwaysApplyingWhenNonNull ();
337
- map .from (cookie ::getDomain ).to (builder ::domain );
338
- map .from (cookie ::getPath ).to (builder ::path );
339
- map .from (cookie ::getHttpOnly ).to (builder ::httpOnly );
340
- map .from (cookie ::getSecure ).to (builder ::secure );
341
- map .from (cookie ::getMaxAge ).to (builder ::maxAge );
342
- map .from (cookie ::getSameSite ).as (SameSite ::attribute ).to (builder ::sameSite );
343
- }
344
-
345
- static final class MaxIdleTimeInMemoryWebSessionStore extends InMemoryWebSessionStore {
346
-
347
- private final Duration timeout ;
348
-
349
- private MaxIdleTimeInMemoryWebSessionStore (Duration timeout ) {
350
- this .timeout = timeout ;
351
- }
352
-
353
- @ Override
354
- public Mono <WebSession > createWebSession () {
355
- return super .createWebSession ().doOnSuccess (this ::setMaxIdleTime );
356
- }
357
-
358
- private void setMaxIdleTime (WebSession session ) {
359
- session .setMaxIdleTime (this .timeout );
360
- }
361
-
362
- }
363
-
364
320
}
365
321
366
322
@ Configuration (proxyBeanMethods = false )
@@ -375,4 +331,23 @@ ResourceChainResourceHandlerRegistrationCustomizer resourceHandlerRegistrationCu
375
331
376
332
}
377
333
334
+ static final class MaxIdleTimeInMemoryWebSessionStore extends InMemoryWebSessionStore {
335
+
336
+ private final Duration timeout ;
337
+
338
+ private MaxIdleTimeInMemoryWebSessionStore (Duration timeout ) {
339
+ this .timeout = timeout ;
340
+ }
341
+
342
+ @ Override
343
+ public Mono <WebSession > createWebSession () {
344
+ return super .createWebSession ().doOnSuccess (this ::setMaxIdleTime );
345
+ }
346
+
347
+ private void setMaxIdleTime (WebSession session ) {
348
+ session .setMaxIdleTime (this .timeout );
349
+ }
350
+
351
+ }
352
+
378
353
}
0 commit comments