Springboot数据库配置文件明文密码加密解密

有时候因为安全问题,需要把配置文件的中数据库用户名密码由明文改成密文,大多数其实是为了应付甲方而已。

1.pom.xml引入依赖

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

2.自己想一个秘钥,然后弄一个main方法来测试和生成加密串,下面例子把“password”当做秘钥,加密 xiaoming 字符串。同样可以把加密的打印出来,放到解密里面去验证一下

//给配置文件加密
public static void main(String[] args) {

    // 加密
    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    //自己设置的秘钥
    textEncryptor.setPassword("password");

    String userName = textEncryptor.encrypt("xiaoming");
    System.out.println(userName);

    // 解密
    BasicTextEncryptor textEncryptor2 = new BasicTextEncryptor();
    textEncryptor2.setPassword("password");
    String oldPassword = textEncryptor2.decrypt("avU0Q/XfNMXcgOgowdcfLfB1FDdApc292pzeq8/uvrllChedBJvj4A==");
    System.out.println(oldPassword);
    System.out.println("--------------------------");

}

3.springboot配置文件 application.properties中添加配置

jasypt.encryptor.password=password
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@192.168.100.123:7029:base
spring.datasource.username=ENC(c31B0jWJp3EGFwqSkrUzhY//4CY/sO)
spring.datasource.password=ENC(+KUeW5dB03CxJYz9oVV2flbYW5xs1+)

要先声明秘钥,然后把刚main方法中加密出来的字符串替换原来的,注意一定要用ENC()把字符串包住才行。

然后重启就完事,就是这么简单。

  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
Java中常用的密码加密方法有MD5、SHA、AES等,下面分别介绍它们的使用方法。 MD5加密: MD5是一种不可逆的加密方式,将明文密码转换成一串固定长度的字符串,一般用于存储密码加密Java中可以通过java.security.MessageDigest类实现MD5加密。示例代码如下: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Util { public static String md5(String password) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] bytes = md5.digest(password.getBytes()); StringBuilder stringBuilder = new StringBuilder(); for (byte b : bytes) { int temp = b & 0xff; String hexString = Integer.toHexString(temp); if (hexString.length() == 1) { stringBuilder.append("0").append(hexString); } else { stringBuilder.append(hexString); } } return stringBuilder.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { String password = "123456"; String encryptedPassword = md5(password); System.out.println(encryptedPassword); } } ``` SHA加密: SHA也是一种不可逆的加密方式,与MD5类似,但SHA更安全。Java中可以通过java.security.MessageDigest类实现SHA加密。示例代码如下: ```java import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SHAUtil { public static String sha(String password) { try { MessageDigest sha = MessageDigest.getInstance("SHA"); byte[] bytes = sha.digest(password.getBytes()); StringBuilder stringBuilder = new StringBuilder(); for (byte b : bytes) { int temp = b & 0xff; String hexString = Integer.toHexString(temp); if (hexString.length() == 1) { stringBuilder.append("0").append(hexString); } else { stringBuilder.append(hexString); } } return stringBuilder.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return null; } public static void main(String[] args) { String password = "123456"; String encryptedPassword = sha(password); System.out.println(encryptedPassword); } } ``` AES加密: AES是一种对称加密方式,即加密和解密使用相同的密钥。Java中可以通过javax.crypto.Cipher类实现AES加密。示例代码如下: ```java import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.util.Base64; public class AESUtil { private static final String AES = "AES"; public static String encrypt(String content, String password) { try { KeyGenerator keyGenerator = KeyGenerator.getInstance(AES); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(password.getBytes()); keyGenerator.init(128, secureRandom); SecretKey secretKey = keyGenerator.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES); Cipher cipher = Cipher.getInstance(AES);// 创建密码器 byte[] byteContent = content.getBytes("utf-8"); cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化 byte[] result = cipher.doFinal(byteContent); return Base64.getEncoder().encodeToString(result);// 加密 } catch (Exception e) { e.printStackTrace(); } return null; } public static String decrypt(String content, String password) { try { KeyGenerator keyGenerator = KeyGenerator.getInstance(AES); SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); secureRandom.setSeed(password.getBytes()); keyGenerator.init(128, secureRandom); SecretKey secretKey = keyGenerator.generateKey(); byte[] enCodeFormat = secretKey.getEncoded(); SecretKeySpec key = new SecretKeySpec(enCodeFormat, AES); Cipher cipher = Cipher.getInstance(AES);// 创建密码器 cipher.init(Cipher.DECRYPT_MODE, key);// 初始化 byte[] result = cipher.doFinal(Base64.getDecoder().decode(content)); return new String(result, "utf-8"); } catch (Exception e) { e.printStackTrace(); } return null; } public static void main(String[] args) { String content = "123456"; String password = "password"; String encryptedContent = encrypt(content, password); System.out.println("加密后:" + encryptedContent); String decryptedContent = decrypt(encryptedContent, password); System.out.println("解密后:" + decryptedContent); } } ``` Spring Boot中可以使用Spring Security提供的PasswordEncoder接口来实现密码加密和解密。示例代码如下: ```java import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; public class PasswordUtil { public static String encode(String password) { PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); return passwordEncoder.encode(password); } public static boolean match(String password, String encodedPassword) { PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(); return passwordEncoder.matches(password, encodedPassword); } public static void main(String[] args) { String password = "123456"; String encodedPassword = encode(password); System.out.println("加密后:" + encodedPassword); boolean match = match(password, encodedPassword); System.out.println("匹配结果:" + match); } } ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆趣编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值