From 3bceadd3698ea7209ed230042a5a5910f65c11e8 Mon Sep 17 00:00:00 2001 From: Rob Winch Date: Fri, 27 Oct 2017 17:32:02 -0500 Subject: [PATCH] Only populate a Context once Fixes gh-4718 --- .../ReactorContextTestExecutionListener.java | 6 ++++++ ...actorContextTestExecutionListenerTests.java | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java b/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java index 76985026b5..1911dbfdf8 100644 --- a/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java +++ b/test/src/main/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListener.java @@ -63,6 +63,8 @@ public class ReactorContextTestExecutionListener } private static class SecuritySubContext implements CoreSubscriber { + private static String CONTEXT_DEFAULTED_ATTR_NAME = SecuritySubContext.class.getName().concat(".CONTEXT_DEFAULTED_ATTR_NAME"); + private final CoreSubscriber delegate; SecuritySubContext(CoreSubscriber delegate) { @@ -72,6 +74,10 @@ public class ReactorContextTestExecutionListener @Override public Context currentContext() { Context context = delegate.currentContext(); + if(context.hasKey(CONTEXT_DEFAULTED_ATTR_NAME)) { + return context; + } + context = context.put(CONTEXT_DEFAULTED_ATTR_NAME, Boolean.TRUE); Authentication authentication = TestSecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { return context; diff --git a/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java b/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java index a2f9089f33..f66d642b4f 100644 --- a/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java +++ b/test/src/test/java/org/springframework/security/test/context/support/ReactorContextTestExecutionListenerTests.java @@ -111,6 +111,24 @@ public class ReactorContextTestExecutionListenerTests { .verifyComplete(); } + @Test + public void beforeTestMethodWhenClearThenReactorContextDoesNotOverride() throws Exception { + TestingAuthenticationToken expectedAuthentication = new TestingAuthenticationToken("user", "password", "ROLE_USER"); + TestingAuthenticationToken contextHolder = new TestingAuthenticationToken("contextHolder", "password", "ROLE_USER"); + TestSecurityContextHolder.setContext(new SecurityContextImpl(contextHolder)); + + this.listener.beforeTestMethod(this.testContext); + + Mono authentication = Mono.just("any") + .flatMap(s -> ReactiveSecurityContextHolder.getContext() + .map(SecurityContext::getAuthentication) + ) + .subscriberContext(ReactiveSecurityContextHolder.clearContext()); + + StepVerifier.create(authentication) + .verifyComplete(); + } + @Test public void afterTestMethodWhenSecurityContextEmptyThenNoError() throws Exception { this.listener.beforeTestMethod(this.testContext);