Properties 암호화(feat.시스템 환경 변수)

박상훈·2023년 3월 9일
0

아래 내용은 실제로 적용 및 정상 작동을 확인하고 작성하였습니다.
같은 환경에서 테스트하신다면 동일한 결과를 예상합니다.

 

  • Version
    • OS → Windows 11
    • Java → 11
    • Spring Boot → 2.7.1
    • jasypt → 3.0.4

 

  • 의존성 추가
    • 주의) 블로그에 Spring Boot, jasypt 버전 호환 이슈 글이 종종 보임
<dependency>
    <groupId>com.github.ulisesbocchio</groupId>
    <artifactId>jasypt-spring-boot-starter</artifactId>
    <version>3.0.4</version>
</dependency>

 

  • jasypt configuration class
    • jasypt default
      • property.prefix = ENC(
      • property.suffix = )
@Configuration
@EnableEncryptableProperties
public class JasyptConfig {
    private static final String PASSWORD = "시스템 환경 변수 Key";

    @Bean("jasyptStringEncryptor")
    StringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        final String PASSWORD = System.getenv(PASSWORD);
        config.setPassword(PASSWORD);
        config.setAlgorithm("PBEWithMD5AndDES");
        // 반복 해싱 횟수
        config.setKeyObtentionIterations(1000);
        config.setPoolSize(1);
        // JVM 보안 프레임워크에 사전 등록된 공급자 명
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
        return encryptor;
    }
}

 

  • jasypt password 운영체제 환경 변수
    • Encryptor Configuration 에 적용할 password
    • 암호화에 사용되는 키
    • 시스템 환경 변수에 Key, Value 설정
System.getenv("시스템 환경 변수 Key");

 

  • Jasypt Encryption and Decryption
    • jasypt configuration class(java) file 사용
      • encryptor.encrypt(“plain text”) 함수 호출
      • 리턴 값의 암호화 문자열 properties 파일에 적용
    • Online URL(Free)
      • Generate and Check Jasypt Encrypted Passwords Online
      • Select Type of Encryption → Two Way Encryption 선택
      • Enter Secret Key → jasypt password 입력

 

  • jasypt 적용
    • application.properties에서 사용 중인 데이터베이스 비밀번호에 적용 하는 예시
spring.datasource.admin.password=ENC(3KJes1I8ioc59POyS/t6rzC+Ua4PzUCN)
profile
엔지니어

0개의 댓글