spring boot配置文件加密、明文密码加密、properties 和 yml 互相转换

1.引入maven

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

2.添加配置(yml格式的;properties 和 yml 在线转换)

properties 和 yml 在线转换:properties 和 yml 在线转换

jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES		#算法-固定写法一般没人改
    password: 1234qwer				#密钥
    iv-generator-classname: org.jasypt.iv.NoIvGenerator	#设置初始向量IV生成器的类名

3.找到maven仓库下:org\jasypt\jasypt\1.9.3将内容进行加密

  1. 打开到地方后-在地址栏输入cmd打开黑窗口
    在这里插入图片描述

  2. 窗口打开后输入

    java -cp jasypt-1.9.3.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=1234qwer algorithm=PBEWithMD5AndDES
    

    java -cp jasypt-版本根据自己的修改.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=“明文” password=密钥 algorithm=算法 这三个值要和配置文件里一样

    在这里插入图片描述

  3. 输入后回车
    ----OUTPUT----------------------下面的就是 root明文 加密后的内容
    在这里插入图片描述

  4. 将内容复制出来(用鼠标选中加密生成的内容右击就可以复制)-替换掉配置文件里的内容—将配置中的密码root替换成刚刚加密的内容使用ENC()包起来;ENC()是可以改的—不想用的大家可以去百度怎么改
    在这里插入图片描述

4.常见问题及排查方法

  1. 问题1
    Failed to bind properties under 'spring.datasource.password' to java.lang.String:      
    			Reason: org.jasypt.encryption.pbe.config.SimpleStringPBEConfig.setIvGeneratorClassName(Ljava/lang/String;)V
    
  2. 问题2
    Failed to bind properties under 'spring.datasource.password' to java.lang.String:
    			Reason: Failed to bind properties under 'spring.datasource.password' to java.lang.String
    		Action:
    			Update your application's configuration
    
  3. 问题3
    Description:
    		An attempt was made to call the method org.jasypt.encryption.pbe.config.SimpleStringPBEConfig.setIvGeneratorClassName(Ljava/lang/String;)V but it does not exist. Its class, org.jasypt.encryption.pbe.config.SimpleStringPBEConfig, is available from the following locations:
    			jar:file:/D:/yixing/maven/repository_uap/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar!/org/jasypt/encryption/pbe/config/SimpleStringPBEConfig.class
    		It was loaded from the following location:
    			file:/D:/yixing/maven/repository_uap/org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar
    		Action:
    		Correct the classpath of your application so that it contains a single, compatible version of org.jasypt.encryption.pbe.config.SimpleStringPBEConfig
    
  4. 排查方法
    1. 先检查配置文件是否有问题、是否有多余的空格
    2. jasypt-spring-boot-starter.jar 是不是引入版本过高
    3. 不管jasypt-spring-boot-starter.jar是几点几的版本都可以加上iv-generator-classname: org.jasypt.iv.NoIvGenerator配置—不是只有3.0以上的才可以加
    4. 如果上面的都没问题,那就是jar包冲突的原因(问题3就是jar包冲突的原因)
      查看冲突版本:

1.点击maven中的Show Dependencies(那两个向上的箭头)会弹出左面的视图
在这里插入图片描述
2.在视图中搜索加密用到的jar包-问题3中报的就是jasypt.jar包冲突那就直接搜索jasypt了
在这里插入图片描述
3.可以看到下面搜到了两个jasypt;点击其中一个就会定位到jasypt包的旁边还得在找一下才能找到jasypt包可以看到下面是1.9.3和1.9.2冲突了(然后我们可以找找1.9.3和1.9.2、能找到那个就把这个版本换成和另一个一样的就可以了)
在这里插入图片描述

  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值