Skip to content

Commit 71dc4f3

Browse files
ttddyyrwinch
authored andcommitted
Allow configuring scheduler on ReactiveAuthenticationManagerAdapter
Currently, authentication logic will be performed on hardcoded elastic scheduler in ReactiveAuthenticationManagerAdapter. This commit makes the authentication logic scheduler configureable.
1 parent e6ace08 commit 71dc4f3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

core/src/main/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapter.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818
import org.springframework.security.core.Authentication;
1919
import org.springframework.util.Assert;
2020
import reactor.core.publisher.Mono;
21+
import reactor.core.scheduler.Scheduler;
2122
import reactor.core.scheduler.Schedulers;
2223

2324
/**
@@ -27,11 +28,14 @@
2728
* from coming in unless it was put on another thread.
2829
*
2930
* @author Rob Winch
31+
* @author Tadaya Tsuyukubo
3032
* @since 5.0
3133
*/
3234
public class ReactiveAuthenticationManagerAdapter implements ReactiveAuthenticationManager {
3335
private final AuthenticationManager authenticationManager;
3436

37+
private Scheduler scheduler = Schedulers.elastic();
38+
3539
public ReactiveAuthenticationManagerAdapter(AuthenticationManager authenticationManager) {
3640
Assert.notNull(authenticationManager, "authenticationManager cannot be null");
3741
this.authenticationManager = authenticationManager;
@@ -40,7 +44,7 @@ public ReactiveAuthenticationManagerAdapter(AuthenticationManager authentication
4044
@Override
4145
public Mono<Authentication> authenticate(Authentication token) {
4246
return Mono.just(token)
43-
.publishOn(Schedulers.elastic())
47+
.publishOn(this.scheduler)
4448
.flatMap( t -> {
4549
try {
4650
return Mono.just(authenticationManager.authenticate(t));
@@ -50,4 +54,15 @@ public Mono<Authentication> authenticate(Authentication token) {
5054
})
5155
.filter( a -> a.isAuthenticated());
5256
}
57+
58+
/**
59+
* Set a scheduler that will be published on to perform the authentication logic.
60+
* @param scheduler a scheduler to be published on
61+
* @throws IllegalArgumentException if the scheduler is {@code null}
62+
*/
63+
public void setScheduler(Scheduler scheduler) {
64+
Assert.notNull(scheduler, "scheduler cannot be null");
65+
this.scheduler = scheduler;
66+
}
67+
5368
}

core/src/test/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapterTests.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ public void constructorNullAuthenticationManager() {
5252
new ReactiveAuthenticationManagerAdapter(null);
5353
}
5454

55+
@Test(expected = IllegalArgumentException.class)
56+
public void setSchedulerNull() {
57+
this.manager.setScheduler(null);
58+
}
59+
5560
@Test
56-
public void authenticateWhenSuccessThenSucces() {
61+
public void authenticateWhenSuccessThenSuccess() {
5762
when(delegate.authenticate(any())).thenReturn(authentication);
5863
when(authentication.isAuthenticated()).thenReturn(true);
5964

0 commit comments

Comments
 (0)