首页 > 编程语言 > 详细

SpringBoot整合Jasypt加密

时间:2021-09-12 10:14:03      阅读:32      评论:0      收藏:0      [点我收藏+]
引入依赖
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

编写yml配置
jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES  #加密算法
    password: cf150b74e4824146ad76e9ebe757ba76 #使用加密秘钥(自己写自己记住)

提示:如果不想密钥暴露,可以通过程序传递密钥,将密钥通过参数传给jvm  -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:
  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==)

格式:ENC(加密后的字符串)

SpringBoot整合Jasypt加密

原文:https://www.cnblogs.com/codegzy/p/15252654.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!