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

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依赖如下:

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

[2] jasypt-spring-boot

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

Maven依赖如下:

  1. <dependency>
  2. <groupId>com.github.ulisesbocchio</groupId>
  3. <artifactId>jasypt-spring-boot</artifactId>
  4. <version>2.0.0</version>
  5. </dependency>

使用:

  1. @Configuration
  2. @EnableEncryptableProperties
  3. public class MyApplication {
  4. ...
  5. }

[3] 指定加密属性文件

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

  1. <dependency>
  2. <groupId>com.github.ulisesbocchio</groupId>
  3. <artifactId>jasypt-spring-boot</artifactId>
  4. <version>2.0.0</version>
  5. </dependency>

@EncryptablePropertySource指定文件:

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

@EncryptablePropertySources指定多个文件:

  1. @Configuration
  2. @EncryptablePropertySources({@EncryptablePropertySource("classpath:encrypted.properties"),
  3. @EncryptablePropertySource("classpath:encrypted2.properties")})
  4. public class MyApplication {
  5. ...
  6. }

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

  1. @SpringBootApplication
  2. @EnableEncryptableProperties
  3. @PropertySource(name="EncryptedProperties", value = "classpath:encrypted.properties")
  4. public class MyApplication {
  5. ...
  6. }

生成密文

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

  1. 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加密

参数说明:

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

配置

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

数据库密码加密配置:

  1. jasypt:
  2. encryptor:
  3. password: allanpassword
  4. spring MySQL密码写法:
  5. password: ENC(DaCc+YhC4sE9SE5I4FIOQQ==)

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

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

在这里有一些示例:https://github.com/ulisesbocchio/jasypt-spring-boot-samples
参考:https://github.com/ulisesbocchio/jasypt-spring-boot

作者:老鼠AI大米_Java全栈 链接:https://www.jianshu.com/p/8939e405deeb 来源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。