Spring

[SpringBoot] yml, propertise μ„€μ • κ°’ μ•”ν˜Έν™” (Jasypt)

λΉ…μ½œνŒ 2023. 8. 20. 14:23
728x90
λ°˜μ‘ν˜•

πŸ”’ Jasypt(Java Simplified Encryption)


- κ°œλ°œμžκ°€ μ•”ν˜Έν™” μž‘λ™ 방식에 λŒ€ν•œ κΉŠμ€ 지식이 없어도 μ΅œμ†Œν•œμ˜ λ…Έλ ₯으둜 ν”„λ‘œμ νŠΈμ— κΈ°λ³Έ μ•”ν˜Έν™” κΈ°λŠ₯을 μΆ”κ°€ν•  수 μžˆλŠ” μžλ°” 라이브러리
- ν”„λ‘œνΌν‹°λ‘œ κ΄€λ¦¬ν•˜λŠ” DB 계정 정보와 같은 μ„€μ • 값을 평문이 μ•„λ‹Œ μ•”ν˜Έλ¬ΈμœΌλ‘œ 관리
 

🐑 Jasypt Spring Stater λ™μž‘ 방식


@SpringBootApplication

 
- μ• ν”Œλ¦¬μΌ€μ΄μ…˜ ꡬ동 λ‹¨κ³„μ—μ„œ ENC(μ•”ν˜Έν™” 된 κ°’) ν˜•μ‹μ˜ 속성을 μ°Ύμ•„ λ³΅ν˜Έν™” μˆ˜ν–‰ ν›„ λ³΅ν˜Έν™” 된 κ°’μœΌλ‘œ μ›λž˜μ˜ μ•”ν˜Έν™”λœ 속성 κ°’ λŒ€μ²΄
 

🧩 Jasypt μ£Όμš” λ©”μ†Œλ“œ


KeyRequiredDefault Value
jasypt.encryptor.passwordTrue-
jasypt.encryptor.algorithmFalsePBEWITHHMACSHA512ANDAES_256
jasypt.encryptor.key-obtention-iterationsFalse1000
jasypt.encryptor.pool-sizeFalse1
jasypt.encryptor.provider-nameFalseSunJCE
jasypt.encryptor.provider-class-nameFalsenull
jasypt.encryptor.salt-generator-classnameFalseorg.jasypt.salt.RandomSaltGenerator
jasypt.encryptor.iv-generator-classnameFalseorg.jasypt.iv.RandomIvGenerator
jasypt.encryptor.string-output-typeFalsebase64
jasypt.encryptor.proxy-property-sourcesFalsefalse
jasypt.encryptor.skip-property-sourcesFalseempty 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

728x90
λ°˜μ‘ν˜•