Explorar el Código

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.
Tadaya Tsuyukubo hace 6 años
padre
commit
71dc4f39be

+ 17 - 2
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 @@ package org.springframework.security.authentication;
 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 @@ import reactor.core.scheduler.Schedulers;
  * 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 class ReactiveAuthenticationManagerAdapter implements ReactiveAuthenticat
 	@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));
@@ -50,4 +54,15 @@ public class ReactiveAuthenticationManagerAdapter implements ReactiveAuthenticat
 			})
 			.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;
+	}
+
 }

+ 6 - 1
core/src/test/java/org/springframework/security/authentication/ReactiveAuthenticationManagerAdapterTests.java

@@ -52,8 +52,13 @@ public class ReactiveAuthenticationManagerAdapterTests {
 		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);