본문 바로가기

Spring

SpringBoot @EnableEncryptableProperties 스프링에서 암호화를 써보자

728x90
반응형

설정파일에 민감한 정보들은 암호화를 해줘야 한다.

(DB비밀번호, URL 같은 것들..)

암호화는 대세를 따라서 jasypt(Java Simplified Encryption)를 썼다.

// https://mvnrepository.com/artifact/com.github.ulisesbocchio/jasypt-spring-boot-starter
compile group: 'com.github.ulisesbocchio', name: 'jasypt-spring-boot-starter', version: '3.0.3'
// https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk15to18
compile group: 'org.bouncycastle', name: 'bcprov-jdk15to18', version: '1.68'

 

BouncyCastle

확장된 기능을 가진 자바 암호화 라이브러리

꽤나 많은 알고리즘을 제공해준다고 한다.

 

github.com/ulisesbocchio/jasypt-spring-boot

 

ulisesbocchio/jasypt-spring-boot

Jasypt integration for Spring boot. Contribute to ulisesbocchio/jasypt-spring-boot development by creating an account on GitHub.

github.com


@Configuration
@EnableEncryptableProperties
public class PropertyEncryptConfig {

    @Bean("encryptBean")
    public PooledPBEStringEncryptor stringEncryptor() {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setProvider(new BouncyCastlePQCProvider());
        encryptor.setPoolSize(2);
        encryptor.setPassword("Password");
        encryptor.setAlgorithm("PBEWithMD5AndDES");
        return encryptor;
    }

    public static void main(String[] args) {
        PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
        encryptor.setProvider(new BouncyCastleProvider());
        encryptor.setPoolSize(2);
        encryptor.setPassword("Password");
        encryptor.setAlgorithm("PBEWithMD5AndDES");

        String plainText = "test";
        String encryptedText = encryptor.encrypt(plainText);
        String decryptedText = encryptor.decrypt(encryptedText);
        System.out.println("Enc:"+encryptedText+", Dec:"+decryptedText);
    }
}

@EnableEncryptableProperties

SpringBoot 암호화할 빈을 생성해줄 class에 어노테이션을 추가한다.

 

PooledPBEStringEncryptor 

 

Pooled implementation of PBEStringEncryptor that in fact contains an array of StandardPBEStringEncryptor objects which are used to attend encrypt and decrypt requests in round-robin. This should result in higher performance in multiprocessor systems.

라운드로빈 방식으로 PBEStringEncryptor를 풀링 구현한다.

Poolsize 는 보통 1이나 2로 하던데..

 

 

PBEWithMD5AndDES

패스워드를 기반한 암호화 방식.

Password는 키로 쓸 값이기 때문에 내가 직접 입력해줘야 한다.

 

Enc:R6bRf8dQPARV5175N84L6Q==, Dec:test

돌려보면 암호화된 값이 나온다.

 

jasypt:
  encryptor:
    bean: encryptorBean
    
password: ENC(R6bRf8dQPARV5175N84L6Q==)

yml파일에서는 위와 같이 사용한다.

 

728x90
반응형