【Quartz】解密properties配置文件中的账号密码
在配置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(); } }