jasypt库的使用

3 篇文章 0 订阅

jasypt库的使用

1.简介

Jasypt是一个Java简易加密库,用于加密配置文件中的敏感信息,如数据库密码。jasypt库与springboot集成,在实际开发中非常方便。

2.添加依赖

jasypt开发者开发了starter,添加jasypt-spring-boot-starter依赖就可以了。该库中有使用到slf4j依赖,若单独测试,需添加相应依赖,或直接添加spring-boot-starter依赖。

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

3.添加注解

在Application应用类上添加注解

@EnableEncryptableProperties
启用jasypt。

4.配置使用

将加密后的配置信息使用ENC函数,添加到配置文件中,应用启动加载配置文件时,会自动解密。
Jasypt默认使用的算法为PBEWithMD5AndDES,该算法需要一个加密密钥,可以在应用启动时指定。也可以直接写入配置文件,安全性稍差。

jasypt:
  encryptor:
    password: password

5.测试示例

5.1 准备工作

添加依赖,应用类添加注解。

5.2 添加加密后的属性配置

在配置文件中加入加密后的属性配置信息,我们加密了字符串Password@1,使用的加密密钥为password,添加到application.yml文件中。

jasypt:
  encryptor:
    password: password

encrypted:
  property: ENC(uTSqb9grs1+vUv3iN8lItC0kl65lMG+8)

5.3 添加属性解析类

添加一个类,加载配置文件中的配置信息。

@Service
public class PropertyServiceForJasyptStarter {

	@Value("${encrypted.property}")
	private String property;

	public String getProperty() {
		return property;
	}

	public String getPasswordUsingEnvironment(Environment environment) {
		return environment.getProperty("encrypted.property");
	}
}

5.4 添加测试函数

@RunWith(SpringRunner.class)
@SpringBootTest
public class JasyptSimpleIntegrationTest {

	@Autowired
	PropertyServiceForJasyptStarter service;

	@Autowired
	Environment environment;

	@Test
	public void whenDecryptedPasswordNeeded_GetFromService() {
		System.out.println("service.getProperty() = " + service.getProperty());

		System.out.println("service = " + service.getPasswordUsingEnvironment(environment));
	}
	
	/**
     * 生成加密密文
     * 每次加密后密文不一样
     */
    @Test
    public void testPBECli(){
        String[] args = {"input='Password@1",
                "password=password", "algorithm=PBEWithMD5AndDES"};
        JasyptPBEStringEncryptionCLI.main(args);
    }
}

执行结果如下:

service.getProperty() = Password@1
service = Password@1

6、加密函数

	public static void main(String[] args) {
		// 创建加密对象,默认 PBEWithMD5AndDES
		BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
		// 加密所需的密钥
		textEncryptor.setPassword("password");
		// 加密后的数据(数据库的用户名或密码)
		String encData = textEncryptor.encrypt("Password@1");
		// 解密后的数据(原数据)
		String decData = textEncryptor.decrypt(encData);
		System.out.println("encData: " + encData);
		System.out.println("decData: " + decData);
	}

输出:

encData: uK6xyed60q9NlSBAVb0pFyxA23TYFgtQ
decData: Password@1

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Jasypt是一个Java,用于对敏感数据进行加密和解密,例如密码、API密钥等。Spring框架提供了对Jasypt的支持,可以轻松地将其与Spring集成。 下面是使用Jasypt和Spring实现加密和解密的步骤: 1. 添加Jasypt依赖 在Maven项目中,可以将以下依赖添加到pom.xml文件中: ```xml <dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>${jasypt.version}</version> </dependency> ``` 其中,${jasypt.version}为Jasypt的版本号。 2. 配置加密算法 在Spring的配置文件(例如application.yml或application.properties)中,需要指定所使用的加密算法和密钥。例如: ```yaml jasypt: encryptor: algorithm: PBEWithMD5AndDES password: mySecretKey ``` 这里使用了PBEWithMD5AndDES算法,并将密钥设置为mySecretKey。 3. 在代码中使用加密和解密 要在代码中使用加密和解密,可以使用Spring的Environment对象。例如: ```java @Autowired private Environment environment; public void someMethod() { String encryptedPassword = environment.getProperty("my.password.property"); StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); encryptor.setPassword(environment.getProperty("jasypt.encryptor.password")); String password = encryptor.decrypt(encryptedPassword); } ``` 在上面的代码中,我们首先从环境变量中获取加密的密码,然后使用Jasypt进行解密。要使用Jasypt的默认配置,可以使用以下代码: ```java String password = environment.getProperty("my.password.property"); ``` 这将自动使用配置文件中指定的加密算法和密钥进行解密。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值