springboot+hikariPOOL 在yml中配置加密的mysql密码,代码中进行解码

需求是这样,出于安全考虑需要yml中数据库密码以加密字符串的形式存在,先行找到使用Jasypt进行加密的方式,说不行,最好不要引进新的依赖。

通过搜索发现可以通过重写数据源的getPassword()方法进行自行解密,实现方法如下:

@Slf4j
public class UmspscDataSource extends HikariDataSource {

    private String passwordDis;
    /**
     * 密匙
     */
    private final static String PKEY ="1234565437892132";
    @Override
    public String getPassword(){

        if(StringUtils.isNotBlank(passwordDis)){return passwordDis;}
        String encPassword = super.getPassword();
        if(null==encPassword){
            return null;
        }
        log.info("数据库密码加解密,{"+encPassword+"}");
        try{
            //替换自己的解密规则
            passwordDis =encPassword.replace(PKEY,""); 
            return passwordDis;
        }catch (Exception e){
            log.error("数据库密码解密出错,{"+encPassword+"}");
            log.info(e.getMessage());
            return null;
        }
    }

}
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

@Component
public class CommonBeanFactory {
    @Bean(name = "dataSource", autowire = Autowire.NO)
    @Primary
    //从yaml文件中读取spring.datasource节点下的配置项
    @ConfigurationProperties(ignoreUnknownFields = false, prefix = "spring.datasource")
    public HikariDataSource dataSource() {
        //以自己的获取数据源对象实现替换HikariDataSource
        HikariDataSource druidDataSource = new UmspscDataSource();

        return druidDataSource;
    }
}

yaml文件配置如下:

spring:
  datasource:
    jdbcUrl: jdbc:mysql://111.111.111.111:3306/hedu?useAffectedRows=true&useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: Root2017#1234565437892132
    driver-class-name: com.mysql.cj.jdbc.Driver
    connection-test-query: SELECT 1
    maximum-pool-size: 150
    minimum-idle: 2

注意:

1.以前配置文件中用的spring.datasource.url需要更换为jdbcUrl

2.以前的spring.datasource.hikari.connection-test-query等配置需要挪到datasource节点之下,否则读不到会报错。

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的Spring Boot RabbitMQ应用程序的完整application.yml配置代码示例: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: / listener: simple: acknowledge-mode: manual concurrency: 1 max-concurrency: 1 template: exchange: example.exchange routing-key: example.routingKey ``` 解释: - `spring.rabbitmq.host`:RabbitMQ服务器的主机名或IP地址。 - `spring.rabbitmq.port`:RabbitMQ服务器的端口号。 - `spring.rabbitmq.username`:连接RabbitMQ服务器的用户名。 - `spring.rabbitmq.password`:连接RabbitMQ服务器的密码。 - `spring.rabbitmq.virtual-host`:连接RabbitMQ服务器的虚拟主机。 - `spring.rabbitmq.listener.simple.acknowledge-mode`:确认模式,这里是手动确认模式。 - `spring.rabbitmq.listener.simple.concurrency`:消费者线程数。 - `spring.rabbitmq.listener.simple.max-concurrency`:最大消费者线程数。 - `spring.rabbitmq.template.exchange`:消息发送时使用的交换机名。 - `spring.rabbitmq.template.routing-key`:消息发送时使用的路由键。 你也可以通过application.yml文件配置RabbitMQ的其他参数,例如: ```yaml spring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: / listener: simple: acknowledge-mode: manual concurrency: 1 max-concurrency: 1 template: exchange: example.exchange routing-key: example.routingKey cache: channel: size: 100 checkout-timeout: 5000 connection: mode: connection size: 10 validated: true ``` 解释: - `spring.rabbitmq.cache.channel.size`:通道缓存的大小。 - `spring.rabbitmq.cache.channel.checkout-timeout`:通道缓存的通道检出超时时间(毫秒)。 - `spring.rabbitmq.cache.connection.mode`:缓存模式,这里是连接模式。 - `spring.rabbitmq.cache.connection.size`:连接缓存的大小。 - `spring.rabbitmq.cache.connection.validated`:连接是否需要验证。 请注意,以上参数仅为示例,具体配置可能因应用程序需求而异。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值