|
@@ -31,6 +31,7 @@ import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
import org.springframework.beans.factory.DisposableBean;
|
|
|
|
+import org.springframework.beans.factory.InitializingBean;
|
|
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
|
|
import org.springframework.jdbc.core.ArgumentPreparedStatementSetter;
|
|
import org.springframework.jdbc.core.JdbcOperations;
|
|
import org.springframework.jdbc.core.JdbcOperations;
|
|
import org.springframework.jdbc.core.PreparedStatementSetter;
|
|
import org.springframework.jdbc.core.PreparedStatementSetter;
|
|
@@ -40,7 +41,6 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
|
import org.springframework.scheduling.support.CronTrigger;
|
|
import org.springframework.scheduling.support.CronTrigger;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.Assert;
|
|
import org.springframework.util.CollectionUtils;
|
|
import org.springframework.util.CollectionUtils;
|
|
-import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
@@ -56,7 +56,7 @@ import org.springframework.util.StringUtils;
|
|
* @author Max Batischev
|
|
* @author Max Batischev
|
|
* @since 6.4
|
|
* @since 6.4
|
|
*/
|
|
*/
|
|
-public final class JdbcOneTimeTokenService implements OneTimeTokenService, DisposableBean {
|
|
|
|
|
|
+public final class JdbcOneTimeTokenService implements OneTimeTokenService, DisposableBean, InitializingBean {
|
|
|
|
|
|
private final Log logger = LogFactory.getLog(getClass());
|
|
private final Log logger = LogFactory.getLog(getClass());
|
|
|
|
|
|
@@ -70,7 +70,7 @@ public final class JdbcOneTimeTokenService implements OneTimeTokenService, Dispo
|
|
|
|
|
|
private ThreadPoolTaskScheduler taskScheduler;
|
|
private ThreadPoolTaskScheduler taskScheduler;
|
|
|
|
|
|
- private static final String DEFAULT_CLEANUP_CRON = "0 * * * * *";
|
|
|
|
|
|
+ private static final String DEFAULT_CLEANUP_CRON = "@hourly";
|
|
|
|
|
|
private static final String TABLE_NAME = "one_time_tokens";
|
|
private static final String TABLE_NAME = "one_time_tokens";
|
|
|
|
|
|
@@ -104,23 +104,27 @@ public final class JdbcOneTimeTokenService implements OneTimeTokenService, Dispo
|
|
/**
|
|
/**
|
|
* Constructs a {@code JdbcOneTimeTokenService} using the provide parameters.
|
|
* Constructs a {@code JdbcOneTimeTokenService} using the provide parameters.
|
|
* @param jdbcOperations the JDBC operations
|
|
* @param jdbcOperations the JDBC operations
|
|
- * @param cleanupCron cleanup cron expression
|
|
|
|
*/
|
|
*/
|
|
- public JdbcOneTimeTokenService(JdbcOperations jdbcOperations, String cleanupCron) {
|
|
|
|
- Assert.isTrue(StringUtils.hasText(cleanupCron), "cleanupCron cannot be null orr empty");
|
|
|
|
|
|
+ public JdbcOneTimeTokenService(JdbcOperations jdbcOperations) {
|
|
Assert.notNull(jdbcOperations, "jdbcOperations cannot be null");
|
|
Assert.notNull(jdbcOperations, "jdbcOperations cannot be null");
|
|
this.jdbcOperations = jdbcOperations;
|
|
this.jdbcOperations = jdbcOperations;
|
|
- this.taskScheduler = createTaskScheduler(cleanupCron);
|
|
|
|
|
|
+ this.taskScheduler = createTaskScheduler(DEFAULT_CLEANUP_CRON);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Constructs a {@code JdbcOneTimeTokenService} using the provide parameters.
|
|
|
|
- * @param jdbcOperations the JDBC operations
|
|
|
|
|
|
+ * Sets the chron expression used for cleaning up expired sessions. The default is to
|
|
|
|
+ * run hourly.
|
|
|
|
+ *
|
|
|
|
+ * For more advanced use cases the cleanupCron may be set to null which will disable
|
|
|
|
+ * the built-in cleanup. Users can then invoke {@link #cleanupExpiredTokens()} using
|
|
|
|
+ * custom logic.
|
|
|
|
+ * @param cleanupCron the chron expression passed to {@link CronTrigger} used for
|
|
|
|
+ * determining how frequent to perform cleanup. The default is "@hourly".
|
|
|
|
+ * @see CronTrigger
|
|
|
|
+ * @see #cleanupExpiredTokens()
|
|
*/
|
|
*/
|
|
- public JdbcOneTimeTokenService(JdbcOperations jdbcOperations) {
|
|
|
|
- Assert.notNull(jdbcOperations, "jdbcOperations cannot be null");
|
|
|
|
- this.jdbcOperations = jdbcOperations;
|
|
|
|
- this.taskScheduler = createTaskScheduler(DEFAULT_CLEANUP_CRON);
|
|
|
|
|
|
+ public void setCleanupCron(String cleanupCron) {
|
|
|
|
+ this.taskScheduler = createTaskScheduler(cleanupCron);
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -174,6 +178,9 @@ public final class JdbcOneTimeTokenService implements OneTimeTokenService, Dispo
|
|
}
|
|
}
|
|
|
|
|
|
private ThreadPoolTaskScheduler createTaskScheduler(String cleanupCron) {
|
|
private ThreadPoolTaskScheduler createTaskScheduler(String cleanupCron) {
|
|
|
|
+ if (cleanupCron == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
|
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
|
|
taskScheduler.setThreadNamePrefix("spring-one-time-tokens-");
|
|
taskScheduler.setThreadNamePrefix("spring-one-time-tokens-");
|
|
taskScheduler.initialize();
|
|
taskScheduler.initialize();
|
|
@@ -188,6 +195,11 @@ public final class JdbcOneTimeTokenService implements OneTimeTokenService, Dispo
|
|
this.logger.debug("Cleaned up " + deletedCount + " expired tokens");
|
|
this.logger.debug("Cleaned up " + deletedCount + " expired tokens");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @Override
|
|
|
|
+ public void afterPropertiesSet() throws Exception {
|
|
|
|
+ this.taskScheduler.afterPropertiesSet();
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public void destroy() throws Exception {
|
|
public void destroy() throws Exception {
|
|
if (this.taskScheduler != null) {
|
|
if (this.taskScheduler != null) {
|