Tomcat数据源密码密文配置
Tomcat数据源
<Resource
name="jdbc/service_db"
auth="Container"
type="javax.sql.DataSource"
maxTotal="100"
maxIdle="30"
maxWaitMillis="10000"
username="root"
password="密文密码"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521/test" />
Spring的Bean配置
将原来Spring的Bean配置修改为自定义的配置
<bean id="dataSource" class="com.test.MyJndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/service_db</value>
</property>
</bean>
MyJndiObjectFactoryBean
自定义的配置目的是获取到密码后方便修改
package com.test;
import org.springframework.jndi.JndiObjectFactoryBean;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
public class MyJndiObjectFactoryBean extends JndiObjectFactoryBean{
@Override
public Object getObject() {
Object object = super.getObject();
BasicDataSource dbSource = (BasicDataSource) object;
String password = dbSource.getPassword();
if(password.equals("数据源密文")) { // if条件是我测试用的
dbSource.setPassword("解密之后的密码");
}
return dbSource;
}
}
Tomcat dbcp坐标
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-dbcp</artifactId>
<version>8.5.79</version><!--版本跟Tomcat的一致(实际上我的tomcat版本比8.5.79还低)-->
</dependency>
Tocmat数据源密码明文配置
Tomcat数据源
<Resource
name="jdbc/service_db"
auth="Container"
type="javax.sql.DataSource"
maxTotal="100"
maxIdle="30"
maxWaitMillis="10000"
username="root"
password="123456"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521/test" />
Spring的Bean配置
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/service_db</value>
</property>
</bean>