idea环境下,配置文件敏感信息使用ENC加密

在普通的web项目中,常常需要对具有敏感问题的数据进行加密,如数据源的url、账号、密码等。本篇博文介绍的是,在idea环境中,对敏感信息进行ENC加密。加密步骤如下:

1.引入pom依赖
<dependency>
     <groupId>com.github.ulisesbocchio</groupId>
     <artifactId>jasypt-spring-boot-starter</artifactId>
     <version>2.1.1</version>
 </dependency>
2.下载需要使用的工具包

本地需要使用到的工具包,包路径为:repository\org\jasypt\jasypt\1.9.2(其中repository为你本地仓库地址,本地不存在时可添加到pom下载后,再删除

在这里插入图片描述

jasypt-1.9.2.jar依赖
<!-- https://mvnrepository.com/artifact/org.jasypt/jasypt -->
<dependency>
    <groupId>org.jasypt</groupId>
    <artifactId>jasypt</artifactId>
    <version>1.9.2</version>
</dependency>
3.执行命令语句,获取加密后密文

依赖和工具包下载完毕后,打开idea 的Terminal命令窗口界面,选到你本地工具依赖jasypt-1.9.2.jar所在目录,或者cmd命令窗口。

执行命令语句:

java -cp jasypt-1.9.2.jar  org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="123456" password=123456 algorithm=PBEWithMD5AndDES
——input:为需要加密的数据,如密码,账号等
——password:为加密数据使用的密钥
——algorithm:为默认算法

实际操作,如下图:
在这里插入图片描述
OUTPUT为加密后所得密文。

4.修改配置文件,添加解密密钥

然后对需要加密的数据所在的配置文件做出对应的修改,修改样式如下:

加密前:

spring:
  data:
    mongodb1:
      uri: mongodb://abc:abc*2020@10.88.37.75:27017/abc
      database: abc
加密后:
spring:
  data:
    mongodb1:
      uri: ENC(7eWO6DlvfoKUF58FASFA7EFS2ZCc3EuWDUMILTpevqXMSITBK7wAZstD82ASF38F2AFF22rfjyQ=)
      database: abc
jasypt:
  encryptor:
    #用来加解密的salt值
    password: 123456

文章借鉴:idea环境下,配置文件敏感信息使用ENC加密

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值