Goal
- Crypto 간소화 서비스를 통해 DB 접속 정보를 암호화한다
- 암호화한 DB정보를 Global.properties 에 세팅한다
Crypto 간소화 서비스란?
- 표준프레임워크 3.8 부터 ARIA 블록암호 알고리즘 기반 암/복호화 설정을 간소화 할 수 있는 방법을 제공
- 내부적으로 필요한 설정을 가지고 있고, XML Schema를 통해 필요한 설정만을 추가할 수 있도록 제공한
- globals.properties 설정 파일의 중요 정보 Url, UserName, Password 항목을 암/복호화 처리 할 수 있도록 제공
예제 1. Crypto 간소화 서비스를 통해 DB 접속 정보 암호화 하기
1. XML namespace 및 schema, Crypto Config 설정
<!-- EgovEnvCryptoUserTest.java 설정 파일 -->
<!-- context-crypto-test.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:egov-crypto="http://maven.egovframe.go.kr/schema/egov-crypto"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://maven.egovframe.go.kr/schema/egov-crypto http://maven.egovframe.go.kr/schema/egov-crypto/egov-crypto-3.10.0.xsd">
<!--
initial : globals.properties 연계 Url, UserName, Password 값 로드 여부(설정값 : true, false)
crypto : 계정 암호화 여부(설정값 : true, false)
algorithm : 계정 암호화 알고리즘
algorithmKey : 계정 암호화키 키
cryptoBlockSize : 계정 암호화키 블록사이즈
cryptoPropertyLocation : 설정파일 암복호화 경로 (선택) 기본값은 'classpath:/egovframework/egovProps/globals.properties'
-->
<egov-crypto:config id="egovCryptoConfig"
initial="true"
crypto="true"
algorithm="SHA-256"
algorithmKey="(생성값)"
algorithmKeyHash="(생성값)"
cryptoBlockSize="1024"
cryptoPropertyLocation="classpath:/프로젝트명/egovProps/globals.properties"
/>
</beans>
- context-crypto-test.xml을 생성하고 위와 같은 xml 선언
- Crypto Config 설정을 하는데, 최초에는 algorithmKey와 algorithmKeyHash가 없을 것이다. 이 부분은 아래에서 알고리즘을 통해 생성하여 값을 세팅할 것임
2. Crypto algorithmKey, algorithmKeyHash 생성
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import egovframework.rte.fdl.cryptography.EgovPasswordEncoder;
public class EgovEnvCryptoAlgorithmCreateTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EgovEnvCryptoAlgorithmCreateTest.class);
//계정암호화키 키
public String algorithmKey = "egovframe"; // 사용자정의
//계정암호화 알고리즘(MD5, SHA-1, SHA-256)
public String algorithm = "SHA-256";
//계정암호화키 블럭사이즈
public int algorithmBlockSize = 1024;
public static void main(String[] args) {
EgovEnvCryptoAlgorithmCreateTest cryptoTest = new EgovEnvCryptoAlgorithmCreateTest();
EgovPasswordEncoder egovPasswordEncoder = new EgovPasswordEncoder();
egovPasswordEncoder.setAlgorithm(cryptoTest.algorithm);
LOGGER.info("------------------------------------------------------");
LOGGER.info("알고리즘(algorithm) : "+cryptoTest.algorithm);
LOGGER.info("알고리즘 키(algorithmKey) : "+cryptoTest.algorithmKey);
LOGGER.info("알고리즘 키 Hash(algorithmKeyHash) : "+egovPasswordEncoder.encryptPassword(cryptoTest.algorithmKey));
LOGGER.info("알고리즘 블럭사이즈(algorithmBlockSize) :"+cryptoTest.algorithmBlockSize);
}
}
- 계정 암호화키를 egovframe로 하였음
- 메인함수를 실행하면 암호화된 알고리즘 키 Hash값이 출력됨
3. context-crypto-test.xml 수정
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:egov-crypto="http://maven.egovframe.go.kr/schema/egov-crypto"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://maven.egovframe.go.kr/schema/egov-crypto http://maven.egovframe.go.kr/schema/egov-crypto/egov-crypto-3.10.0.xsd">
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="useCodeAsDefaultMessage">
<value>true</value>
</property>
<property name="basenames">
<list>
//프로젝트 경로 설정
<value>classpath:/프로젝트명/egovProps/globals</value>
</list>
</property>
</bean>
<!--
initial : globals.properties 연계 Url, UserName, Password 값 로드 여부(설정값 : true, false)
crypto : 계정 암호화 여부(설정값 : true, false)
algorithm : 계정 암호화 알고리즘
algorithmKey : 계정 암호화키 키
cryptoBlockSize : 계정 암호화키 블록사이즈
cryptoPropertyLocation : 설정파일 암복호화 경로 (선택) 기본값은 'classpath:/egovframework/egovProps/globals.properties'
-->
<egov-crypto:config id="egovCryptoConfig"
initial="true"
crypto="true"
algorithm="SHA-256"
algorithmKey="egovfreame"
algorithmKeyHash="sdgHSlfkja/2GH24+="
cryptoBlockSize="1024"
cryptoPropertyLocation="classpath:/프로젝트명/egovProps/globals.properties"
/>
</beans>
- 출력된 암호화 정보를 Crypto Config 설정정보에 세팅
- <bean name="messageSource" ></> 의 정보세팅
4. 데이터베이스 정보 암호화 값 생성
package rec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import egovframework.rte.fdl.cryptography.EgovEnvCryptoService;
import egovframework.rte.fdl.cryptography.impl.EgovEnvCryptoServiceImpl;
public class EgovEnvCryptoUserTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EgovEnvCryptoUserTest.class);
public static void main(String[] args) {
String[] arrCryptoString = {
"userId", //데이터베이스 접속 계정 설정
"password", //데이터베이스 접속 패드워드 설정
"url", //데이터베이스 접속 주소 설정
"databaseDriver" //데이터베이스 드라이버
};
System.out.println("------------------------------------------------------");
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:프로젝트명/spring/context-crypto-test.xml"});
EgovEnvCryptoService cryptoService = context.getBean(EgovEnvCryptoServiceImpl.class);
System.out.println("------------------------------------------------------");
String label = "";
try {
for(int i=0; i < arrCryptoString.length; i++) {
if(i==0)label = "사용자 아이디";
if(i==1)label = "사용자 비밀번호";
if(i==2)label = "접속 주소";
if(i==3)label = "데이터 베이스 드라이버";
System.out.println(label+" 원본(orignal):" + arrCryptoString[i]);
System.out.println(label+" 인코딩(encrypted):" + cryptoService.encrypt(arrCryptoString[i]));
System.out.println(label+" 인코딩(decryted):" + cryptoService.decrypt(arrCryptoString[i]));
System.out.println("------------------------------------------------------");
}
} catch (IllegalArgumentException e) {
LOGGER.error("["+e.getClass()+"] IllegalArgumentException : " + e.getMessage());
} catch (Exception e) {
LOGGER.error("["+e.getClass()+"] Exception : " + e.getMessage());
}
}
}
- 메임함수 실행 시 암호화 된 db정보 콘솔에 출력
5. globals.properties 인코딩 값 설정
#MariaDB
Globals.maria.DriverClassName=org.mariadb.jdbc.Driver
Globals.maria.Url=(생성값)
Globals.maria.UserName = (생성값)
Globals.maria.Password = (생성값)
- 해당 프로젝트는 MariaDB를 사용했기 때문에 위와 같이 세팅했고, 사용한 데이터베이스에 따라 설정하면 됨
끝. 성공
밖에서 진행했던 프로젝트를 회사 서버에 다시 올리면서 몇몇 프로퍼티 설정을 그에 맞게 바꿔줬어야 했다.
이와 관련된 설정들은 프로젝트 진행 시에는 내가 했던게 아니라서 구글링해서 전자정부프레임워크 가이드보면서 했는데 삽질 좀 했다.
해결하긴 했쥐만 나와 같은 분들 그리고 미래의 나를 위해 기록 ~!
참고
https://www.egovframe.go.kr/wiki/doku.php?id=egovframework:rte2:fdl:crypto_simplify_v3_8
'나의 에러일지' 카테고리의 다른 글
the proxy server received an invalid response from an upstream server (0) | 2022.08.01 |
---|---|
The temporary upload location [] is not valid (0) | 2022.07.28 |
Goal
- Crypto 간소화 서비스를 통해 DB 접속 정보를 암호화한다
- 암호화한 DB정보를 Global.properties 에 세팅한다
Crypto 간소화 서비스란?
- 표준프레임워크 3.8 부터 ARIA 블록암호 알고리즘 기반 암/복호화 설정을 간소화 할 수 있는 방법을 제공
- 내부적으로 필요한 설정을 가지고 있고, XML Schema를 통해 필요한 설정만을 추가할 수 있도록 제공한
- globals.properties 설정 파일의 중요 정보 Url, UserName, Password 항목을 암/복호화 처리 할 수 있도록 제공
예제 1. Crypto 간소화 서비스를 통해 DB 접속 정보 암호화 하기
1. XML namespace 및 schema, Crypto Config 설정
<!-- EgovEnvCryptoUserTest.java 설정 파일 -->
<!-- context-crypto-test.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:egov-crypto="http://maven.egovframe.go.kr/schema/egov-crypto"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://maven.egovframe.go.kr/schema/egov-crypto http://maven.egovframe.go.kr/schema/egov-crypto/egov-crypto-3.10.0.xsd">
<!--
initial : globals.properties 연계 Url, UserName, Password 값 로드 여부(설정값 : true, false)
crypto : 계정 암호화 여부(설정값 : true, false)
algorithm : 계정 암호화 알고리즘
algorithmKey : 계정 암호화키 키
cryptoBlockSize : 계정 암호화키 블록사이즈
cryptoPropertyLocation : 설정파일 암복호화 경로 (선택) 기본값은 'classpath:/egovframework/egovProps/globals.properties'
-->
<egov-crypto:config id="egovCryptoConfig"
initial="true"
crypto="true"
algorithm="SHA-256"
algorithmKey="(생성값)"
algorithmKeyHash="(생성값)"
cryptoBlockSize="1024"
cryptoPropertyLocation="classpath:/프로젝트명/egovProps/globals.properties"
/>
</beans>
- context-crypto-test.xml을 생성하고 위와 같은 xml 선언
- Crypto Config 설정을 하는데, 최초에는 algorithmKey와 algorithmKeyHash가 없을 것이다. 이 부분은 아래에서 알고리즘을 통해 생성하여 값을 세팅할 것임
2. Crypto algorithmKey, algorithmKeyHash 생성
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import egovframework.rte.fdl.cryptography.EgovPasswordEncoder;
public class EgovEnvCryptoAlgorithmCreateTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EgovEnvCryptoAlgorithmCreateTest.class);
//계정암호화키 키
public String algorithmKey = "egovframe"; // 사용자정의
//계정암호화 알고리즘(MD5, SHA-1, SHA-256)
public String algorithm = "SHA-256";
//계정암호화키 블럭사이즈
public int algorithmBlockSize = 1024;
public static void main(String[] args) {
EgovEnvCryptoAlgorithmCreateTest cryptoTest = new EgovEnvCryptoAlgorithmCreateTest();
EgovPasswordEncoder egovPasswordEncoder = new EgovPasswordEncoder();
egovPasswordEncoder.setAlgorithm(cryptoTest.algorithm);
LOGGER.info("------------------------------------------------------");
LOGGER.info("알고리즘(algorithm) : "+cryptoTest.algorithm);
LOGGER.info("알고리즘 키(algorithmKey) : "+cryptoTest.algorithmKey);
LOGGER.info("알고리즘 키 Hash(algorithmKeyHash) : "+egovPasswordEncoder.encryptPassword(cryptoTest.algorithmKey));
LOGGER.info("알고리즘 블럭사이즈(algorithmBlockSize) :"+cryptoTest.algorithmBlockSize);
}
}
- 계정 암호화키를 egovframe로 하였음
- 메인함수를 실행하면 암호화된 알고리즘 키 Hash값이 출력됨
3. context-crypto-test.xml 수정
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:egov-crypto="http://maven.egovframe.go.kr/schema/egov-crypto"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://maven.egovframe.go.kr/schema/egov-crypto http://maven.egovframe.go.kr/schema/egov-crypto/egov-crypto-3.10.0.xsd">
<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="useCodeAsDefaultMessage">
<value>true</value>
</property>
<property name="basenames">
<list>
//프로젝트 경로 설정
<value>classpath:/프로젝트명/egovProps/globals</value>
</list>
</property>
</bean>
<!--
initial : globals.properties 연계 Url, UserName, Password 값 로드 여부(설정값 : true, false)
crypto : 계정 암호화 여부(설정값 : true, false)
algorithm : 계정 암호화 알고리즘
algorithmKey : 계정 암호화키 키
cryptoBlockSize : 계정 암호화키 블록사이즈
cryptoPropertyLocation : 설정파일 암복호화 경로 (선택) 기본값은 'classpath:/egovframework/egovProps/globals.properties'
-->
<egov-crypto:config id="egovCryptoConfig"
initial="true"
crypto="true"
algorithm="SHA-256"
algorithmKey="egovfreame"
algorithmKeyHash="sdgHSlfkja/2GH24+="
cryptoBlockSize="1024"
cryptoPropertyLocation="classpath:/프로젝트명/egovProps/globals.properties"
/>
</beans>
- 출력된 암호화 정보를 Crypto Config 설정정보에 세팅
- <bean name="messageSource" ></> 의 정보세팅
4. 데이터베이스 정보 암호화 값 생성
package rec;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import egovframework.rte.fdl.cryptography.EgovEnvCryptoService;
import egovframework.rte.fdl.cryptography.impl.EgovEnvCryptoServiceImpl;
public class EgovEnvCryptoUserTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EgovEnvCryptoUserTest.class);
public static void main(String[] args) {
String[] arrCryptoString = {
"userId", //데이터베이스 접속 계정 설정
"password", //데이터베이스 접속 패드워드 설정
"url", //데이터베이스 접속 주소 설정
"databaseDriver" //데이터베이스 드라이버
};
System.out.println("------------------------------------------------------");
ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"classpath:프로젝트명/spring/context-crypto-test.xml"});
EgovEnvCryptoService cryptoService = context.getBean(EgovEnvCryptoServiceImpl.class);
System.out.println("------------------------------------------------------");
String label = "";
try {
for(int i=0; i < arrCryptoString.length; i++) {
if(i==0)label = "사용자 아이디";
if(i==1)label = "사용자 비밀번호";
if(i==2)label = "접속 주소";
if(i==3)label = "데이터 베이스 드라이버";
System.out.println(label+" 원본(orignal):" + arrCryptoString[i]);
System.out.println(label+" 인코딩(encrypted):" + cryptoService.encrypt(arrCryptoString[i]));
System.out.println(label+" 인코딩(decryted):" + cryptoService.decrypt(arrCryptoString[i]));
System.out.println("------------------------------------------------------");
}
} catch (IllegalArgumentException e) {
LOGGER.error("["+e.getClass()+"] IllegalArgumentException : " + e.getMessage());
} catch (Exception e) {
LOGGER.error("["+e.getClass()+"] Exception : " + e.getMessage());
}
}
}
- 메임함수 실행 시 암호화 된 db정보 콘솔에 출력
5. globals.properties 인코딩 값 설정
#MariaDB
Globals.maria.DriverClassName=org.mariadb.jdbc.Driver
Globals.maria.Url=(생성값)
Globals.maria.UserName = (생성값)
Globals.maria.Password = (생성값)
- 해당 프로젝트는 MariaDB를 사용했기 때문에 위와 같이 세팅했고, 사용한 데이터베이스에 따라 설정하면 됨
끝. 성공
밖에서 진행했던 프로젝트를 회사 서버에 다시 올리면서 몇몇 프로퍼티 설정을 그에 맞게 바꿔줬어야 했다.
이와 관련된 설정들은 프로젝트 진행 시에는 내가 했던게 아니라서 구글링해서 전자정부프레임워크 가이드보면서 했는데 삽질 좀 했다.
해결하긴 했쥐만 나와 같은 분들 그리고 미래의 나를 위해 기록 ~!
참고
https://www.egovframe.go.kr/wiki/doku.php?id=egovframework:rte2:fdl:crypto_simplify_v3_8
'나의 에러일지' 카테고리의 다른 글
the proxy server received an invalid response from an upstream server (0) | 2022.08.01 |
---|---|
The temporary upload location [] is not valid (0) | 2022.07.28 |