依赖

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

springboot配置

yml配置

jasypt:
  encryptor:
    password: saltValue    #salt值,密文加盐
spring:
  datasource: # 数据库链接
    db1:
      jdbc-url: jdbc:mysql://x.x.x.x:3306/db_test?useUnicode=true&characterEncoding=utf8&useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=GMT%2B8
      username: root      #也可以加密用户名,依然是ENC()格式,这里没有进行加密
      password: ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+)  #加密了密码,ENC()括号内为密文
      driver-class-name: com.mysql.cj.jdbc.Driver
      mapper-locations: classpath*:mapper/otcmapper/*.xml

启动类添加注解:@EnableEncryptableProperties

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEncryptableProperties
@EnableScheduling
//@EnableAsync
public class SpBatchApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpBatchApplication.class, args);
    }
}

通过明文获取加密的值

cmd在自己的maven仓库目录下执行命令,(要保证依赖下载下来了)
解释:
input:文字的明文
password:加密的盐值(可随意,必须=jasypt:encryptor:password: saltValue)
algorithm:PBEWithMD5AndDES(默认算法)

java -cp org/jasypt/jasypt/1.9.2/jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="密码明文" password=saltValue algorithm=PBEWithMD5AndDES

执行后输出结果:OUTPUT就是密文了,把密文替换yml的属性值就行
ENC(OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+)

----ARGUMENTS-------------------

algorithm: PBEWithMD5AndDES
input: 密码明文
password: saltValue



----OUTPUT----------------------

OVL1V3KDtTa8w9IIOVuSdeyCOsZXAN0+

启动springboot就会自动解密了

通过密文和盐值解密得到明文

java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringDecryptionCLI input="加密后的密文" password=saltValue algorithm=PBEWithMD5AndDES
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