2016년 2월 11일 목요일

Properties file encryption with Jasypt + SpringBoot

You can encrypt your properties by using jasypt library.
I introduce the way to use jasypt encryption with spring-boot

- Structure


















- TestApplication.java

package org.blog.test;

import lombok.extern.slf4j.Slf4j;

import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
import org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;

@SpringBootApplication
@Slf4j
public class TestApplication implements CommandLineRunner {

    @Value("${test.password}")
    private String encryptedPassword;

    @Value("${test.test}")
    private String testString;

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

    @Bean
    public static EnvironmentStringPBEConfig environmentVariablesConfiguration() {
        EnvironmentStringPBEConfig environmentVariablesConfiguration = new EnvironmentStringPBEConfig();
        environmentVariablesConfiguration.setAlgorithm("PBEWithMD5AndDES");
        environmentVariablesConfiguration.setPasswordEnvName("APP_ENCRYPTION_PASSWORD");
        environmentVariablesConfiguration.setPassword("jasypt");
        return environmentVariablesConfiguration;
    }

    @Bean
    public static StringEncryptor configurationEncryptor() {
        StandardPBEStringEncryptor configurationEncryptor = new StandardPBEStringEncryptor();
        configurationEncryptor.setConfig(environmentVariablesConfiguration());
        return configurationEncryptor;
    }

    @Bean
    public static PropertyPlaceholderConfigurer propertyConfigurer() {
        EncryptablePropertyPlaceholderConfigurer propertyConfigurer = new EncryptablePropertyPlaceholderConfigurer(configurationEncryptor());
        propertyConfigurer.setLocation(new ClassPathResource("application.properties"));
        //        propertyConfigurer.setLocation(resource);
        return propertyConfigurer;
    }

    public void run(String... arg0) throws Exception {

        log.info("encryptedPassword : {}", encryptedPassword);

        log.info("testString : {}", testString);
    }
}


- application.properties

test.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)
test.test=test


- logback.xml


 
  
  
   %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
   
  
 

 
  
  
 



- pom.xml


 4.0.0
 org.blog.test
 jasypt-test
 0.1.0

 
  org.springframework.boot
  spring-boot-starter-parent
  1.3.2.RELEASE
 

 
  
   org.springframework.boot
   spring-boot-starter-web
  
  
   org.projectlombok
   lombok
   1.16.6
  
  
   ch.qos.logback
   logback-classic
  
  
   ch.qos.logback
   logback-core
   1.1.3
  
  
   org.jasypt
   jasypt-spring31
   1.9.2
  
 


Jasypt handle the property surrround by "ENC()" as encrypted value, and decrypt that value except for "ENC()".
You can find the value decrypted as below.

test.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm)

encrypted value => G6N718UuyPE5bHyWKyuLQSm02auQPUtm => decrypt => reports_passwd

Execution Result

18:30:45.150 [main] INFO  org.blog.test.TestApplication - encryptedPassword : reports_passwd
18:30:45.150 [main] INFO  org.blog.test.TestApplication - testString : test
18:30:45.155 [main] INFO  org.blog.test.TestApplication - Started TestApplication in 2.503 seconds (JVM running for 2.888)


original source : www.jasypt.org/spring31.html

댓글 2개 :