Skip to content

ReactiveSecurityContextHolder.getContext() is broken when used with Mono.toFuture(). #5690

Open
@dave-fl

Description

@dave-fl

Summary

ReactiveSecurityContextHolder is broken when used with Futures. It does not always provide results and sometimes just fires the onComplete signal.

Actual Behavior

Executes onComplete()

Expected Behavior

Should execute onNext()

Version

5.0.7 Release

Sample

@Test
public void testWorkingContext() {
	Authentication authentication = new PreAuthenticatedAuthenticationToken("TEST", "");
	Mono<String> working = ReactiveSecurityContextHolder.getContext()
			.map(securityContext -> (String)securityContext.getAuthentication().getPrincipal());

	Mono<String> stringMono = working.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication));
	StepVerifier.create(stringMono).expectNext("TEST").verifyComplete();
}


@Test
public void testBrokenContext() {
	Authentication authentication = new PreAuthenticatedAuthenticationToken("TEST", "");
	Mono<String> working = ReactiveSecurityContextHolder.getContext()
			.map(securityContext -> (String)securityContext.getAuthentication().getPrincipal());
	Mono<String> broken = Mono.fromFuture(working.toFuture());
	Mono<String> stringMono = broken.subscriberContext(ReactiveSecurityContextHolder.withAuthentication(authentication));
	StepVerifier.create(stringMono).expectNext("TEST").verifyComplete();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreAn issue in spring-security-corestatus: blockedAn issue that's blocked on an external project changetype: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions