Spring Boot集成 Druid数据库密码加密

1、使用AES对称加密数据库密码

public class AesUtils {
	
	private final static String  KEY="112b3b4d5e6f7m8n";
	private static final String KEY_ALGORITHM = "AES";
	/**"算法/模式/补码方式"*/
	private static final String CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
	private static final String DEFAULT_CHARSET = "UTF-8";

	public static String encrypt(String content) {
		try {
			byte[] byteContent = content.getBytes(DEFAULT_CHARSET);
			SecretKeySpec sKeySpec = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE, sKeySpec);
			byte[] encrypted = cipher.doFinal(byteContent);
			return  new String(Base64Utils.encode(encrypted));
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static String decrypt(String content) {
		try {
			byte[] byteContent = Base64Utils.decodeFromString(content);
			SecretKeySpec key = new SecretKeySpec(KEY.getBytes(), KEY_ALGORITHM);
			Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] rByte = cipher.doFinal(byteContent);
			return new String(rByte, DEFAULT_CHARSET);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
}

   AesUtils.encrypt("root")运算得出数据库加密后密码  Wb3QAAhCDQvcNSzVz5z3lw=

2、数据源配置

   yml文件:

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    one:
      url: jdbc:mysql://${serverIp}:3306/cateen_db?useUnicode=true&characterEncoding=utf-8
      username: root
      password: Wb3QAAhCDQvcNSzVz5z3lw==
    two:
      url: jdbc:mysql://${serverIp}:3306/fee_db?useUnicode=true&characterEncoding=utf-8
      username: root
      password: Wb3QAAhCDQvcNSzVz5z3lw==

数据源注入spring

@Configuration
public class MyDataSourceConfig {

    @Bean("dataSourceOne")
    @ConfigurationProperties(prefix = "spring.datasource.one")
    public DruidDataSource dataSourceOne(){
        return new DruidDataSource();
    }

    @Bean("dataSourceTwo")
    @ConfigurationProperties(prefix = "spring.datasource.two")
    public DruidDataSource dataSourceTwo(){
        return new DruidDataSource();
    }

}

3、数据库密码解密

@Component
public class CustomBeanPostProcessor extends ConfigurationClassPostProcessor implements  BeanPostProcessor  {

    private final static Logger LOGGER = LoggerFactory.getLogger(CustomBeanPostProcessor.class);

    private ConfigurableListableBeanFactory  beanFactory;

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {
        super.postProcessBeanFactory(beanFactory);
        this.beanFactory=beanFactory;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        String dataSourceOne ="dataSourceOne";
        String dataSourceTwo ="dataSourceTwo";
        if (StringUtils.equals(dataSourceOne,beanName)){
            DruidDataSource druidDataSourceOne = (DruidDataSource) beanFactory.getBean(dataSourceOne);
            String password = druidDataSourceOne.getPassword();
            LOGGER.info("druidDataSourceOne--->pwd="+ password);
            druidDataSourceOne.setPassword(AesUtils.decrypt(password));
        }
        if (StringUtils.equals(dataSourceTwo,beanName)){
            DruidDataSource druidDataSourceTwo = (DruidDataSource) beanFactory.getBean(dataSourceTwo);
            String password = druidDataSourceTwo.getPassword();
            LOGGER.info("druidDataSourceTwo--->pwd="+ password);
            druidDataSourceTwo.setPassword(AesUtils.decrypt(password));
        }
        return bean;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一种用于快速开发基于Spring框架的应用程序的框架,而Druid是一个开源的数据库连接池和监控平台。Druid Admin是一种基于Web的管理平台,可以用于监控和管理Druid数据库连接池。 要在Spring Boot中使用Druid Admin,需要遵循以下步骤: 1. 在pom.xml文件中添加DruidDruid Admin的依赖。 ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-admin-starter</artifactId> <version>1.1.10</version> </dependency> ``` 2. 在application.properties或application.yml文件中添加Druid的配置信息。 ```properties spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver # Druid监控配置 # 访问Druid监控平台的URL为:http://localhost:8080/druid spring.datasource.druid.stat-view-servlet.enabled=true spring.datasource.druid.stat-view-servlet.url-pattern=/druid/* spring.datasource.druid.stat-view-servlet.login-username=admin spring.datasource.druid.stat-view-servlet.login-password=admin spring.datasource.druid.stat-view-servlet.reset-enable=true spring.datasource.druid.stat-view-servlet.allow=127.0.0.1 ``` 3. 在Spring Boot启动类中添加@EnableAdminServer注解,启用Druid Admin监控平台。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import de.codecentric.boot.admin.server.config.EnableAdminServer; @EnableAdminServer @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 4. 启动Spring Boot应用程序,Druid Admin监控平台即可访问。在浏览器中输入"http://localhost:8080",即可进入Druid Admin监控平台。 希望这些步骤能够帮助你在Spring Boot中成功使用Druid Admin监控平台。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值