2016년 8월 10일 수요일

Encrypting properties when packaging the jar file



You can encrypt properties when packaging the jar by using jasypt library.

- src/main/resources/application.properties

test.encrypt.value=encrypted
test.not.encrypt.value=notEncrypted


- sub/var.gradle

ext {
    encryptTargetList =
            ['test.encrypt.value']
}

- sub/encrypt.gradle

import org.jasypt.util.text.BasicTextEncryptor

ext.encryptProperty = { string ->
    BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
    textEncryptor.setPassword("jasypt");
    return textEncryptor.encrypt(string);
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.jasypt:jasypt:1.9.2")
    }
}


- build.gradle

fileTree('sub').each { apply from: "${it}" }
apply plugin: 'java'
apply plugin: 'spring-boot'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

processResources {
    filter {
        String line ->
            encryptTargetList.contains(line.split('=')[0]) ? line.split('=')[0] + "=" + "ENC(" + project.encryptProperty(line.split('=')[1]) + ")" : line
    }
}


(*) result

you can find the encrypted value at properties file in the jar.

- build/resources/main/application.properties

test.encrypt.value=ENC(blnPoVkyiAzejbT1mdA48fmDyxRS86Gl)
test.not.encrypt.value=notEncrypted

댓글 없음 :

댓글 쓰기