mysql 密码 enc_配置文件的属性ENC加密

在micro service体系中,有了config server,我们可以把配置存放在git、svn、数据库等,普通的web项目也基本上是把配置存放在配置文件中。如果我们把大量的配置信息都放在配置文件中是会有安全隐患的,那么如何消除这个隐患呢?最直接的方式就是把配置信息中的一些敏感信息(比如数据库密码、中间件密码)加密,然后程序在获取这些配置的时候解密,就可以达到目的。这个时候,

Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.

High-security, standards-based encryption techniques, both for unidirectional and bidirectional encryption. Encrypt passwords, texts, numbers, binaries...

Transparent integration with Hibernate.

Suitable for integration into Spring-based applications and also transparently integrable with Spring Security.

Integrated capabilities for encrypting the configuration of applications (i.e. datasources).

Specific features for high-performance encryption in multi-processor/multi-core systems.

Open API for use with any JCE provider.

意思其实就是可以把Jasypt这个Java库当成是一个黑盒子,无需深入了解里面怎么运作的,可以拿来直接用(当然,作为码农我们还是要去一探究竟的,个人兴趣自己看吧)。现在基本上项目都集成spring boot了,因此我们可以使用

1. 引入pom包

com.github.ulisesbocchio

jasypt-spring-boot-starter

2.0.0

2. 找到maven仓库中的 jasypt-1.9.2.jar(路径是:.m2\repository\org\jasypt\jasypt\1.9.2)

3. 调用jasypt-1.9.2.jar包中一个main方法:org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI ,源码参考:

8f900a89c6347c561fdf2122f13be562.png View Code

然后执行一下语句:

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="test" password=salt algorithm=PBEWithMD5AndDES

-- input参数:你想要加密的密码

-- password参数:jasypt用来加密你的密码的密码

结果如下图,途中的output就是加密后的密文了。

7a8f8d7fb1dd10429e2a5606f3d3609b.png

4. 修改配置文件,把配置文件中要加密的字段改为:ENC(密文)

48304ba5e6f9fe08f3fa1abda7d326ab.png

system:

mysql:

url: xxx.xxx.xx.xx

username: username

password: ENC(qh8kixDUkvm1DIJrpLFtzw==)

jasypt:

encryptor:

password: salt

48304ba5e6f9fe08f3fa1abda7d326ab.png

以上就可以达到目的。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Jasypt对MySQL密码进行加密,可以将其加密后的值存储在配置文件中,以确保密码的安全性。下面是一个示例: 1. 在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> ``` 2. 在application.properties文件中添加以下属性: ``` # MySQL数据库连接信息 spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root # 加密后的密码,前缀为ENC spring.datasource.password=ENC(encryptPassword) # Jasypt配置 jasypt.encryptor.password=jasyptPassword ``` 3. 创建一个加密工具类,用于执行加密操作: ``` import org.jasypt.encryption.StringEncryptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class JasyptEncryptor { private final StringEncryptor stringEncryptor; @Autowired public JasyptEncryptor(StringEncryptor stringEncryptor) { this.stringEncryptor = stringEncryptor; } public String encrypt(String input) { return stringEncryptor.encrypt(input); } public String decrypt(String encryptedMessage) { return stringEncryptor.decrypt(encryptedMessage); } } ``` 4. 在应用程序中使用加密工具类来加密密码: ``` import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyApp implements CommandLineRunner { private final JasyptEncryptor jasyptEncryptor; @Autowired public MyApp(JasyptEncryptor jasyptEncryptor) { this.jasyptEncryptor = jasyptEncryptor; } @Override public void run(String... args) { String password = "myPassword"; String encryptedPassword = jasyptEncryptor.encrypt(password); System.out.println("Encrypted Password: " + encryptedPassword); } } ``` 在此示例中,我们使用JasyptEncryptor类对密码进行加密,并将加密后的值存储在application.properties文件中。在应用程序中,我们可以使用JasyptEncryptor类来加密和解密密码,并将其用于数据库连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值