在配置quartz時,為了保密某些信息(特別是賬號密碼),通常會使用密文。那么在實際使用這些配置信息時,需要進行解密。本文提供一種解密方法如下:
(1)假設在properties文件中加密了賬號密碼

1 #============================================================================ 2 # 基礎配置 3 #============================================================================ 4 org.quartz.scheduler.instanceName = JobScheduler 5 org.quartz.scheduler.instanceId = AUTO 6 org.quartz.scheduler.rmi.export = false 7 org.quartz.scheduler.rmi.proxy = false 8 org.quartz.scheduler.wrapJobExecutionInUserTransaction = false 9 10 #============================================================================ 11 # 調度器線程池配置 12 #============================================================================ 13 org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool 14 org.quartz.threadPool.threadCount = 20 15 org.quartz.threadPool.threadPriority = 5 16 org.quartz.jobStore.misfireThreshold = 60000 17 18 #============================================================================ 19 # Configure JobStore 作業存儲配置 20 #============================================================================ 21 org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX 22 org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.StdJDBCDelegate 23 org.quartz.jobStore.useProperties = true 24 org.quartz.jobStore.tablePrefix = QRTZ_ 25 org.quartz.jobStore.dataSource = qzDS 26 27 org.quartz.jobStore.isClustered = true 28 org.quartz.jobStore.clusterCheckinInterval = 15000 29 30 #============================================================================ 31 # JDBC 32 #============================================================================ 33 org.quartz.dataSource.qzDS.driver = com.mysql.jdbc.Driver 34 org.quartz.dataSource.qzDS.URL = jdbc:mysql://localhost:3306/job_scheduler 35 org.quartz.dataSource.qzDS.user = ***************************** 36 org.quartz.dataSource.qzDS.password = ***************************** 37 org.quartz.dataSource.qzDS.maxConnections = 5 38 org.quartz.dataSource.qzDS.validationQuery = select 0 from dual
注意:properties文件名不能是quartz.properties,否則Quartz可能還是會使用解密前的配置信息。
(2)寫SchedulerConfig.java文件解密賬號密碼后使用
import org.quartz.Scheduler; import org.quartz.ee.servlet.QuartzInitializerListener; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.wcc.crypt.Crypter; import org.wcc.crypt.CrypterFactory; import java.io.IOException; import java.util.Properties; @Configuration //類似xml中的<beans>標簽,一般和@bean注解一起使用來配置一個Bean,讓Spring來管理它的生命周期 public class SchedulerConfig { @Bean(name="SchedulerFactory") public SchedulerFactoryBean schedulerFactoryBean() throws IOException { SchedulerFactoryBean factory = new SchedulerFactoryBean(); factory.setQuartzProperties(quartzProperties()); return factory; } /** * 加載Quartz配置 * */ @Bean public Properties quartzProperties() throws IOException { //使用Spring的PropertiesFactoryBean對屬性配置文件進行管理 PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean(); propertiesFactoryBean.setLocation(new ClassPathResource("/quartz_config.properties")); propertiesFactoryBean.afterPropertiesSet(); Properties properties = propertiesFactoryBean.getObject(); // 賬號密碼解密 Crypter crypter = CrypterFactory.getCrypter(CrypterFactory.AES_CBC); String user = properties.getProperty("org.quartz.dataSource.qzDS.user"); if (user != null) { user = crypter.decrypt(user); properties.setProperty("org.quartz.dataSource.qzDS.user", user); } String password = properties.getProperty("org.quartz.dataSource.qzDS.password"); if (password != null) { password = crypter.decrypt(password); properties.setProperty("org.quartz.dataSource.qzDS.password", password); } return properties; } /** * 初始化Quartz監聽器,讓Spring boot啟動時初始化Quartz * */ @Bean public QuartzInitializerListener executorListener() { return new QuartzInitializerListener(); } /** * 通過SchedulerFactoryBean獲取Scheduler的實例 */ @Bean(name="Scheduler") public Scheduler scheduler() throws IOException { return schedulerFactoryBean().getScheduler(); } }