개발

전자정부프레임워크 3.10 데이터베이스 비밀번호 암호화 생성

피터JK 2025. 3. 14. 10:46
728x90

전자정부프레임워크 3.10 에서 globals.properties에 데이터베이스 암호화된 패스워드를 적용하기 위해 아래 절차를 따른다.

1. EgovEnvCryptoAlgorithmCreateTest.java 파일을 생성하고
 egov-crypto에 필요한 calgorithmKey, algorithmKeyHash 인코딩 키를 생성 한다.

package eGovTest;

import egovframework.rte.fdl.cryptography.EgovPasswordEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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);

    }
}

결과

2025-03-14 14:07:36,918  INFO [eGovTest.EgovEnvCryptoAlgorithmCreateTest] ------------------------------------------------------
2025-03-14 14:07:36,924  INFO [eGovTest.EgovEnvCryptoAlgorithmCreateTest] 알고리즘(algorithm) : SHA-256
2025-03-14 14:07:36,924  INFO [eGovTest.EgovEnvCryptoAlgorithmCreateTest] 알고리즘 키(algorithmKey) : egovframe
2025-03-14 14:07:36,949  INFO [eGovTest.EgovEnvCryptoAlgorithmCreateTest] 알고리즘 키 Hash(algorithmKeyHash) : gdyYs/IZqY86VcWhT8emCYfqY1ahw2vtLG+/FzNqtrQ=
2025-03-14 14:07:36,949  INFO [eGovTest.EgovEnvCryptoAlgorithmCreateTest] 알고리즘 블럭사이즈(algorithmBlockSize)  :1024

 

2. context-crypto.xml 
messageSource bean 추가(유닛 테스트를 위해 추가, 결과 확인 후 제거 한다.)

<?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">
 
 	<!-- Unit Test를 위해 messageSource bean 추가 -->
 	<bean name="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
		<property name="useCodeAsDefaultMessage">
			<value>true</value>
		</property>
		<property name="basenames">
			<list>
				<value>classpath:/egovframework/message/com</value>
			</list>
		</property>
	</bean>

    <egov-crypto:config id="egovCryptoConfig" 
    	initial="true"
    	crypto="true"
    	algorithm="SHA-256"
    	algorithmKey="egovframe"
    	algorithmKeyHash="gdyYs/IZqY86VcWhT8emCYfqY1ahw2vtLG+/FzNqtrQ="
		cryptoBlockSize="1024"
		cryptoPropertyLocation="classpath:/egovframework/egovProps/globals.properties"
	/>
 
</beans>

반응형

3. PasswordEncryptor.java 파일 생성하여 암호화된 비밀번호를 생성한다.

package eGovTest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import egovframework.rte.fdl.cryptography.EgovEnvCryptoService;

public class PasswordEncryptor {
	
	public static void main(String[] args) throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/egovframework/spring/com/context-crypto.xml");
        EgovEnvCryptoService cryptoService = (EgovEnvCryptoService) context.getBean("egovEnvCryptoService");

        String plainPassword = "password";  //비밀번호 입력
        String encryptedPassword = cryptoService.encrypt(plainPassword);

        System.out.println("Encrypted Password: " + encryptedPassword);
    }
}

 

4. PasswordEncryptor.java 실행하여 결과 확인

Encrypted Password: 2khZSrI18xaxRqRdqQWm%2BA%3D%3D

 

5. globals.properties 에 db password 에 출력된 암호 설정

#postgreSQL
Globals.postgres.DriverClassName=org.postgresql.Driver
Globals.postgres.Url=jdbc:postgresql://127.0.0.1:5432/testdb
Globals.postgres.UserName=dbuser
Globals.postgres.Password=2khZSrI18xaxRqRdqQWm%2BA%3D%3D

 

 

참고페이지 : egovframework:rte3.10:fdl:crypto [eGovFrame]

728x90