Skip to content

Commit 5ad31df

Browse files
committed
Support configuring max sessions for reactive server
1 parent 6dff3c5 commit 5ad31df

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ public static class Session {
329329
@DurationUnit(ChronoUnit.SECONDS)
330330
private Duration timeout = Duration.ofMinutes(30);
331331

332+
/**
333+
* The maximum number of sessions that can be stored.
334+
*/
335+
private Integer maxSessions;
336+
332337
@NestedConfigurationProperty
333338
private final Cookie cookie = new Cookie();
334339

@@ -340,6 +345,14 @@ public void setTimeout(Duration timeout) {
340345
this.timeout = timeout;
341346
}
342347

348+
public Integer getMaxSessions() {
349+
return this.maxSessions;
350+
}
351+
352+
public void setMaxSessions(Integer maxSessions) {
353+
this.maxSessions = maxSessions;
354+
}
355+
343356
public Cookie getCookie() {
344357
return this.cookie;
345358
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfiguration.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,12 @@ public LocaleContextResolver localeContextResolver() {
332332
public WebSessionManager webSessionManager(ObjectProvider<WebSessionIdResolver> webSessionIdResolver) {
333333
DefaultWebSessionManager webSessionManager = new DefaultWebSessionManager();
334334
Duration timeout = this.serverProperties.getReactive().getSession().getTimeout();
335-
webSessionManager.setSessionStore(new MaxIdleTimeInMemoryWebSessionStore(timeout));
335+
var maxSessions = this.serverProperties.getReactive().getSession().getMaxSessions();
336+
var sessionStore = new MaxIdleTimeInMemoryWebSessionStore(timeout);
337+
if (maxSessions != null) {
338+
sessionStore.setMaxSessions(maxSessions);
339+
}
340+
webSessionManager.setSessionStore(sessionStore);
336341
webSessionIdResolver.ifAvailable(webSessionManager::setSessionIdResolver);
337342
return webSessionManager;
338343
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/reactive/WebFluxAutoConfigurationTests.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
import org.springframework.web.server.i18n.FixedLocaleContextResolver;
110110
import org.springframework.web.server.i18n.LocaleContextResolver;
111111
import org.springframework.web.server.session.CookieWebSessionIdResolver;
112+
import org.springframework.web.server.session.DefaultWebSessionManager;
113+
import org.springframework.web.server.session.InMemoryWebSessionStore;
112114
import org.springframework.web.server.session.WebSessionIdResolver;
113115
import org.springframework.web.server.session.WebSessionManager;
114116
import org.springframework.web.util.pattern.PathPattern;
@@ -620,6 +622,17 @@ void customSessionTimeoutConfigurationShouldBeApplied() {
620622
})));
621623
}
622624

625+
@Test
626+
void customSessionMaxSessionsConfigurationShouldBeApplied() {
627+
this.contextRunner.withPropertyValues("server.reactive.session.max-sessions:123").run(context -> {
628+
var sessionManager = context.getBean(WebSessionManager.class);
629+
assertThat(sessionManager).isInstanceOf(DefaultWebSessionManager.class);
630+
var sessionStore = ((DefaultWebSessionManager) sessionManager).getSessionStore();
631+
assertThat(sessionStore).isInstanceOf(InMemoryWebSessionStore.class);
632+
assertThat(((InMemoryWebSessionStore) sessionStore).getMaxSessions()).isEqualTo(123);
633+
});
634+
}
635+
623636
@Test
624637
void customSessionCookieConfigurationShouldBeApplied() {
625638
this.contextRunner.withPropertyValues("server.reactive.session.cookie.name:JSESSIONID",

0 commit comments

Comments
 (0)