diff --git a/core/src/main/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapter.java b/core/src/main/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapter.java index 46fb250738d..21a45adac48 100644 --- a/core/src/main/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapter.java +++ b/core/src/main/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapter.java @@ -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. @@ -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; /** @@ -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; @@ -40,7 +44,7 @@ public ReactiveAuthenticationManagerAdapter(AuthenticationManager authentication @Override public Mono authenticate(Authentication token) { return Mono.just(token) - .publishOn(Schedulers.elastic()) + .publishOn(this.scheduler) .flatMap( t -> { try { return Mono.just(authenticationManager.authenticate(t)); @@ -50,4 +54,15 @@ public Mono 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) { + Assert.notNull(scheduler, "scheduler cannot be null"); + this.scheduler = scheduler; + } + } diff --git a/core/src/test/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapterTests.java b/core/src/test/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapterTests.java index 6d9a7272d1e..259e91746f8 100644 --- a/core/src/test/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapterTests.java +++ b/core/src/test/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapterTests.java @@ -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);