jasypt-spring-boot-starter用于对SpringBoot配置文件中的关键信息加密

1,468 阅读1分钟

1、引入maven依赖

<dependencies>
	...
	<dependency>
		<groupId>com.github.ulisesbocchio</groupId>
		<artifactId>jasypt-spring-boot-starter</artifactId>
		<version>${jasypt.version}</version>
	</dependency>
	...
<dependencies>

2、application-dev.yml配置

jasypt:  
  encryptor:    
    password: lynseries #设置跟密码,类似盐

3、获取加密后的值

使用@SpringBootTest

package com.lynseries.test
import lombok.extern.slf4j.Slf4j;
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;

/**
 * @author lynseries
 * @date
 */
@SpringBootTest(classes = TestApplication.class)
public class MyApplicationTest {

	@Autowired
	private StringEncryptor stringEncryptor;

	@Test
	public void testJasypt(){
		String enpassword=stringEncryptor.encrypt("lynseries");

		System.out.println("encode password:" + enpassword);
		System.out.println("decode password:" + stringEncryptor.decrypt(enpassword));
	}
}


运行结果:
encode password:t4dRt2z++RAMMG9u9ObUIQp3mf8FTi0v
decode password:lynseries

4、在配置文件中使用

spring:
  datasource:
    password: ENC(t4dRt2z++RAMMG9u9ObUIQp3mf8FTi0v)