加上依赖
<!-- 保护springboot配置文件加密-->
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
配置
# jasypt加密的密匙
jasypt:
encryptor:
password: Y6M9fAJQdU7jNp5MW
package springboot.test;
import org.jasypt.encryption.StringEncryptor;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* @author cruder
* @since 2021/9/17
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class DatabaseTest {
@Autowired
private StringEncryptor encryptor;
@Test
public void getPass() {
String url = encryptor.encrypt("jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8");
String name = encryptor.encrypt("root");
String password = encryptor.encrypt("123456");
System.out.println("url: " + url);
System.out.println("username: " + name);
System.out.println("password: " + password);
Assert.assertTrue(url.length() > 0);
Assert.assertTrue(name.length() > 0);
Assert.assertTrue(password.length() > 0);
}
}
conlose输出:
url: f0cuu4F0E5JveF6VbBzF4L8JVlPhDiUAKRauQAMvO71ZdHO8jbFcy39EKGoMQgoc6iOBQY1Fe2h166paeDuMxICoobyZS+oFU36bTh8GUlJJgcItFCohNNaWl5Lv+tzN
username: w70pSwXkQpiwDvRRvUfHIQ==
password: NqMKAVdTDda7rVDdIIKPsg==
把配置文件对应部分改了
然后发现居然能正常启动,
盐巴是放在配置文件里的不安全,
为了防止salt(盐)泄露,反解出密码.可以在项目部署的时候使用命令传入salt(盐)
java -jar xxx.jar -Djasypt.encryptor.password=Y6M9fAJQdU7jNp5MW