说在前面的话,由于我们项目比较老,数据库连接方式还是使用传统的dbcp2,数据库连接信息都是写在配置文件方式里面,由于是银行相关的项目,客户对于敏感信息比较看重,不能轻易的暴露在配置文件里面,因此需要对原有的方式进行改造,使用AES(对称加密,项目需要,可以根据自己实际情况来进行选择)用户名和密码,具体做法如下:
数据库配置文件信息(db.properties):
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://xxxx:3306/mybatis?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=CONVERT_TO_NULL
jdbc.username=eOM3hnWq3bBYg8RRIXXqLw==
jdbc.password=/GikNOUbNLFMNEUf2FeZHA==
spring.xml信息:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<!-- 这个是自定义的读取数据库配置文件占位符类,用来解密用户名和密码-->
<bean id="propertyConfigurer" class="com.laozeng.component.MyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:db.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url&