From 4f328c950365457c739dd61c053aa28ad5f7b2df Mon Sep 17 00:00:00 2001 From: Rob Winch <362503+rwinch@users.noreply.github.com> Date: Tue, 1 Oct 2024 08:50:41 -0500 Subject: [PATCH] destroy() shuts down the taskScheduler Issue gh-15735 --- .../authentication/ott/JdbcOneTimeTokenService.java | 10 +++++++++- .../ott/JdbcOneTimeTokenServiceTests.java | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenService.java b/core/src/main/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenService.java index fe8b32e48d..379733d83b 100644 --- a/core/src/main/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenService.java +++ b/core/src/main/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenService.java @@ -30,6 +30,7 @@ import java.util.function.Function; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.DisposableBean; import org.springframework.jdbc.core.ArgumentPreparedStatementSetter; import org.springframework.jdbc.core.JdbcOperations; import org.springframework.jdbc.core.PreparedStatementSetter; @@ -55,7 +56,7 @@ import org.springframework.util.StringUtils; * @author Max Batischev * @since 6.4 */ -public final class JdbcOneTimeTokenService implements OneTimeTokenService { +public final class JdbcOneTimeTokenService implements OneTimeTokenService, DisposableBean { private final Log logger = LogFactory.getLog(getClass()); @@ -187,6 +188,13 @@ public final class JdbcOneTimeTokenService implements OneTimeTokenService { this.logger.debug("Cleaned up " + deletedCount + " expired tokens"); } + @Override + public void destroy() throws Exception { + if (this.taskScheduler != null) { + this.taskScheduler.shutdown(); + } + } + /** * Sets the {@link Clock} used when generating one-time token and checking token * expiry. diff --git a/core/src/test/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenServiceTests.java b/core/src/test/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenServiceTests.java index 9bbbe32fb6..016c4b0f49 100644 --- a/core/src/test/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenServiceTests.java +++ b/core/src/test/java/org/springframework/security/authentication/ott/JdbcOneTimeTokenServiceTests.java @@ -68,8 +68,9 @@ public class JdbcOneTimeTokenServiceTests { } @AfterEach - public void tearDown() { + public void tearDown() throws Exception { this.db.shutdown(); + this.oneTimeTokenService.destroy(); } private static EmbeddedDatabase createDb() {