SpringCloud开发-mysql启动包开发

前言

制作mysql启动包的目的是,项目采用HikariCP作为连接池,需要加密配置中的数据库帐号和密码。虽然Druid提供了原生支持,但是在最新版本配合mybatis-plus使用会导致出错,因此选HikariCP作为连接池。

实现

创建phenix-spring-boot-starter-mysql的子模块,pom配置为



    4.0.0

    com.phenix
    phenix-spring-boot-starter-mysql
    ${phenix-spring-boot-starter-mysql.version}
    jar

    phenix-spring-boot-starter-mysql
    mysql starter

    
        com.phenix
        phenix-spring-boot-starter-parent
        1.0.0-SNAPSHOT
    
	
    
        
        3.2.0
        3.2.0
        
        8.0.18
        2.2.0.RELEASE
        1.3.2
    


    
        
            mysql
            mysql-connector-java
            ${mysql.connector.version}
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
            ${jdbc-spring-boot-starter.version}
        
        
            log4j
            log4j
            1.2.17
        
    

 

创建自定义数据源

package com.phenix.starter.mysql.datasource;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 数据源配置解密密码
 */
@Slf4j
public class UmspscDataSource extends HikariDataSource {
    /**
     * 解密的用户名
     */
    private String usernameDecipher;

    /**
     * 解密的密码
     */
    private String passwordDecipher;
    /**
     * 密匙
     */
    private static final String KEY = "gvrlucrb7s3k076n";


    /**
     * 获取加密的用户名
     * @return
     */
    @Override
    public String getUsername() {
        if (StringUtils.isNotBlank(usernameDecipher)) {
            return usernameDecipher;
        }
        String encUsername = super.getUsername();
        if (null == encUsername) {
            return null;
        }
        try {
            //  密文解密,解密方法可以修改
            SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, KEY.getBytes());
            usernameDecipher = aes.decryptStr(encUsername, CharsetUtil.CHARSET_UTF_8);
            return usernameDecipher;
        } catch (Exception e) {
            log.error("数据库用户名解密出错,{" + encUsername + "}");
            return "";
        }
    }

    /**
     * 获取加密的密码
     * @return
     */
    @Override
    public String getPassword() {
        if (StringUtils.isNotBlank(passwordDecipher)) {
            return passwordDecipher;
        }
        String encPassword = super.getPassword();
        if (null == encPassword) {
            return null;
        }
        try {
            //  密文解密,解密方法可以修改
            SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, KEY.getBytes());
            passwordDecipher = aes.decryptStr(encPassword, CharsetUtil.CHARSET_UTF_8);
            return passwordDecipher;
        } catch (Exception e) {
            log.error("数据库密码解密出错,{" + encPassword + "}");
            return "";
        }
    }

}

 

然后配置数据源,注册成Bean

package com.phenix.starter.mysql.autoconfigure;

import com.phenix.starter.mysql.datasource.UmspscDataSource;
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.Configuration;
import org.springframework.context.annotation.Primary;

/**
 * 配置数据源
 * @author phenix
 * @date  2019-11-19
 */
@Configuration
public class PhenixDataSourceConfiguration {

    @Bean(name = "dataSource", autowire = Autowire.NO)
    @Primary
    @ConfigurationProperties(prefix="spring.datasource")
    public HikariDataSource dataSource() {
        HikariDataSource druidDataSource = new UmspscDataSource();
        return druidDataSource;
    }

}

至此,mysql的starter 包制作完成

 

源码地址: https://github.com/zhenghui317/phenix-spring-boot-starter-parent

你可能感兴趣的:(Starter包制作,SpringBoot,SpringCloud)