Bibi's DevLog ๐Ÿค“๐ŸŽ

[Spring] Jasypt - ์ž๋ฐ” ์ฝ”๋“œ ์•”ํ˜ธํ™” (์‹œํฌ๋ฆฟ ํ‚ค ์•”ํ˜ธํ™”ํ•˜๊ธฐ) ๋ณธ๋ฌธ

๐Ÿ–ฅ BE ๋ฐฑ์—”๋“œ/Spring ์Šคํ”„๋ง

[Spring] Jasypt - ์ž๋ฐ” ์ฝ”๋“œ ์•”ํ˜ธํ™” (์‹œํฌ๋ฆฟ ํ‚ค ์•”ํ˜ธํ™”ํ•˜๊ธฐ)

๋น„๋น„ bibi 2021. 6. 11. 00:15

jasypt (์ž๋ฐ” ํ‚ค ์•”ํ˜ธํ™” ๋ฐฉ์‹)

์ถœ์ฒ˜ : ์šฐ๋””๊ฐ€ ๊ณต์œ ํ•ด์ฃผ์‹  ํ•„๊ธฐ๋…ธํŠธ๐Ÿ™‡โ€โ™‚๏ธ

Jasypt

  • Java Simplified Encryption
  • ์ž๋ฐ” ์ฝ”๋“œ ์•”ํ˜ธํ™” ์„œ๋น„์Šค ์ œ๊ณต
  • DB ํŒจ์Šค์›Œ๋“œ, OAuth Client Secret ๋“ฑ ๋ฏผ๊ฐ์ •๋ณด๋ฅผ ์•”ํ˜ธํ™”ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

Jasypt ์‚ฌ์šฉํ•˜๊ธฐ

1. ์˜์กด์„ฑ ์ถ”๊ฐ€

build.gradle ์— ์•„๋ž˜ ์ฝ”๋“œ ์ถ”๊ฐ€

compile 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.3'

2. ๋น„๋ฐ€๋ฒˆํ˜ธ ์•”ํ˜ธํ™”

https://www.devglan.com/online-tools/jasypt-online-encryption-decryption ์—์„œ ์•”ํ˜ธํ™”ํ•˜๊ณ  ์‹ถ์€ ์ฝ”๋“œ์˜ ์•”ํ˜ธํ™”๋ฅผ ์ง„ํ–‰

[Jasypt Online Encryption] ์˜์—ญ์œผ๋กœ ์Šคํฌ๋กค ํ›„

  • Enter plain text to Encrypt : ์•”ํ˜ธํ™”ํ•  ์ฝ”๋“œ ์ž…๋ ฅ
  • Select Type of Encryption : ์•”ํ˜ธํ™”ํ•  ๋ฐฉ์‹ ์„ ํƒ - Two way๋กœ ์„ ํƒ. ๋””ํดํŠธ ์•”ํ˜ธํ™” ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ PBEWithMD5AndDES์ด๋‹ค.
  • Secret Key to Be Used While ... : ์•”ํ˜ธํ™”์— ์‚ฌ์šฉํ•  ์‹œํฌ๋ฆฟ ํ‚ค ์ž…๋ ฅ. (์ž„์˜์˜ ๋ฌธ์ž์—ด ๋˜๋Š” ์˜๋ฏธ์žˆ๋Š” ๋‹จ์–ด. ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฐ™์€ ๊ฐœ๋…)
  • [Encrypt]๋ฅผ ๋ˆ„๋ฅด๋ฉด ์•”ํ˜ธํ™”๊ฐ€ ์™„๋ฃŒ๋œ ๋ฌธ์ž์—ด์ด ์•„๋ž˜ Encrypted String์— ์ถœ๋ ฅ๋œ๋‹ค.

3. ๋ฏผ๊ฐ์ •๋ณด๋ฅผ ์•”ํ˜ธํ™”๋œ ์ฝ”๋“œ๋กœ ๋ณ€๊ฒฝ

์•”ํ˜ธํ™”ํ•˜๊ธฐ ์ „ ์ฝ”๋“œ๋ฅผ ENC(์•”ํ˜ธํ™”ํ•œ ์ฝ”๋“œ)๋กœ ๋กœ ์น˜ํ™˜ํ•œ ๋’ค, 2.์—์„œ ์ž…๋ ฅํ•œ ์‹œํฌ๋ฆฟ ํ‚ค๋ฅผ ์„ค์ •ํ•ด ์ค€๋‹ค.

application.properties

github.client.secrets=ENC(OQLcoaTQsAdvyxldhkju7xBUa2c8cpTgIKSgM8SipRRMsr7cLHnrwu36AKEZTgTSoE014OG84eo=)
jasypt.encryptor.password=${JASYPT_PASSWORD}
  • ${JASYPT_PASSWORD} : ์•”ํ˜ธํ™”์— ์‚ฌ์šฉํ•œ ์‹œํฌ๋ฆฟํ‚ค๋ฅผ ์ธํ…”๋ฆฌ์ œ์ด ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ ๋“ฑ๋ก (์ฐธ๊ณ )

4. Jasypt Configuration ํด๋ž˜์Šค ์ถ”๊ฐ€ ๋ฐ Bean ์„ค์ •

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;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource("application.properties")
public class JasyptConfig {

    @Value("${jasypt.encryptor.password}") // ์‹œํฌ๋ฆฟ ํ‚ค 
    private String encryptKey;

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(encryptKey);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }

}

Two Way Encryption(PBEWithMD5AndDES)์œผ๋กœ ์•”ํ˜ธํ™”๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ํด๋ž˜์Šค.

  • ${jasypt.encryptor.password}๋กœ ๊ฐ€์ ธ์˜จ enctryptKey ๋ณ€์ˆ˜(=์‹œํฌ๋ฆฟํ‚ค)๋ฅผ ๊ฐ€์ ธ์˜ด
  • setPassword()์˜ ์ธ์ž๋กœ ์‹œํฌ๋ฆฟํ‚ค๋ฅผ ์„ค์ •
  • setAlgorithm("PBEWithMD5AndDES") ์œผ๋กœ ์•”ํ˜ธํ™” ํƒ€์ž… ์„ค์ •

์œ„ ํด๋ž˜์Šค๋ฅผ ์ถ”๊ฐ€ ํ›„, ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ application.properties์— ์ถ”๊ฐ€ํ•ด JasyptStringEncryptor๋ฅผ Bean์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค.

  • application.properties

    #jaspyt config
    jasypt.encryptor.bean=jasyptStringEncryptor

5. @EnableEncryptableProperties ์ถ”๊ฐ€

ํ”„๋กœ์ ํŠธ mainํด๋ž˜์Šค์— @EnableEncryptableProperties ์–ด๋…ธํ…Œ์ด์…˜ ์ถ”๊ฐ€

@SpringBootApplication
@EnableEncryptableProperties
public class AirbnbApplication {
    public static void main(String[] args) {
        SpringApplication.run(AirbnbApplication.class, args);
    }
}