Druid对数据库密码加密的坑
背景:
在对已有项目搭建本地环境,修改了本地ip端口和数据库帐号密码(使用了明文)。
然后项目一直跑不起来,还抛出各种异常,经过分析发现主要错在这里:druid java.lang.IllegalArgumentException: Failed to decrypt.
解密失败?哪里要解密?想了一下,原来的这么长的密码是密文,现在被我改成了这么短的明文。
解决方案:
1、不使用密文
将数据库配置中的<property name = "filters" value = "config" />删除。如有value的值有多个(比如<property name="filters" value="stat,slf4j,config"/>),删除其中的config。
2、使用密文(对明文加密)
找到maven本地库druid的jar包所在路径,该路径下打开cmd命令窗口。
java -cp druid-1.0.27.jar com.alibaba.druid.filter.config.ConfigTools yourpassword
其中druid-1.0.27.jar替换成你的版本,yourpassword替换成你的密码。
然后会出现
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA1j7Oorws/mu2MmHl4qy/8wxpo+E6ypn/cfatwIrrblTw1m/KIHvlNug1JX7S/+6oH8flqLziLadu5FPtCAxD/QIDAQABAkBDPdkq2Dai73lynKlDGIbyKG4rgjAqkGTNBugHq1IzASC1UlSK/rfuKc7Az4ee/mS6X0LRdFoCL566fMUcX8PdAiEA8aMMrhCk22Zy26DJyD56BZ6tGyrhGqo+rtegUr22328CIQDi+vHHYDZjLWuRd69sbLlAqE819zQ9OFcNnHh295HdUwIhAJo/NfnrYAE57bnrcsNjtmrd9qyv+5AU+vyLYqlOt2z7AiEAnnbH+YPINLFvGd3PEQlOxD89HdX6GIuO6yfESnpI0ZUCIDB9bCRYKMI7KXx3l1dxVUVjwrW6kpeWMlAqdhV5gMl8 publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANY+zqK8LP5rtjJh5eKsv/MMaaPhOsqZ/3H2rcCK625U8NZvyiB75TboNSV+0v/uqB/H5ai84i2nbuRT7QgMQ/0CAwEAAQ== password:d3hCh7tS8Fy9d0hpZ4AuaRx7tfusY1Db+7TGZeHuDNclWob24eqMgUpa0ZofLU/GXumb8qR0RLJfuGYDgK4cxQ==
最后替换原来的密文和公钥。
整体的数据库配置展示:
<bean id = "dataSource" class = "com.alibaba.druid.pool.DruidDataSource" destroy-method = "close" > <!-- 数据库基本信息配置 --> <property name = "url" value = "${url}" /> <property name = "username" value = "${username}" /> <property name = "password" value = "${password}" /> **主要看这里!!!** 第一 <property name = "driverClassName" value = "${driverClassName}" /> <property name = "filters" value = "config" /> **主要看这里!!!!** 第二 <!-- 最大并发连接数 --> <property name = "maxActive" value = "${maxActive}" /> <!-- 初始化连接数量 --> <property name = "initialSize" value = "${initialSize}" /> <!-- 配置获取连接等待超时的时间 --> <property name = "maxWait" value = "${maxWait}" /> <!-- 最小空闲连接数 --> <property name = "minIdle" value = "${minIdle}" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name = "timeBetweenEvictionRunsMillis" value ="${timeBetweenEvictionRunsMillis}" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name = "minEvictableIdleTimeMillis" value ="${minEvictableIdleTimeMillis}" /> <property name = "validationQuery" value = "${validationQuery}" /> <property name = "testWhileIdle" value = "${testWhileIdle}" /> <property name = "testOnBorrow" value = "${testOnBorrow}" /> <property name = "testOnReturn" value = "${testOnReturn}" /> <property name = "maxOpenPreparedStatements" value ="${maxOpenPreparedStatements}" /> <!-- 打开 removeAbandoned 功能 --> <property name = "removeAbandoned" value = "${removeAbandoned}" /> <!-- 1800 秒,也就是 30 分钟 --> <property name = "removeAbandonedTimeout" value ="${removeAbandonedTimeout}" /> <!-- 关闭 abanded 连接时输出错误日志 --> <property name = "logAbandoned" value = "${logAbandoned}" /> <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${publicKey}" /> **还有这里!!!**第三 </bean>
参考:https://blog.csdn.net/woshfhsefksdf/article/details/54412375