๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Back-end/JAVA & Spring

[ Spring Boot ] Spring Boot์—์„œ Datasource ์•”ํ˜ธํ™”ํ•˜๊ธฐ

 

 

 

 

๐ŸŒฑ Spring Boot - Datasource ์•”ํ˜ธํ™”

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ํฌํ•จํ•œ spring.datasource.url, spring.datasource.username, spring.datasource.password๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ•ต์‹ฌ ๋ฏผ๊ฐ ์ •๋ณด ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ด ์ •๋ณด๊ฐ€ ์™ธ๋ถ€๋กœ ๋…ธ์ถœ๋˜๋ฉด ๋ณด์•ˆ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, ์ด๋ฅผ ์•”ํ˜ธํ™”ํ•˜์—ฌ ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์ค‘์š”ํ•˜๋‹ค. 

Spring Boot์™€ Jasypt๋ฅผ ํ™œ์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ฒฐ ์ •๋ณด๋ฅผ ์•”ํ˜ธํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์†Œ๊ฐœํ•  ์˜ˆ์ •์ด๋‹ค.

 

 

 

Jasypt

Jasypt(Java Simplified Encryption)๋ž€ Java ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฏผ๊ฐํ•œ ์ •๋ณด๋ฅผ ์‰ฝ๊ฒŒ ์•”ํ˜ธํ™”ํ•˜๊ณ  ๋ณตํ˜ธํ™”ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ด๋‹ค. Spring Boot์™€ ํ†ตํ•ฉํ•˜๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ • ํŒŒ์ผ์— ํฌํ•จ๋œ ์•”ํ˜ธํ™”๋œ ๊ฐ’์„ ์ž๋™์œผ๋กœ ๋ณตํ˜ธํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

Jasypt ์„ค์ •ํ•˜๊ธฐ

 

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

build.gradle ํŒŒ์ผ์— Jasypt ์˜์กด์„ฑ ์ถ”๊ฐ€

 

dependencies {
    implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5'
}

 

 

 

 

Jasypt Config ์ž‘์„ฑ

 

@Configuration
@Slf4j
public class JasyptConfig {

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {

        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();

        // ์•”ํ˜ธํ™” ํ‚ค ์„ค์ •: ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋˜๋Š” ์‹œ์Šคํ…œ ์†์„ฑ์—์„œ ์ฝ์Œ
        String encryptionKey = System.getenv("JASYPT_ENCRYPTOR_PASSWORD");
        if ( StringUtils.isBlank(encryptionKey) ) {
            throw new IllegalStateException("JASYPT_ENCRYPTOR_PASSWORD ํ™˜๊ฒฝ๋ณ€์ˆ˜๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค.");
        }

        // Jasypt ์•”ํ˜ธํ™” ๊ธฐ๋ณธ ์„ค์ •
        config.setPassword(encryptionKey);
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");

        encryptor.setConfig(config);
        log.info("Jasypt StringEncryptor๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์„ค์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.");

        return encryptor;
    }
}

 

 

 

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ •๋ณด ์•”ํ˜ธํ™”

1. Java ํ”„๋กœ๊ทธ๋žจ ํ™œ์šฉํ•˜์—ฌ ์•”ํ˜ธํ™” ๊ฐ’ ์ƒ์„ฑ

 

public class EncryptionUtil {
    public static void main(String[] args) {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        encryptor.setPassword("your-secret-key");
        encryptor.setAlgorithm("PBEWithMD5AndDES");

        String url = encryptor.encrypt("jdbc:mysql://dbIp:dbProt/yourdb");
        String username = encryptor.encrypt("username");
        String password = encryptor.encrypt("password");

        System.out.println("Encrypted URL: ENC(" + url + ")");
        System.out.println("Encrypted Username: ENC(" + username + ")");
        System.out.println("Encrypted Password: ENC(" + password + ")");
    }
}

 

 

 

2. ์‚ฌ์ดํŠธ ํ™œ์šฉํ•˜์—ฌ ์•”ํ˜ธํ™” ๊ฐ’ ์ƒ์„ฑ 

 

https://www.devglan.com/online-tools/jasypt-online-encryption-decryption

 

Jasypt Encryption and Decryption Online

Jasypt online free tool for encryption and decryption.This tool supports one way and two way password encryptor using Jasypt as well as matching encrypted password using Jasypt.

www.devglan.com

 

 

 

 

application.properties ํŒŒ์ผ ์„ค์ •

 

spring.datasource.url=ENC(your-encrypted-url)
spring.datasource.username=ENC(your-encrypted-username)
spring.datasource.password=ENC(your-encrypted-password)

 

 

 

 

ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •

์•”ํ˜ธํ™” ํ‚ค๋Š” ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ๋˜๋Š” ์‹œ์Šคํ…œ ์†์„ฑ์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค. 

 

1. ํ†ฐ์บฃ ๋ฐฐํฌ์‹œ (Windows/Linux ๋“ฑ OS ํ™˜๊ฒฝ์—์„œ ์„ค์ •)

1.1 Windows์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •

๊ณ ๊ธ‰ ์‹œ์Šคํ…œ ์„ค์ •์—์„œ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ cmd ์ฐฝ์—์„œ ์•„๋ž˜ ๋ช…๋ น์–ด ์‚ฌ์šฉ

 

set JASYPT_ENCRYPTOR_PASSWORD=your-secret-key

 

 

 

1.2 Linux์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์„ค์ •

Bash Profile ์„ค์ •(์˜ˆ: /etc/profile, ~/.bashrc)

 

export JASYPT_ENCRYPTOR_PASSWORD=your-secret-key

 

 

 

 

2. VSCode์—์„œ Spring Boot ์‹คํ–‰์‹œ 

VSCode์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด launch.json ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ํ„ฐ๋ฏธ๋„์—์„œ ์ง์ ‘ ์„ค์ •ํ•œ๋‹ค. 

2.1 launch.json ํŒŒ์ผ์— ์„ค์ •

Run and Debug ํƒญ์—์„œ create a launch.json file ํด๋ฆญ

 

 

 

launch.json

 

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "java",
      "name": "Spring Boot",
      "request": "launch",
      "mainClass": "com.example.Application",
      "env": {
        "JASYPT_ENCRYPTOR_PASSWORD": "your-secret-key"
      }
    }
  ]
}

 

 

 

 

3. Spring Boot Suite, eclipse ์—์„œ ์‹คํ–‰์‹œ 

์‹คํ–‰ ์„ค์ •์—์„œ ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ถ”๊ฐ€ 

3.1 Run Configurations ์„ค์ •

1. Run → Run Configurations ์ด๋™

2. Environment ํƒญ ํด๋ฆญ 

3. ์ƒˆ ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ถ”๊ฐ€

 

 

 

 

์‹คํ–‰ ๋ฐ ํ…Œ์ŠคํŠธ

๋กœ๊ทธ ํ™•์ธ

 

INFO  [main] Jasypt StringEncryptor๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์„ค์ •๋˜์—ˆ์Šต๋‹ˆ๋‹ค.