Spring Boot配置文件中的配置项加密&jasypt使用

介绍如何使用jasypt对Spring Boot配置文件中的敏感信息进行加密处理,包括安装配置、加密过程及配置使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用jasypt对Spring Boot配置文件中的配置项加密

在Spring Boot中,有很多口令需要加密,如数据库连接密码、访问第三方接口的Token等。常见的方法就是用jasypt对口令进行加密。

实际上,jasypt可以对配置文件中任意配置项的值进行加密,不局限于对密码的加密。


1.在pom.xml中添加jasypt相关依赖

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

2.对需要加密的字符串进行加密,获取密文

网上有很多文章告诉你通过命令行,直接指定加密算法、加密实现类等参数,调用jasypt-x.x.x.jar对字符串进行加密。由于不同版本jasypt的默认加密算法和其它默认配置有差异,而且要想知道pom.xml中依赖的jasypt-spring-boot-starter依赖的jasypt-x.x.x.jar具体是哪个版本,还得查看jasypt-spring-boot-starterpom.xml文件,再查看其parent的pom.xml文件,找到jasypt.version的值,才能知道jasypt-x.x.x.jar的具体版本。

所以,直接在Spring Boot中注入用于字符串加密的Bean,然后调用加密方法对字符串加密。加密完成后,加密的代码就不再需要了(后续如果还有加密需求,重新用下面代码加密一遍即可)。

注:执行下面代码前,请参照修改配置文件在配置文件配置加密口令jasypt.encryptor.password;或参照IDEA中如何注入jasypt配置?,在启动时通过参数配置加密口令jasypt.encryptor.password

加密代码:

package com.example.study;

import org.jasypt.encryption.StringEncryptor;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;

@SpringBootTest
public class MyPropertiesTest {
    /**
     * 用于加密字符串的接口,默认实现类是DefaultLazyEncryptor
     */
    @Autowired
    private StringEncryptor encryptor;

    @Test
    public void encryptProperties() {
        // 需要加密的字符串
        List<String> properties = new ArrayList<>();
        properties.add("firstValue");
        properties.add("secondValue");
        // 加密后的字符串
        List<String> encryptStrs = new ArrayList<>();
        System.out.println("加密字符串(明文:密文)");
        for (String property : properties) {
            String encryptStr = encryptor.encrypt(property);
            System.out.println(property + ":" + encryptStr);
            encryptStrs.add(encryptStr);
        }
        System.out.println("解密字符串(密文:明文)");
        for (String encryptStr : encryptStrs) {
            System.out.println(encryptStr + ":" + encryptor.decrypt(encryptStr));
        }
    }
}

3.修改配置文件

得到步骤2完成加密后的密文,将密文用ENC()包裹起来(形如ENC(密文)),替换配置文件中的明文字符串。

说明:

  • ENC()分别是jasypt默认的密文前缀和后缀,分别可以通过jasypt.encryptor.property.prefixjasypt.encryptor.property.suffix修改默认的密文前缀、后缀;

  • 只有jasypt.encryptor.password=MyEncryptPassword一个配置是必须的,且实际运用中一般通过环境变量或项目参数注入,不写在配置文件中。

# jasypt配置
# 用户指定的加密口令,必填。实际运用中一般通过环境变量或项目参数注入
# jasypt.encryptor.password=MyEncryptPassword
# 修改密文前缀,非必须
# jasypt.encryptor.property.prefix=ENC(
# 修改密文后缀,非必须
# jasypt.encryptor.property.suffix=)

# 用密文替换明文字符串
my.test.key.first=ENC(riVj8PtdhOcX6sd8Peie2zzunc93u+bPOKc2Nrw8wr6DOHEj2hhHyculwLH6PoXc)
my.test.key.second=ENC(mdChittS38s+ehLRgM+KCw5Bze9LbdxVvddEGzwLYZ3bRjWC+0t5+prIt5GK6yyl)

4.使用

直接跟以前一样用@Value获取配置就行,无需做其它特殊配置。

以下是一个获取配置的示例:

package com.example.study.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class MyPropertiesConfig {
    @Value("${my.test.key.first}")
    private String first;

    @Value("${my.test.key.second}")
    private String second;

    @PostConstruct
    public void printProperties() {
        System.out.println("first:" + first);
        System.out.println("second:" + second);
    }
}

启动后,相关输出如下:

first:firstValue
second:secondValue

问题:

1.步骤2中对字符串加密时出现以下报错:
org.jasypt.exceptions.EncryptionOperationNotPossibleException: Encryption raised an exception. A possible cause is you are using strong encryption algorithms and you have not installed the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files in this Java Virtual Machine

原因:

加密强度受限。我使用的是java 1.8.0_101,可以通过在Oracle官网下载JCE,用来替换原本的jar包即可。

解决方法:

  1. 从Oracle下载JCE包(需要登陆):https://www.oracle.com/java/technologies/javase-jce8-downloads.html
  2. 将下载的zip包解压,在里面能找到三个文件local_policy.jarUS_export_policy.jarREADME.txt
  3. local_policy.jarUS_export_policy.jar替换掉%JAVA_HOME%\jre\lib\security\中的local_policy.jarUS_export_policy.jar
2.IDEA中如何注入jasypt配置?
  1. 打开IDEA右上角Edit Run/Debug configurations
  2. 对于Spring Boot,在左侧选中需要设置的应用后,在Program arguments添加配置--jasypt.encryptor.password=MyEncryptPassword
  3. 对于JUnit,在左侧选中需要设置的应用后,在Environment Variables添加配置--jasypt.encryptor.password=MyEncryptPassword

如图:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值