IT/WEB

[JAVA] DB 접속정보를 Base64로 인코딩/디코딩 하여 비식별화를 해보자.

오달달씨 2021. 1. 6. 00:01
728x90
반응형

dbcp는 java class의 BasicDataSource에 상수로 선언되어있다.

DB 접속정보가 써져있는 xml에서 대게 BasicDataSource로 DB정보를 직접 보낸다.

나는 xml에 써져 있는 DB 정보를 비식별화하기 위해 Base64 인코딩/디코딩 하여 dbcp에 정보를 보내려고한다.

BasicDataSource는 java에 있는 class이고 final 상수 타입으로 선언되어있어 직접 컴파일하기 어렵다.

따라서 BasicDataSource를 상속받고 암호화된 정보를 복호화 할 때 필요한 정보만 처리하도록한다.


DB접속정보 xml

<!-- <bean id="dataSource_type1" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> -->
<bean id="dataSource_type2" class="util.SecureBasicDataSource">
<property name="driverClassName" value="org.gjt.mm.mysql.Driver"/>
<property name="url" value="URL Base64 인코딩"/>
<property name="username" value="DB명 Base64 인코딩"/>
<property name="password" value="DB 비밀번호 Base64 인코딩"/>
</bean>

Base64를 사용해서 암호화 / 복호화를 해보자.

import java.util.Base64; 
import java.util.Base64.Decoder; 
import java.util.Base64.Encoder;

public class Base64Test { 
	public static void main(String[] args) { 
	base64(); 
	} 
	
	public static void base64() { 
     		String text = "ktko"; 
		byte[] targetBytes = text.getBytes(); 

		// Base64 인코딩 /////////////////////////////////////////////////// 
		Encoder encoder = Base64.getEncoder();
		byte[] encodedBytes = encoder.encode(targetBytes); 

		// Base64 디코딩 /////////////////////////////////////////////////// 
		Decoder decoder = Base64.getDecoder(); 
		byte[] decodedBytes = decoder.decode(encodedBytes); 

		System.out.println("인코딩 전 : " + text); 
		System.out.println("인코딩 text : " + new String(encodedBytes)); 
		System.out.println("디코딩 text : " + new String(decodedBytes)); 
	} 
}

BasicDataSource를 상속받는 SecureBasicDataSource 클래스를 만들어서

xml에서 Base64로 인코딩 된 DB명과 DB 비밀번호를 직접 디코딩하고 부모클래스 BasicDataSource에 세팅한다.

package util;

import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.dbcp.BasicDataSource;

public class SecureBasicDataSource extends BasicDataSource {
	
    public void setUsername(String username){
    	
    	byte[] decodedBytes = Base64.decodeBase64(username.getBytes()); 
    	
        super.setUsername(new String(decodedBytes)); 
    }
   
    public void setPassword(String password) {
    	
    	byte[] decodedBytes = Base64.decodeBase64(password.getBytes()); 
    	
        super.setPassword(new String(decodedBytes));
    }
    
}
728x90
반응형