Skip to content

Make scheduler configurable on ReactiveAuthenticationManagerAdapter #6852

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@
import org.springframework.security.core.Authentication;
import org.springframework.util.Assert;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Scheduler;
import reactor.core.scheduler.Schedulers;

/**
Expand All @@ -27,11 +28,14 @@
* from coming in unless it was put on another thread.
*
* @author Rob Winch
* @author Tadaya Tsuyukubo
* @since 5.0
*/
public class ReactiveAuthenticationManagerAdapter implements ReactiveAuthenticationManager {
private final AuthenticationManager authenticationManager;

private Scheduler scheduler = Schedulers.elastic();

public ReactiveAuthenticationManagerAdapter(AuthenticationManager authenticationManager) {
Assert.notNull(authenticationManager, "authenticationManager cannot be null");
this.authenticationManager = authenticationManager;
Expand All @@ -40,7 +44,7 @@ public ReactiveAuthenticationManagerAdapter(AuthenticationManager authentication
@Override
public Mono<Authentication> authenticate(Authentication token) {
return Mono.just(token)
.publishOn(Schedulers.elastic())
.publishOn(this.scheduler)
.flatMap( t -> {
try {
return Mono.just(authenticationManager.authenticate(t));
Expand All @@ -50,4 +54,15 @@ public Mono<Authentication> authenticate(Authentication token) {
})
.filter( a -> a.isAuthenticated());
}

/**
* Set a scheduler that will be published on to perform the authentication logic.
* @param scheduler a scheduler to be published on
* @throws IllegalArgumentException if the scheduler is {@code null}
*/
public void setScheduler(Scheduler scheduler) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a null check and a test to validate the null check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the PR with null check and test

Assert.notNull(scheduler, "scheduler cannot be null");
this.scheduler = scheduler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,13 @@ public void constructorNullAuthenticationManager() {
new ReactiveAuthenticationManagerAdapter(null);
}

@Test(expected = IllegalArgumentException.class)
public void setSchedulerNull() {
this.manager.setScheduler(null);
}

@Test
public void authenticateWhenSuccessThenSucces() {
public void authenticateWhenSuccessThenSuccess() {
when(delegate.authenticate(any())).thenReturn(authentication);
when(authentication.isAuthenticated()).thenReturn(true);

Expand Down