[SpringConfig] Properties 암호화

JAVA/Spring|2019. 12. 24. 19:15

현재 토이 프로젝트에 Spring Cloud Config Server를 덕분에 설정을 편하게 사용하고 있습니다.

(config repo는 github를 사용하고 있는데 잘 되네요, 내역 관리도 되고 github 들어가서 수정하기도 편하니)

 

그런데 설정중에 암호에 대한 설정들이 있습니다.

 

현재는 혼자서 개발중이기 때문에 문제 없지만 공용 repo에 암호를 그대로 노출한다는게 조금 찝찝함은 있습니다.

 

그래서 미뤄놓았던것중 하나인 설정 암호화를 적용하려고 합니다.

 

설정 암호화는

https://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

에 올라와있는 jasypt-spring-boot를 사용하여 처리하려고 합니다.

 

간단히 pom.xml에 해당 의존성을 추가하고

(최신버전이 3.0.0이네요)

<dependency>
      <groupId>com.github.ulisesbocchio</groupId>
      <artifactId>jasypt-spring-boot-starter</artifactId>
      <version>3.0.0</version>
    </dependency>

 

properties에 암호화 키를

jasypt.encryptor.password= xxx 로 설정합니다.

 

그후에 StringEncryptor로 

 

    @Autowired
    StringEncryptor encryptor;

    @Test
    public void test(){

        String defaultStr = "aa131";

        String encrytStr = encryptor.encrypt(defaultStr);

        System.out.println("encrytStr : " + encrytStr);

        String decrytStr = encryptor.decrypt(encrytStr);
        System.out.println("decrytStr : " + decrytStr);

//        System.out.println("encrypt : " + encryptor.encrypt("a"));

    }

와 같이 암호화된 패스워드를 구합니다.

(org.jasypt.exceptions.EncryptionOperationNotPossibleException 와 같은 에러가 발생할 수 있는데, 검색해보시면 oracle에서 JCE 파일을 받아서 해결하는 방법을 찾을수 있습니다.)

암호화된 패스워드를 ENC() 로 감싸서 설정합니다.

 

spring.data.mongodb.password=ENC(xxxxxx)
jasypt.encryptor.password= xxx

설정을 변경후 기존 서버를 부팅시켜보면 정상적으로 동작하는것을 볼 수 있는데

 

같은 properties 파일에 암호키랑 암호화된 정보를 둘다 같고 있는건 조금 아니다 싶습니다.

 

그래서 어떻게 분리 시킬까 하다

저는 bootstrap.yml 에 

jasypt.encryptor.password 설정을 분리하고

properties에는 암호화된 값(ENC) 만 남겨 분리 했습니다.

jasypt:
  encryptor:
    password: xxxxxx

 

 

 

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

[Spring] LifeCycle, SmartLifeCycle  (0) 2020.06.23
[MSA] sidecar 패턴  (0) 2020.01.13
spring boot log level 변경(actuator)  (0) 2019.12.19
reactor Schedulers  (0) 2019.12.16
스프링 부트 2.2 릴리즈노트  (0) 2019.11.01

댓글()