SpringBoot配置文件里,如何实施两种数据库密码加密方案,哪种更推荐?

2026-06-09 04:133阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1407个文字,预计阅读时间需要6分钟。

SpringBoot配置文件里,如何实施两种数据库密码加密方案,哪种更推荐?

SpringBoot项目中,通常会将连接数据库的密码以明文形式存储在配置文件中,这会导致安全性较低。尤其是在对安全性要求较高的企业环境中,这种做法显然不够安全。因此,我们考虑对密码进行加密处理。

以下是两种常见的加密方式:

1. Jasypt加密: Jasypt(Java Simplified Encryption)是一个Java库,可以简化Java应用程序中的加密操作。使用Jasypt,我们可以将密码加密后存储在配置文件中,然后在需要时解密。

示例: - 加密密码:`jasypt -c yourPassword` - 配置文件中使用加密后的密码:

spring.datasource.password=${jasypt:ENC(hashedPassword)}

- 解密密码:`jasypt -d hashedPassword`

2. 其他加密库: 除了Jasypt,还有其他一些加密库,如Bouncy Castle、Apache Commons Codec等,它们也提供了密码加密的功能。

示例: - 使用Bouncy Castle加密:

java import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.crypto.engines.AESFastEngine; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.paddings.PKCS7Padding; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.CipherParameters; import org.bouncycastle.crypto.BlockCipher; import org.bouncycastle.crypto.Cipher;

public class PasswordEncryptor { private static final String AES=AES; private static final String AES_CBC=AES/CBC/PKCS7Padding; private static final String ENCRYPTION_KEY=yourEncryptionKey; private static final String SALT=yourSalt;

public static String encryptPassword(String password) throws Exception { Cipher cipher=new CBCBlockCipher(new AESFastEngine()); cipher.init(true, new CipherParameters(new KeyParameter(ENCRYPTION_KEY.getBytes()), new PKCS7Padding())); byte[] encryptedPassword=cipher.processBlock(password.getBytes(), 0, password.getBytes().length); return Base64.toBase64String(encryptedPassword); }

public static String decryptPassword(String encryptedPassword) throws Exception { Cipher cipher=new CBCBlockCipher(new AESFastEngine()); cipher.init(false, new CipherParameters(new KeyParameter(ENCRYPTION_KEY.getBytes()), new PKCS7Padding())); byte[] decryptedPassword=cipher.processBlock(Base64.fromBase64String(encryptedPassword), 0, Base64.fromBase64String(encryptedPassword).length); return new String(decryptedPassword); } }

- 配置文件中使用加密后的密码:

spring.datasource.password=${passwordEncryptor.encryptPassword(yourPassword)}

通过以上两种方式,我们可以有效地对SpringBoot项目中的密码进行加密,提高安全性。

SpringBoot项目经常将连接数据库的密码明文放在配置文件里,安全性就比较低一些,尤其在一些企业对安全性要求很高,因此我们就考虑如何对密码进行加密。

介绍两种加密方式:jasypt 可加密配置文件中所有属性值; druid 自带了加解密,可对数据库密码进行加密。

jasypt 加解密

jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring 项目中。可以快速集成到 Spring Boot 项目中,并提供了自动配置,使用非常简单。

步骤如下:

1)引入maven依赖

<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>

2)在配置文件application

#jasypt加密的盐值 jasypt.encryptor.password=erp

3)测试用例中生成加密后的密匙

@Autowired StringEncryptor encryptor; @Test public void getPass() { String url = encryptor.encrypt("jdbc:mysql://127.0.0.1:3306/erp-framework?useUnicode=true&characterEncoding=utf8"); String name = encryptor.encrypt("root"); String password = encryptor.encrypt("mysql"); System.out.println(url+"----------------"); System.out.println(name+"----------------"); System.out.println(password+"----------------"); Assert.assertTrue(name.length() > 0); Assert.assertTrue(password.length() > 0); }

执行后,会生成:

BA9uZ35umFic6NbuaLdGzZBodw/wSqvztMt9UGdlOtxxs/fr/W5kf8Bs6GzzHklNfkcU30g8aQ/XdihsZtqRz1J34zNIQxuH3BCG1kknFayp13G8RhkeF4ptBfx6i6nqnP4Uc0UKpjcsxxfTZImHBVvcTY0RDANk26IGBPZvQry7qKuna/RTMQ==
kyMvAncHqzcvGAildsK67w==
7QCSL5/HKjxFQRPLGgGH7kAElrmf/mgQ

