Preloader image
DDD

자바

Spring Boot - db connection - ENC 암호화

작성자 관리자 (admin)
조회수 454
입력일 2023-01-31 21:54:16

-- application.properties

# db connection

spring.datasource.driver-class-name=net.sf.log4jdbc.sql.jdbcapi.DriverSpy
spring.datasource.url=ENC(IXHgw6XudfVT2xS4/zIsCO+m9TrZFoRdc9gt74kiyzG7WlGA3T1PAL/hsXJ1dbUn8vrX0nr3rmbzQ0lSMP94D3+1Yj/zYwuXi1melIyfM/SpdjkQJpG03QvTCp7hSTJ3Cdmm99VT98mL6o2oqT2IcIxF462lW4NN)
spring.datasource.username=ENC(6yZN6kLqpqrmsupriDmz5w==)
spring.datasource.password=ENC(oVS8VkfR0xzmLaqNBOME+QX3D/zG0DqD)

위와 같은 암호화 방법 (Spring Boot 2.7.5)

-- JasyptApplication.java (암호화 결과 파일 생성)

package com.blz.dia.cmm.function;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

public class JasyptApplication {

  public static void main(String[] args) {

    String url = "jdbc:log4jdbc:mysql://digital2u.co.kr:3306/user_db?useUnicode=true&characterEncoding=utf8&autoReconnect=true";
    String username = "your_user";
    String password = "your_pass";

    StandardPBEStringEncryptor pbeEnc = new StandardPBEStringEncryptor();
    pbeEnc.setAlgorithm("PBEWithMD5AndDES"); // 알고리즘
    pbeEnc.setPassword("0123456789"); // 암호화 키

    System.out.println("URL  : " + pbeEnc.encrypt(url)     );
    System.out.println("USER : " + pbeEnc.encrypt(username));
    System.out.println("PASS : " + pbeEnc.encrypt(password));
  }
}

-- JasyptConfigDES.java (Bean설정 - 실제 설정에 관여되는 프로그램)

package com.blz.dia.cmm.function;

import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableEncryptableProperties

public class JasyptConfigDES {

  @Bean("jasyptStringEncryptor")
  public StringEncryptor stringEncryptor() {
    PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
    SimpleStringPBEConfig config = new SimpleStringPBEConfig();
    config.setPassword("0123456789"); // 암호화 키
    config.setAlgorithm("PBEWithMD5AndDES"); // 알고리즘
    config.setKeyObtentionIterations("1000");
    config.setPoolSize("1");
    config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
    config.setStringOutputType("base64");
    encryptor.setConfig(config);
    return encryptor;
  }
}

^