Jasypt 加密-整合SpringBoot

引入依赖

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

编写配置

jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES
    password: cf150b74e4824146ad76e9ebe757ba76 #使用加密秘钥

通过程序传递密钥

-Djasypt.encryptor.password=cf150b74e4824146ad76e9ebe757ba76

加密、解密

@Autowired
StringEncryptor encryptor;

@Test
public void testSecret() {
  //加密
  String url = encryptor.encrypt("localhost");
  System.out.println(url);
  //解密
  String decrypt = encryptor.decrypt("nRmCj//nOuErKepiS6dyBO/9Ff7LGCXZ");
  System.out.println(decrypt);
}

使用

mysql:
  host: ENC(pNVN9SKixSnfJ8yZjjIESZuP8HTwG0Hm)

spring:
  thymeleaf:
    suffix: .html
    prefix: classpath:/templates/
    cache: false
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://${mysql.host}:3306/emp?characterEncoding=UTF-8
    username: ENC(M9LpI0WkgVh0rpx+BkOGew==)
    password: ENC(M9LpI0WkgVh0rpx+BkOGew==)

Springboot中使用jasypt进行加密的方式有多种。其中一种方式是引入jasypt-spring-boot加密组件,并在工程中设置加密秘钥。首先,需要在pom.xml文件中添加依赖: ```xml <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>3.0.3</version> </dependency> ``` 接下来,可以通过在应用程序的配置文件(application.properties或application.yml)中设置加密秘钥来实现加密。例如,在application.properties文件中添加以下配置: ``` jasypt.encryptor.password=your_encryption_password ``` 其中"your_encryption_password"是你自己设置的加密密码。这样,加密模块会在应用程序启动时加载秘钥,并使用该秘钥对被加密的数据进行加密和解密操作。 另一种方式是通过脚本的方式进行加密。可以使用jasypt提供的命令行工具来实现加密。例如,在命令行中执行以下命令: ``` java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=your_data password=your_encryption_password algorithm=PBEWithMD5AndDES ``` 其中"your_data"是待加密的内容,"your_encryption_password"是你自己设置的加密密码,"algorithm"是加密算法。执行该命令后,会输出加密后的结果。 总之,Springboot中使用jasypt进行加密可以通过引入jasypt-spring-boot加密组件并设置加密秘钥,或者通过脚本的方式实现加密。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值