4)将上面的生成的密匙如下替换,此处主要是数据库密码密文使用ENC进行标识

first.spring.datasource.password=ENC(7QCSL5/HKjxFQRPLGgGH7kAElrmf/mgQ)

ENC( )是固定写法,( )里面是加密后的信息。

druid 非对称加密

数据库连接池 Druid 自身支持对数据库密码的加密解密,具体可以看git:github.com/alibaba/druid/wiki/%E5%A6%82%E4%BD%95%E5%9C%A8Spring-Boot%E4%B8%AD%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86%EF%BC%9F

1)引入maven依赖

SpringBoot配置文件里,如何实施两种数据库密码加密方案,哪种更推荐?

<!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency>

2)在配置文件application

first.spring.datasource.username = root first.spring.datasource.password = iujrgO5nD09lQfS9+SbTvOf4Wcp8jtJOSDnqSEq1jnFEUYW3UE/2bMMLv/Y74wSXUPUu6H6L/SJWkYpYujZ9AA== first.spring.datasource.publicKey = MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJHmM9MkpAGxAnx/6+b2zNUIXYQUpgIOQBbEzkBmhgrrOa2oxYNzm7zYGuBlAugYzk1zSW8SAVT/b9ihGvHqutsCAwEAAQ

其中first.spring.datasource.password为第三步中生成的

3)测试用例中生成加密后的密匙

