7월 초 프로젝트 점검 진행 후 보안을 진행하면서 작업하게 된 작업입니다.
프로젝트 내에 데이터 베이스를 만들기 위해 yml 파일에 데이터베이스 정보를 넣어둔 상태이다.
혹시 모를 상황이 존재하기에 yml의 정보 암호화를 진행 하기로 했다!
우선 Jsypt는 무엇인가?
Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.
개발자가 깊은 지식이 없더라도 최소한의 노력으로 프로젝트에 기본적인 암호화 능력을 쥐어주는 자바 라이브러리입니다.
- 특히 Spring Boot에서 properties/yml 을 암호화하여 @Bean을 사용하여 암호화된 properties 값을 자동으로 복호화 합니다.
사랑해요 스프링 부트
Jsypt : http://www.jasypt.org/
SpringBoot Starter : https://github.com/ulisesbocchio/jasypt-spring-boot
해당 프로젝트는 SpringBoot & 메이븐으로 진행하였습니다.
예시의 yml 입니다.
spring:
datasource:
url: jdbc:mysql://localhost:8080/
username: jdbc_username
password: jdbc_password
Maven 추가
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
Config 생성
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class JasyptConfig {
@Value("${jasypt.encryptor.password}")
private String PASSWORD;
@Bean("jasyptStringEncryptor")
public StringEncryptor stringEncryptor(){
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(PASSWORD);
config.setPoolSize("1");
config.setAlgorithm("PBEWithMD5AndDES");
config.setStringOutputType("base64");
config.setKeyObtentionIterations("1000");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
encryptor.setConfig(config);
return encryptor;
}
}
application.yml 수정
우리가 만들어준 Config를 Bean으로 설정해주자
spring:
datasource:
url: jdbc:mysql://localhost:8080
username: jdbc_username
password: jdbc_password
jasypt:
encryptor:
bean: jasyptStringEncryptor
password : winter13(^
이제 우리가 암호화를 하고 싶었던
jdbc_username, jdbc_password 를 암호화 해보자
테스트 코드로 진행 할 수 있습니다. (이게 맞습니당)
하지만 우선 저는 아래의 사이트를 통해서 암호화를 진행하였습니다.
https://www.devglan.com/online-tools/jasypt-online-encryption-decryption
Programming Blog Article Feeds as per your Interest | DevGlan
Best programming article feeds as per your Interest on different technologies. Subscribe to any technology and explore the best articles from around the web.
www.devglan.com
암호화 값을 작성 해주자
spring:
datasource:
url: jdbc:mysql://localhost:8080
username: UZ5xZLnoHjFdQKNer0q/ys/0W/OkRcV+
password: yKXBC6N/478NjoOwbqaQAIAXPyiHU2x3
jasypt:
encryptor:
bean: jasyptStringEncryptor
password : winter13(^
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/007.gif)
하지만 지금 우리 눈에 신경 쓰이는 아이가 있다...
위 datasource는 이쁘게 암호화가 진행 되었지만
아래의 jasypt의 password가 너무 신경 쓰인다...
해당 password를 숨겨보자!
IntelliJ의 VM 설정으로 이동한다.
VM option 안에 -Djasypt.encryptor.password=winter13(^ 을 입력해주자
이후 yml의 password를 지워주고 실행한다!
위의 암호화를 진행하고 있는 프로젝트는 외부 톰캣으로 진행되고 있는 프로젝트다!
로컬 단계에서는 내부 톰캣으로 진행 (VM에 -Djasypt.encryptor.password=winter13(^ ) 추가
검증, 상용 서버는 외부 톰캣이다.
- catalina.properties로 접근
tomcat folder/conf/catalina.properties
jasypt.encryptor.password=winter13(^ 를 추가해주자
이후 톰캣 shutdown -> start
잘 작동한다!
참고 자료
https://velog.io/@haeny01/Jasypt-yaml-%ED%8C%8C%EC%9D%BC%EC%9D%98-%EC%95%94%ED%98%B8%ED%99%94
[Jasypt] yaml 파일의 암호화
SpringBoot 프로젝트 yaml 파일 내 특정문자열 암호화
velog.io
https://derveljunit.tistory.com/339
[Spring & Boot][2.6.2] 스프링 프로퍼티 암복호화 Jasypt 예제
스프링부트 2.6.2 버전을 기준으로 작성되었습니다. 예제 Github 주소 https://github.com/Derveljun/derveljun-jasypt-example GitHub - Derveljun/derveljun-jasypt-example Contribute to Derveljun/derveljun-..
derveljunit.tistory.com
'개인적 정리' 카테고리의 다른 글
next(), nextLine() (0) | 2022.09.27 |
---|---|
java -jar jar이름.jar 실행 (0) | 2022.08.02 |
[Linux] 명령어 정리 (0) | 2022.07.26 |
Spring Security - OAuth2UserService (0) | 2022.07.10 |
Spring Security - SpringConfig (0) | 2022.07.10 |