DB 접속 정보를 암호화 하는 이유 

프로젝트 내 DB 연결 값을 암호화 하지 않고 그대로 올릴 경우 서버에 직접 접근하지 않는 이상 확인하기 어렵겠지만 

 

접속이 가능하다면 누구나 DB 정보를 확인할 수 있다는 보안 취약점이 있다. 

 

그래서 가능한 민감 정보는 암호화해서 사용하는 것을 권장한다.  (필수는 아니다)

 

 

 

암호화를 위해  jasypt 사용 

나의 경우 jasypt 를 사용한 암호화 작업을 진행했다. 

 

 

pom.xml

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

 

jasyptConfig.java

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JasyptConfig {
	private static final String ENCRYPT_KEY = "skdmldufthlrkqtdlqslek"; // 임의로 지정한 키값 

    @Bean("JasyptStringEncryptor")
    public StandardPBEStringEncryptor stringEncryptor() {
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();

        config.setPassword(ENCRYPT_KEY);
        config.setAlgorithm("PBEWithSHA1AndRC4_128");
        encryptor.setConfig(config);

        
        return encryptor;
    }
    
    
    
    
    // DB에 연결할 url, password 등의 입력값을 암호화 해서 넣어주려고 할 때 
    // 다운받아서 쓰는 방법도 있지만 나는 main 메서드 하나 파서 암호화 작업함. 
    // 실제 프로젝트를 업로드할때에는 main 메서드는 지워주기! 
	public static void main(String[] args) {
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();

        config.setPassword(ENCRYPT_KEY);
        config.setAlgorithm("PBEWithSHA1AndRC4_128"); // 알고리즘을 잘 골라야한다. 취약하지 않은 알고리즘으로
        encryptor.setConfig(config);
        
        // DB 하나만 연결할 때
        String url = encryptor.encrypt("jdbc:oracle:thin:@192.168.01.01:1521/orcl");
        
        // DB 다중화 되어있어 여러개 연결할 경우.
        // String url = encryptor.encrypt("@(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.01.01)(PORT = 1521))(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.01.02)(PORT = 1521))(CONNECT_DATA = (SERVER = DEDICATED)(SERVICE_NAME = orcl)))");

        String username= encryptor.encrypt("mytest");
        String pass = encryptor.encrypt("test1234");
        
        System.out.println(url);
        System.out.println(username);
        System.out.println(pass);      
      
      // 프로젝트 우클릭 run As -> java Application 실행하면 콘솔창에서 암호화된 값을 
      // 확인할 수 있다. ( 단축키 : alt +shift+x  누른다음 j 눌러주기.
      
        
	}
}

 

실행하면 아래와 같이 암호화 키를 얻을 수  있다! 

 

 

application.properties ( 꼭 이 파일이 아니더라도 내가 설정해둔 properties에 상황에 맞게 넣어주면된다. ) 

spring.datasource.url=ENC(dzUolknJnznQLlBweSj2AOhP1V6cOlZcGqhC/0uM7YBIQGKk+VAQYbNqXUCDoI4fgw==)
spring.datasource.username=ENC(jEOr0qnaLLVoIZ6bzSo=)
spring.datasource.password=ENC(zzkHO3LV9EauEoLpzvRrxw==)

 

+ Recent posts