@Test public void druidEncrypt() throws Exception { //密码明文 String password = "mysql"; System.out.println("明文密码: " + password); String[] keyPair = ConfigTools.genKeyPair(512); //私钥 String privateKey = keyPair[0]; //公钥 String publicKey = keyPair[1]; //用私钥加密后的密文 password = ConfigTools.encrypt(privateKey, password); System.out.println("privateKey:" + privateKey); System.out.println("publicKey:" + publicKey); System.out.println("password:" + password); String decryptPassword = ConfigTools.decrypt(publicKey, password); System.out.println("解密后:" + decryptPassword); }

执行后,会生成:

明文密码: mysql
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAglWrmLR1m+uUmfrscYuHda/iWFOfCcXv8DswDEvkvAz62dE3iJGJuhDa38LlbD+ps9kvLfPzaKcCiXWq2+ICzwIDAQABAkBA+rlcnsvr+p3RzxpG2culTxVH+BjYZIjlenzQBJ57aj8aylcm1x56usRNV1NGWeBJhAzJyXXi+v72goI3aqeRAiEAt0KGJMDvpcwM3aGUPIcFDa0YaU9KoqqwnwYUd7d5YSMCIQC2EUnF8NecvgpDycXqvjurh1qKLyfuYNg9fHEpaWmQZQIhAKtE99tnmVEJR8jmVoTO5zEl4ZeiLC2kepbUdtJ/3WejAiEAlAk0idxNA+ZFZF7gLQEmdU6l4Gt9NUbLLBtNy5UpPz0CIDS9ifH9wHONgLDEWh2w0lmxHni/LCQFbFunUzyAJTRZ
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIJVq5i0dZvrlJn67HGLh3Wv4lhTnwnF7/A7MAxL5LwM+tnRN4iRiboQ2t/C5Ww/qbPZLy3z82inAol1qtviAs8CAwEAAQ==
password:H8wvdSyjc9gAsdlbJ23G6kkgp4dEAwGRd6DABCftxBznVag5t+m/XWaRgtuznX0e5uXH2wxupeLiOUojo20IVg==
解密后:mysql

注意:替换完密文的密码后,再application.properties中还得配置:

# 配置监控统计拦截的filters first.spring.datasource.filters=stat,log4j2,config # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 first.spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=${first.spring.datasource.publicKey}

本文中对application.properties中的属性都是自定义的,会在项目启动的时候,通过@Configuration来加载,

完整的代码可以从git上获取:github.com/chyanwu/erp-framework

总结

以上所述是小编给大家介绍的SpringBoot配置文件中数据库密码加密两种方案,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自由互联网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

本文共计1407个文字,预计阅读时间需要6分钟。

SpringBoot配置文件里,如何实施两种数据库密码加密方案,哪种更推荐?

SpringBoot项目中,通常会将连接数据库的密码以明文形式存储在配置文件中,这会导致安全性较低。尤其是在对安全性要求较高的企业环境中,这种做法显然不够安全。因此,我们考虑对密码进行加密处理。

以下是两种常见的加密方式:

1. Jasypt加密: Jasypt(Java Simplified Encryption)是一个Java库,可以简化Java应用程序中的加密操作。使用Jasypt,我们可以将密码加密后存储在配置文件中,然后在需要时解密。

示例: - 加密密码:`jasypt -c yourPassword` - 配置文件中使用加密后的密码:

spring.datasource.password=${jasypt:ENC(hashedPassword)}

- 解密密码:`jasypt -d hashedPassword`

2. 其他加密库: 除了Jasypt,还有其他一些加密库,如Bouncy Castle、Apache Commons Codec等,它们也提供了密码加密的功能。

示例: - 使用Bouncy Castle加密:

java import org.bouncycastle.util.encoders.Base64; import org.bouncycastle.crypto.engines.AESFastEngine; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.paddings.PKCS7Padding; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.CipherParameters; import org.bouncycastle.crypto.BlockCipher; import org.bouncycastle.crypto.Cipher;

public class PasswordEncryptor { private static final String AES=AES; private static final String AES_CBC=AES/CBC/PKCS7Padding; private static final String ENCRYPTION_KEY=yourEncryptionKey; private static final String SALT=yourSalt;

public static String encryptPassword(String password) throws Exception { Cipher cipher=new CBCBlockCipher(new AESFastEngine()); cipher.init(true, new CipherParameters(new KeyParameter(ENCRYPTION_KEY.getBytes()), new PKCS7Padding())); byte[] encryptedPassword=cipher.processBlock(password.getBytes(), 0, password.getBytes().length); return Base64.toBase64String(encryptedPassword); }

public static String decryptPassword(String encryptedPassword) throws Exception { Cipher cipher=new CBCBlockCipher(new AESFastEngine()); cipher.init(false, new CipherParameters(new KeyParameter(ENCRYPTION_KEY.getBytes()), new PKCS7Padding())); byte[] decryptedPassword=cipher.processBlock(Base64.fromBase64String(encryptedPassword), 0, Base64.fromBase64String(encryptedPassword).length); return new String(decryptedPassword); } }

- 配置文件中使用加密后的密码:

spring.datasource.password=${passwordEncryptor.encryptPassword(yourPassword)}

通过以上两种方式,我们可以有效地对SpringBoot项目中的密码进行加密,提高安全性。

SpringBoot项目经常将连接数据库的密码明文放在配置文件里,安全性就比较低一些,尤其在一些企业对安全性要求很高,因此我们就考虑如何对密码进行加密。

介绍两种加密方式:jasypt 可加密配置文件中所有属性值; druid 自带了加解密,可对数据库密码进行加密。

jasypt 加解密

jasypt 是一个简单易用的加解密Java库,可以快速集成到 Spring 项目中。可以快速集成到 Spring Boot 项目中,并提供了自动配置,使用非常简单。

步骤如下:

1)引入maven依赖

<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency>

2)在配置文件application

#jasypt加密的盐值 jasypt.encryptor.password=erp

3)测试用例中生成加密后的密匙

@Autowired StringEncryptor encryptor; @Test public void getPass() { String url = encryptor.encrypt("jdbc:mysql://127.0.0.1:3306/erp-framework?useUnicode=true&characterEncoding=utf8"); String name = encryptor.encrypt("root"); String password = encryptor.encrypt("mysql"); System.out.println(url+"----------------"); System.out.println(name+"----------------"); System.out.println(password+"----------------"); Assert.assertTrue(name.length() > 0); Assert.assertTrue(password.length() > 0); }

执行后,会生成:

BA9uZ35umFic6NbuaLdGzZBodw/wSqvztMt9UGdlOtxxs/fr/W5kf8Bs6GzzHklNfkcU30g8aQ/XdihsZtqRz1J34zNIQxuH3BCG1kknFayp13G8RhkeF4ptBfx6i6nqnP4Uc0UKpjcsxxfTZImHBVvcTY0RDANk26IGBPZvQry7qKuna/RTMQ==
kyMvAncHqzcvGAildsK67w==
7QCSL5/HKjxFQRPLGgGH7kAElrmf/mgQ

