application.yml 파일에 대한 설정 정보 암호화/복호화는 spring boot 에서 제공하는 config server 서버을 활용하면 구현이 가능하다. 다만 이를 위해서 별도의 config server 구성이 필요하다. config server 구성 없이 jasypt 을 기반으로 암호화/복호화를 기능을 제공하고 있다.
// build.gradle.kts
dependencies {
implementation("studio.one.starter:studio-platform-starter")
**implementation("**studio.one.starter:**studio-platform-starter-jasypt")**
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("org.springframework.boot:spring-boot-starter-aop")
implementation("org.springframework.boot:spring-boot-starter-web")
}
studio.features.jasypt.enabled 값을 true 로 설정하고 암호화를 위한 비밀번호를 설정한다.운영에서는 환경변수 JASYPT_ENCRYPTOR_PASSWORD 을 사용하여 주입하는 것을 권장한다
studio:
features:
jasypt:
enabled: true
fail-if-missing: true
encryptor:
password: ${JASYPT_ENCRYPTOR_PASSWORD}
algorithm: PBEWithSHA256And128BitAES-CBC-BC # Bouncy Castle 알고리즘 사용 provider-name: BC
key-obtention-iterations: 1000
pool-size: 1
salt-generator-classname: org.jasypt.salt.RandomSaltGenerator
iv-generator-classname: org.jasypt.iv.RandomIvGenerator
string-output-type: base64
암호화/복호화를 위해서 웹 기능을 활성화 해야한다. 보안을 위하여 헤더에 인증 값을 추가할 수 있는데, require-token 을 true 로 설정하고 token-value 에 인증 값을 설정하면 클라이언트는 헤더에 X-JASYPT-TOKEN 에 해당 token-value 을 넣어야 정상적으로 암호화/복호화를 할 수 있게 된다. 주의할 점은 암호화 값을 센팅하는 경우에만 studio.features.jasypt.web.enabled 값을 true 로 설정하는 것을 권장한다.
studio:
features:
jasypt:
enabled: true
fail-if-missing: true
encryptor:
password: ${JASYPT_ENCRYPTOR_PASSWORD}
algorithm: PBEWithSHA256And128BitAES-CBC-BC # Bouncy Castle 알고리즘 사용 provider-name: BC
key-obtention-iterations: 1000
pool-size: 1
salt-generator-classname: org.jasypt.salt.RandomSaltGenerator
iv-generator-classname: org.jasypt.iv.RandomIvGenerator
string-output-type: base64
http:
enabled: true
base-path: /internal/jasypt
require-token: true
token-value: local-dev-token
아래와 같이 실행하면 ENC(...) 형태로 반환된다.
# 암호화 (로컬)
curl -v -i -X POST <http://127.0.0.1:8080/internal/jasypt/encrypt> \\
-H "Content-Type: application/json" \\
-H "X-JASYPT-TOKEN: local-dev-token" \\
-d '{"value":"hello"}'
응답 값을 application.yml 파일에 설정하면 된다. 복호화는 다음과 같이 실행하면 된다.
# 복호화 (로컬)
curl -s -X POST <http://127.0.0.1:8080/internal/jasypt/decrypt> \\
-H "Content-Type: application/json" \\
-H "X-JASYPT-TOKEN: local-dev-token" \\
-d '{"value":"ENC(5UNeZ8d+fwPs1BrrYswRwe1wuWpLQWNY27iiKv5h7P20Mb/twATIz1quSIh3Amd7)"}'