[SpringBoot] yml, propertise μ€μ κ° μνΈν (Jasypt)
π Jasypt(Java Simplified Encryption)
- κ°λ°μκ° μνΈν μλ λ°©μμ λν κΉμ μ§μμ΄ μμ΄λ μ΅μνμ λ
Έλ ₯μΌλ‘ νλ‘μ νΈμ κΈ°λ³Έ μνΈν κΈ°λ₯μ μΆκ°ν μ μλ μλ° λΌμ΄λΈλ¬λ¦¬
- νλ‘νΌν°λ‘ κ΄λ¦¬νλ DB κ³μ μ 보μ κ°μ μ€μ κ°μ νλ¬Έμ΄ μλ μνΈλ¬ΈμΌλ‘ κ΄λ¦¬
π‘ Jasypt Spring Stater λμ λ°©μ
@SpringBootApplication
- μ ν리μΌμ΄μ
ꡬλ λ¨κ³μμ ENC(μνΈν λ κ°) νμμ μμ±μ μ°Ύμ 볡νΈν μν ν 볡νΈν λ κ°μΌλ‘ μλμ μνΈνλ μμ± κ° λ체
π§© Jasypt μ£Όμ λ©μλ
Key | Required | Default Value |
jasypt.encryptor.password | True | - |
jasypt.encryptor.algorithm | False | PBEWITHHMACSHA512ANDAES_256 |
jasypt.encryptor.key-obtention-iterations | False | 1000 |
jasypt.encryptor.pool-size | False | 1 |
jasypt.encryptor.provider-name | False | SunJCE |
jasypt.encryptor.provider-class-name | False | null |
jasypt.encryptor.salt-generator-classname | False | org.jasypt.salt.RandomSaltGenerator |
jasypt.encryptor.iv-generator-classname | False | org.jasypt.iv.RandomIvGenerator |
jasypt.encryptor.string-output-type | False | base64 |
jasypt.encryptor.proxy-property-sources | False | false |
jasypt.encryptor.skip-property-sources | False | empty list |
λλ€ μνΈ μμ±κΈ° - λλ€ μνΈ μμ±κΈ°λ₯Ό μ¬μ©νλ―λ‘ λμΌν λ©μμ§μ λν λκ°μ μνΈν κ²°κ³Όκ° λ€λ¦
무μμ IV μμ±κΈ° - IVλ 무μμμ¬μΌ νκ³ ν λ²λ§ μ¬μ©ν΄μΌ νλ―λ‘ org.jasypt.RandomIvGeneratorκ° κΆμ₯ λ¨.
π¨ λνμ μΈ Jasypt μνΈν μκ³ λ¦¬μ¦
PBEWithMD5AndDES
- MD5 ν΄μ ν¨μμ DES λμΉν€ μνΈνλ₯Ό μ‘°ν©νμ¬ λ°μ΄ν° μνΈν
- λΉ λ₯Έ μ볡νΈν μλλ₯Ό κ°μ§μ§λ§ μλμ μΌλ‘ 보μμ μ·¨μ½
PBES2WithHmacSHA512AndAES_256 (default)
- HmacSHA512 : Hmacμ μ¬μ©νμ¬ SHA-512 ν΄μ μκ³ λ¦¬μ¦ κ΅¬ν
- HmacSHA512λ‘ λΆν° μμ±λ ν€λ₯Ό μ¬μ©νμ¬ λ°μ΄ν°λ₯Ό AES-256 μκ³ λ¦¬μ¦μ μ΄μ©νμ¬ μνΈν
πββοΈ μ¬μ© μμ
build.gradle μμ‘΄μ± μΆκ°
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5'
application.yml
jasypt:
encryptor:
key: ${jasypt-key}
JasyptConfig.java
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JasyptConfig {
@Value("${jasypt.encryptor.key}")
private String key;
@Bean(name = "jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(key); // μνΈν ν€
config.setAlgorithm("PBEWithHMACSHA512AndAES_256"); // μνΈν μκ³ λ¦¬μ¦
config.setIvGenerator(new RandomIvGenerator()); // PBE-AES κΈ°λ° μκ³ λ¦¬μ¦μ κ²½μ° IV μμ± νμ
config.setKeyObtentionIterations("1000"); // λ°λ³΅ν ν΄μ± νμ
config.setPoolSize("1"); // μΈμ€ν΄μ€ pool
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); // salt μμ± ν΄λμ€
config.setStringOutputType("base64"); // μΈμ½λ©
encryptor.setConfig(config);
return encryptor;
}
}
- application.ymlμ jasypt.encryptor.key κ°μ΄ λ
ΈμΆλλ©΄ μλλ―λ‘ νκ²½ λ³μλ‘ ν λΉ

class JasyptConfigTest {
private static final String SECRET_KEY = "my_secret_key";
@Test
void string_encryption() {
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(SECRET_KEY);
config.setAlgorithm("PBEWithHMACSHA512AndAES_256");
config.setIvGenerator(new RandomIvGenerator());
config.setKeyObtentionIterations("1000");
config.setPoolSize("1");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setStringOutputType("base64");
encryptor.setConfig(config);
String originalString = "my_password";
// μνΈν
String encryptedString = encryptor.encrypt(originalString);
System.out.println("Encrypted String ::: ENC(" + encryptedString + ")");
// 볡νΈν
String decryptedString = encryptor.decrypt(encryptedString);
System.out.println("Decrypted String ::: " + decryptedString);
assertEquals(originalString, decryptedString);
}
}
ν
μ€νΈ μ½λλ₯Ό μ€νν΄ λμ¨ enc κ°μΌλ‘ λ체
π§ reference
https://github.com/ulisesbocchio/jasypt-spring-boot#use-you-own-custom-encryptor
http://www.jasypt.org/encrypting-texts.html