4)将上面的生成的密匙如下替换,此处主要是数据库密码密文使用ENC进行标识

first.spring.datasource.password=ENC(7QCSL5/HKjxFQRPLGgGH7kAElrmf/mgQ)

ENC( )是固定写法,( )里面是加密后的信息。

druid 非对称加密

数据库连接池 Druid 自身支持对数据库密码的加密解密,具体可以看git:github.com/alibaba/druid/wiki/%E5%A6%82%E4%BD%95%E5%9C%A8Spring-Boot%E4%B8%AD%E9%85%8D%E7%BD%AE%E6%95%B0%E6%8D%AE%E5%BA%93%E5%AF%86%E7%A0%81%E5%8A%A0%E5%AF%86%EF%BC%9F

1)引入maven依赖

SpringBoot配置文件里,如何实施两种数据库密码加密方案,哪种更推荐?

<!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.12</version> </dependency>

2)在配置文件application

first.spring.datasource.username = root first.spring.datasource.password = iujrgO5nD09lQfS9+SbTvOf4Wcp8jtJOSDnqSEq1jnFEUYW3UE/2bMMLv/Y74wSXUPUu6H6L/SJWkYpYujZ9AA== first.spring.datasource.publicKey = MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJHmM9MkpAGxAnx/6+b2zNUIXYQUpgIOQBbEzkBmhgrrOa2oxYNzm7zYGuBlAugYzk1zSW8SAVT/b9ihGvHqutsCAwEAAQ

其中first.spring.datasource.password为第三步中生成的

3)测试用例中生成加密后的密匙

@Test public void druidEncrypt() throws Exception { //密码明文 String password = "mysql"; System.out.println("明文密码: " + password); String[] keyPair = ConfigTools.genKeyPair(512); //私钥 String privateKey = keyPair[0]; //公钥 String publicKey = keyPair[1]; //用私钥加密后的密文 password = ConfigTools.encrypt(privateKey, password); System.out.println("privateKey:" + privateKey); System.out.println("publicKey:" + publicKey); System.out.println("password:" + password); String decryptPassword = ConfigTools.decrypt(publicKey, password); System.out.println("解密后:" + decryptPassword); }

执行后,会生成:

明文密码: mysql
privateKey:MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAglWrmLR1m+uUmfrscYuHda/iWFOfCcXv8DswDEvkvAz62dE3iJGJuhDa38LlbD+ps9kvLfPzaKcCiXWq2+ICzwIDAQABAkBA+rlcnsvr+p3RzxpG2culTxVH+BjYZIjlenzQBJ57aj8aylcm1x56usRNV1NGWeBJhAzJyXXi+v72goI3aqeRAiEAt0KGJMDvpcwM3aGUPIcFDa0YaU9KoqqwnwYUd7d5YSMCIQC2EUnF8NecvgpDycXqvjurh1qKLyfuYNg9fHEpaWmQZQIhAKtE99tnmVEJR8jmVoTO5zEl4ZeiLC2kepbUdtJ/3WejAiEAlAk0idxNA+ZFZF7gLQEmdU6l4Gt9NUbLLBtNy5UpPz0CIDS9ifH9wHONgLDEWh2w0lmxHni/LCQFbFunUzyAJTRZ
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAIJVq5i0dZvrlJn67HGLh3Wv4lhTnwnF7/A7MAxL5LwM+tnRN4iRiboQ2t/C5Ww/qbPZLy3z82inAol1qtviAs8CAwEAAQ==
password:H8wvdSyjc9gAsdlbJ23G6kkgp4dEAwGRd6DABCftxBznVag5t+m/XWaRgtuznX0e5uXH2wxupeLiOUojo20IVg==
解密后:mysql

注意:替换完密文的密码后,再application.properties中还得配置:

# 配置监控统计拦截的filters first.spring.datasource.filters=stat,log4j2,config # 通过connectProperties属性来打开mergeSql功能;慢SQL记录 first.spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=true;config.decrypt.key=${first.spring.datasource.publicKey}

本文中对application.properties中的属性都是自定义的,会在项目启动的时候,通过@Configuration来加载,

完整的代码可以从git上获取:github.com/chyanwu/erp-framework

总结

以上所述是小编给大家介绍的SpringBoot配置文件中数据库密码加密两种方案,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对自由互联网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!