【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
quartz_config.properties

  注意: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();
    }
}

 

转载于:https://www.cnblogs.com/xiongxx/p/9018314.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值