From be397b8b33ebe50991df0a5f1bfe146290a9c703 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Thu, 16 Nov 2017 13:23:38 -0600 Subject: [PATCH] WebSessionServerSecurityContextRepository Polish - map(WebSession::getAttributes) - use Mono.justOrEmpty Issue: gh-4843 --- .../WebSessionServerSecurityContextRepository.java | 11 +++++++---- ...ebSessionServerSecurityContextRepositoryTests.java | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/web/src/main/java/org/springframework/security/web/server/context/WebSessionServerSecurityContextRepository.java b/web/src/main/java/org/springframework/security/web/server/context/WebSessionServerSecurityContextRepository.java index 551be8c2a0..e40fb394da 100644 --- a/web/src/main/java/org/springframework/security/web/server/context/WebSessionServerSecurityContextRepository.java +++ b/web/src/main/java/org/springframework/security/web/server/context/WebSessionServerSecurityContextRepository.java @@ -19,6 +19,7 @@ import org.springframework.security.core.context.SecurityContext; import org.springframework.util.Assert; import org.springframework.web.server.ServerWebExchange; +import org.springframework.web.server.WebSession; import reactor.core.publisher.Mono; /** @@ -61,9 +62,11 @@ public class WebSessionServerSecurityContextRepository } public Mono load(ServerWebExchange exchange) { - return exchange.getSession().flatMap( session -> { - SecurityContext context = (SecurityContext) session.getAttributes().get(this.springSecurityContextAttrName); - return context == null ? Mono.empty() : Mono.just(context); - }); + return exchange.getSession() + .map(WebSession::getAttributes) + .flatMap( attrs -> { + SecurityContext context = (SecurityContext) attrs.get(this.springSecurityContextAttrName); + return Mono.justOrEmpty(context); + }); } } diff --git a/web/src/test/java/org/springframework/security/web/server/context/WebSessionServerSecurityContextRepositoryTests.java b/web/src/test/java/org/springframework/security/web/server/context/WebSessionServerSecurityContextRepositoryTests.java index 9101ab9995..2ff1a7e104 100644 --- a/web/src/test/java/org/springframework/security/web/server/context/WebSessionServerSecurityContextRepositoryTests.java +++ b/web/src/test/java/org/springframework/security/web/server/context/WebSessionServerSecurityContextRepositoryTests.java @@ -80,4 +80,10 @@ public class WebSessionServerSecurityContextRepositoryTests { WebSession session = this.exchange.getSession().block(); assertThat(session.getId()).isNotEqualTo(originalSessionId); } + + @Test + public void loadWhenNullThenNull() { + SecurityContext context = this.repository.load(this.exchange).block(); + assertThat(context).isNull(); + } }