首页  编辑  

Spring Boot项目明文简单加密(jasypt)

Tags: /Java/   Date Created:

添加 POM 依赖

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

添加 MAVEN 插件

<build>
  <plugins>
    <plugin>
      <groupId>com.github.ulisesbocchio</groupId>
      <artifactId>jasypt-maven-plugin</artifactId>
      <version>3.0.4</version>
    </plugin>
  </plugins>
</build>

修改 Application Properties 相关配置

# 添加 jasypt 配置
jasypt:
  encryptor:
    algorithm: PBEWithMD5AndDES
    # 加盐加密
    password: the-salt-password-for-encrypted

# 明文加密配置
secret:
    # property: the-value-need-to-be-encrypted
  # 加密后密文为: entrypted-value-you-need
    property: ENC(entrypted-value-you-need)

生成密文的方式

  • Java 代码加密
public static String generateEncryptedValue(String salt, String value) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword(salt);
        encryptor.setIvGenerator(new RandomIvGenerator());
        return encryptor.encrypt(value);
}
  • Maven 命令行加密
mvn jasypt:encrypt-value -Djasypt.encryptor.password="the-salt-password-for-encrypted" -Djasypt.plugin.value="the-value-need-to-be-encrypted"

References:

Source Code: