개발용 메모장...

[Java]/[Spring]

[SpringBoot] Jasypt 설정( 프로퍼티 암호화 )

redeyesboy 2024. 8. 26. 15:05

1. 의존성 추가

- pom.xml

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

 

2. Jasypt 관련 설정 추가

- application.properties

jasypt.encryptor.password=비밀번호키
jasypt.encryptor.algorithm=PBEWITHHMACSHA512ANDAES_256
jasypt.encryptor.key-obtention-iterations=1000
jasypt.encryptor.pool-size=1
jasypt.encryptor.salt-generator-classname=org.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.string-output-type=base64
jasypt.encryptor.iv-generator-classname=org.jasypt.iv.RandomIvGenerator

 

설정값과 관련된 내용은 아래 링크 참조.

https://github.com/ulisesbocchio/jasypt-spring-boot

 

GitHub - ulisesbocchio/jasypt-spring-boot: Jasypt integration for Spring boot

Jasypt integration for Spring boot. Contribute to ulisesbocchio/jasypt-spring-boot development by creating an account on GitHub.

github.com

 

3. Spring Boot 설정 파일 추가

@Configuration
@EnableEncryptableProperties
public class JasyptConfig {

	@Bean
	@ConfigurationProperties(prefix = "jasypt.encryptor")
	SimpleStringPBEConfig simpleStringPBEConfig() {
		return new SimpleStringPBEConfig();
	}

	@Bean(name = "jasyptEncryptor")
	StringEncryptor stringEncryptor(SimpleStringPBEConfig simpleStringPBEConfig) {
		PooledPBEStringEncryptor pooledPBEStringEncryptor = new PooledPBEStringEncryptor();
		pooledPBEStringEncryptor.setConfig(simpleStringPBEConfig);
		return pooledPBEStringEncryptor;
	}

}

 

4. 테스트

@Slf4j
@SpringBootTest
class ApplicationTests {

	@Autowired
	@Qualifier("jasyptEncryptor")
	private PooledPBEStringEncryptor encryptor;

	@Test
	void jasyptEncryption() {

		String value = "test_value";
		String encValue = encryptor.encrypt(value);
		String decValue = encryptor.decrypt(encValue);

		log.info("Value:: {}", value);
		log.info("EncValue:: {}", encValue);
		log.info("DecValue:: {}", decValue);

	}

}

 

'[Java] > [Spring]' 카테고리의 다른 글

[SpringBoot] Database Logging 설정  (0) 2024.08.27
[SpringBoot] Logback 설정  (0) 2024.08.27
[SpringBoot] Thymeleaf 설정  (0) 2024.08.26
[SpringBoot] MyBatis 설정  (0) 2024.08.26
[SpringBoot] Database Connetion 설정  (0) 2024.08.26