본문 바로가기
개발/개발환경설정

Spring property 암호화 (jasypt)

by 카리3 2020. 8. 11.

1. POM.xml 설정

		<!-- jasypt 프로퍼티 암호화 -->
		<!-- https://mvnrepository.com/artifact/org.jasypt/jasypt -->
		<dependency>
		    <groupId>org.jasypt</groupId>
		    <artifactId>jasypt</artifactId>
		    <version>${org.jasypt-version}</version>
		</dependency>
		
		<dependency>
            <groupId>org.jasypt</groupId>
            <artifactId>jasypt-spring31</artifactId>
            <version>${org.jasypt-version}</version>
		</dependency>

2. mysql password 암호화

	public void encryptTest() {
		StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
		encryptor.setPassword("encryptpassword");
		encryptor.setAlgorithm("PBEWithMD5AndDES");
		// encryptor.setSaltGenerator(new StringFixedSaltGenerator("someFixedSalt"));

		String str = "1234";
		String encStr = encryptor.encrypt(str);
		String decStr = encryptor.decrypt(encStr);
		log.info("encStr : " + encStr);
	}
    
encStr : iRjKfgR5lWlM4TVktzXkrw==

3. config.properties에 저장

#DB
mysql.username =  SECKIM
mysql.password = ENC(iRjKfgR5lWlM4TVktzXkrw==)

4. root-context.xml 설정

<!-- Jasypt -->
    <bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWITHMD5ANDDES"/>
	<!--<property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD"/> tomcat 환경변수-->
        <property name="password" value="encryptpassword"/>
    </bean>    

    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration"/>
    </bean>    

    <bean id="propertyConfig" class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor"/>
        <property name="location" value="classpath:config.properties"/>
    </bean>       
   
	<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    	<property name="driverClassName" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy" />
        <property name="url" value="jdbc:log4jdbc:mysql://127.0.0.1:3306/SECKIMDB?useSSL=false&amp;serverTimezone=UTC" />
		<property name="username" value="#{configProperties['mysql.username']}"></property>
        <property name="password" value="${mysql.password}"></property>
    </bean> 

 

'개발 > 개발환경설정' 카테고리의 다른 글

Spring 다국어 적용  (0) 2020.08.11
Spring message ResourceBundle 설정  (0) 2020.08.11
util:properties 설정  (0) 2020.08.10
Tiles 설정  (0) 2020.08.10
Filter 설정  (0) 2020.03.18