目录
问题描述
Description:
Failed to bind properties under 'spring.datasource.password' to java.lang.String:
Reason: Failed to bind properties under 'spring.datasource.password' to java.lang.String
Action:
Update your application's configuration
描述:
无法将“ spring.datasource.password”下的属性绑定到java.lang.String:
原因:无法将“ spring.datasource.password”下的属性绑定到java.lang.String
行动:
更新您的应用程序的配置
原因分析
解决方法
步骤1:配置检查
#jasypt加密的密匙(写到启动参数里面)
jasypt:
encryptor:
password: qwert12345
algorithm: PBEWithMD5AndDES
步骤2:更改pom引用jar版本
1)检查pom引用jar版本
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
2)配置无误test重新生成
3)代码
package com.god.demo;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class EncryptTests {
private static final String ALGORITHM_INFO = "PBEWithMD5AndDES";
private static final String PASSWORD_INFO = "qwert12345";
@Test
void contextLoads() {
}
@Autowired
StringEncryptor stringEncryptor;
@Test
public void encryptPwd() {
StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
//配置文件中配置如下的算法
standardPBEStringEncryptor.setAlgorithm(ALGORITHM_INFO);
//配置文件中配置的password
standardPBEStringEncryptor.setPassword(PASSWORD_INFO);
//要加密的文本
String name = standardPBEStringEncryptor.encrypt("root");
String password = standardPBEStringEncryptor.encrypt("root");
String redisPassword = standardPBEStringEncryptor.encrypt("123456");
//将加密的文本写到配置文件中
System.out.println("name=" + name);
System.out.println("password=" + password);
System.out.println("redisPassword=" + redisPassword);
//要解密的文本
String name2 = standardPBEStringEncryptor.decrypt("FarrmxSQX5uwtH/NZRxy+g==");
String password2 = standardPBEStringEncryptor.decrypt("vhiaYB1gl9zPj16yu7uMkA==");
String redisPassword2 = standardPBEStringEncryptor.decrypt("ZII7UphhbVuJ8c3oxPUeyw==");
//解密后的文本
System.out.println("name2=" + name2);
System.out.println("password2=" + password2);
System.out.println("redisPassword2=" + redisPassword2);
}
}
jar高版本+新配置
有位同学加了补充,本人亲测可用。依旧使用3.0.3版本的添加如下配置即可。
因为官方在 3.0.0 以后更改了加密算法,所以假如你不设置的话,使用网上的方法加密出来的密码启动就会报错。
iv-generator-classname: org.jasypt.iv.NoIvGenerator
如图:
参考文章
原文链接:https://blog.csdn.net/libusi001/article/details/108465034