반응형
Jasypt 라이브러리를 통한 DB 접속정보 암호화
사내에서 DB 서버 보안을 위해 접속정보를 암호화처리 하기로 하였습니다.
개발환경 : JDK 11, Spring Boot 2.4.0
1. Jasypt 라이브러리 의존성 추가
implementation 'com.github.ulisesbocchio:jasypt-spring-boot-starter:3.0.4'
2. JasyptConfig Class 추가
@Configuration
public class JasyptConfig {
@Value("${jasypt.encryptor.password}")
private String encryptKey;
@Bean("jasyptStringEncryptor")
public StringEncryptor stringEncryptor() {
if (encryptKey == null || encryptKey.trim().isEmpty()) {
throw new IllegalArgumentException("Jasypt 암호화 키(jasypt.encryptor.password)가 설정되지 않음");
}
PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor();
SimpleStringPBEConfig config = new SimpleStringPBEConfig();
config.setPassword(encryptKey);
config.setAlgorithm("PBEWITHHMACSHA512ANDAES_256"); // 적용 알고리즘
config.setKeyObtentionIterations("1000"); // 해싱 반복 횟수
config.setPoolSize("1"); // 인스턴스 Pool 갯수
config.setProviderName("SunJCE");
config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
config.setStringOutputType("base64"); // 인코딩 방식
encryptor.setConfig(config);
return encryptor;
}
}
@EnableEncryptableProperties 의 경우 Spring Boot 2.4+ 버전이고 Jasypt 가 최신버전일 때 추가하면 되므로 삭제하였습니다.
PBEWITHHMACSHA512ANDAES\_256 알고리즘의 경우 PBE 기반 + SHA-512 해시로 키를 파생하여 AES-256 블록음 암호화 처리 함으로써 아주 강력한 보안으로 해당 알고리즘을 선택하였습니다.
실무에서도 해당 알고리즘으로 암호화하는 것이 일반적이며 PBEWithMD5AndDES 의 경우 레거시 프로젝트에서 사용한다고 합니다.
3. 암호화 키 생성 테스트
@EnableConfigurationProperties
class JasyptConfigTest {
@Value("${jasypt.encryptor.password}")
String encryptKey;
@Test
@DisplayName("DB 정보 암호화 테스트")
void stringEncryptor() {
String url = "DB URL 정보";
String username = "DB USERNAME 정보";
String password = "DB PASSWORD 정보";
System.out.println(jasyptEncoding(url));
System.out.println(jasyptEncoding(username));
System.out.println(jasyptEncoding(password));
}
public String jasyptEncoding(String value) {
StandardPBEStringEncryptor stringEncryptor = new StandardPBEStringEncryptor();
System.out.println(encryptKey);
stringEncryptor.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
stringEncryptor.setPassword(encryptKey);
stringEncryptor.setIvGenerator(new RandomIvGenerator());
return stringEncryptor.encrypt(value);
}
}
위 테스트 코드를 실행하여 나온 url, username, password 의 값들을 application.yml 에 넣어주면 됩니다.
4. application.yml 설정
spring:
profiles: LOCAL
datasource:
url: ENC(...)
username: ENC(...)
password: ENC(...)
driver-class-name: org.mariadb.jdbc.Driver
5. VM Options 설정 추가
-Djasypt.encryptor.password="encryptorPassword"
@Value 에 들어간 jasypt.encryptor.password 의 값은 VM Options 에 추가하여 서버 실행시 password 가 적용되도록 하였습니다.
해당 방법 이외에 아래와 같이 application.yml 에 추가하는 방법도 있습니다.
jasypt:
encryptor:
password: encryptorPassword반응형
'Back-End > Spring' 카테고리의 다른 글
| [Spring] Gradle Project war 파일 생성 (0) | 2023.10.06 |
|---|---|
| [Spring] 실무에서 자주사용하는 Lombok 어노테이션 (0) | 2023.01.05 |
| [Spring] Spring boot, MyBatis 환경 log4jdbc 설정 (0) | 2022.12.30 |
| [Spring] cannot deserialize from Object value (no delegate- or property-based Creator) 에러 (0) | 2022.06.13 |
| [Spring] Spring Boot 로 Hello world 실행하기 (0) | 2022.06.12 |

최근댓글