SpringBoot 之jasypt 数据库密码加密

在开发过程中,需要多个开发环境,如开发环境,测试环境,集成环境,线上环境等等,有一些公司会有运维专门维护一套上线的配置文件,在发布时替换掉测试环境的配置文件,但也有一些是直接把线上配置prod和dev写在一起,这样就很不安全了,今天讲解一种配置文件密码加密的方式,可以有效避免这种情况。
参考Spring Boot使用Jasypt加密配置文件

Jasypt Spring Boot为Spring Boot项目的属性提供了加密支持。有三种方式集成jasypt-spring-boot到Spring boot应用中。

[1] jasypt-spring-boot-starter

如果Spring Boot项目中使用了@SpringBootApplication或者@EnableAutoConfiguration,在项目里添加jasypt-spring-boot-starter依赖会自动对项目中整个属性(包括系统属性,环境属性, 命令行参数, application.properties, yaml)启动加密。

Maven依赖如下:

<!--明文配置密码加密解密-->
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>

[2] jasypt-spring-boot

如果项目里没有使用@SpringBootApplication或者@EnableAutoConfiguration,可以手动在Configuration类上添加注解@EnableEncryptableProperties,来在整个环境的属性启用属性加密。

Maven依赖如下:

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

使用:

@Configuration
@EnableEncryptableProperties
public class MyApplication {
    ...
}

[3] 指定加密属性文件

前面两种方法都会在整个环境中启动属性加密。如果想指定加密文件,可以使用@EncryptablePropertySource指定。

Maven依赖:

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

@EncryptablePropertySource指定文件:

@Configuration
@EncryptablePropertySource(name = "EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
    ...
}

@EncryptablePropertySources指定多个文件:

@Configuration
@EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"),
                                 @EncryptablePropertySource("classpath:encrypted2.properties")})
public class MyApplication {
    ...
}

如果项目里使用@SpringBootApplication,可以使用@PropertySource指定加密的文件:

@SpringBootApplication
@EnableEncryptableProperties
@PropertySource(name="EncryptedProperties", value = "classpath:encrypted.properties")
public class MyApplication {
        ...
}

生成密文

cmd窗口打开命令窗口,输入命令:

java -cp D:\mavenspace\repository\org\jasypt\jasypt\1.9.2\jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="root" password=allanpassword algorithm=PBEWithMD5AndDES

image.png

java –cp jar包所在路径\jar包 org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input=”数据库密码” password=加密字段,随意设置algorithm=默认PBEWithMD5AndDES加密

参数说明:
input =数据库链接密码
password=加密字段,随意设置(配置文件中需要添加此密码,相当于约定密码)
algorithm= 算法,默认PBEWithMD5AndDES

配置

OUTPUT 就是加密后的密码,在配置项目密码时结合ENC(密码) 使用,如下图:

image.png

数据库密码加密配置
jasypt:
encryptor:
password: allanpassword
spring MySQL密码写法:
password: ENC(DaCc+YhC4sE9SE5I4FIOQQ==)

启动spring boot应用需要--jasypt.encryptor.password配置前面用来加密明文的密码

java -jar target/jasypt-spring-boot-demo-0.0.1-SNAPSHOT.jar --jasypt.encryptor.password=supersecretz

在这里有一些示例:GitHub - ulisesbocchio/jasypt-spring-boot-samples: Sample apps using jasypt-spring-boot

参考:GitHub - ulisesbocchio/jasypt-spring-boot: Jasypt integration for Spring boot


 


原文地址:https://www.jianshu.com/p/8939e405deeb

参考文章1:Jasypt 开源加密库使用教程_蚩尤后裔的博客-CSDN博客_org.jasypt

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值