본문 바로가기

Spring/Lib

Spring Boot-Jasypt

반응형

JasyptConfig.java

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
public class JasyptConfig {
    private static final String KEY = "사용할 키값";
    private static final String ALGORITHM = "PBEWithMD5AndDES";

    @Bean("jasyptStringEncryptor")
    public StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        config.setPassword(KEY); // 암호화할 때 사용하는 키
        config.setAlgorithm(ALGORITHM); // 암호화 알고리즘
        config.setKeyObtentionIterations("1000"); // 반복할 해싱 회수
        config.setPoolSize("1"); // 인스턴스 pool
        config.setProviderName("SunJCE"); // 프로바이더
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator"); // salt 생성 클래스 지정
        config.setStringOutputType("base64"); // 인코딩
        encryptor.setConfig(config); // 설정 정보 Set
        return encryptor;
    }
}

 

application.yml

spring:
  datasource:
    url: ENC(Database URL 암호화 데이터)
    username: ENC(Database UserName 암호화 데이터)
    password: ENC(Database Password 암호화 데이터)
    driver-class-name: com.mysql.cj.jdbc.Driver

 

build.gradle

implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.5' // Jasypt를 사용한 암호화 기능을 제공하는 의존성

 

 

3가지만 입력 하면 완료

 

 

암호화 출력 방법

public static void EncPrint() {
    // 원본 DB 연결 정보
    String dburl = "jdbc:mysql://localhost:3306/DbName?serverTimezone=Asia/Seoul&useSSL=false";
    String dbuser = "user";
    String dbpasswd = "password";

    // Jasypt 라이브러리의 StandardPBEStringEncryptor 인스턴스 생성
    StandardPBEStringEncryptor pbeEnc = new StandardPBEStringEncryptor();
    pbeEnc.setAlgorithm("PBEWithMD5AndDES"); // 암호화 알고리즘 설정
    pbeEnc.setPassword("Password"); // JasyptConfig에 설정된 암호화 키를 입력

    // DB 연결 정보를 암호화
    String enc_dburl = pbeEnc.encrypt(dburl);
    String enc_dbuser = pbeEnc.encrypt(dbuser);
    String enc_dbpasswd = pbeEnc.encrypt(dbpasswd);

    // 암호화된 정보 출력
    System.out.println("Encrypted DBUrl = " + enc_dburl); // 암호화된 DBUrl 출력
    System.out.println("Encrypted DBUser = " + enc_dbuser); // 암호화된 DBUser 출력
    System.out.println("Encrypted DBPassword = " + enc_dbpasswd); // 암호화된 DBPassword 출력
}

'Spring > Lib' 카테고리의 다른 글

Spring Boot - JPA  (0) 2023.08.08