在micro service體系中,有了config server,我們可以把配置存放在git、svn、數據庫等,普通的web項目也基本上是把配置存放在配置文件中。如果我們把大量的配置信息都放在配置文件中是會有安全隱患的,那么如何消除這個隱患呢?最直接的方式就是把配置信息中的一些敏感信息(比如數據庫密碼、中間件密碼)加密,然后程序在獲取這些配置的時候解密,就可以達到目的。這個時候,jasypt框架就派上用場了。
Jasypt is a java library which allows the developer to add basic encryption capabilities to his/her projects with minimum effort, and without the need of having deep knowledge on how cryptography works.
- High-security, standards-based encryption techniques, both for unidirectional and bidirectional encryption. Encrypt passwords, texts, numbers, binaries...
- Transparent integration with Hibernate.
- Suitable for integration into Spring-based applications and also transparently integrable with Spring Security.
- Integrated capabilities for encrypting the configuration of applications (i.e. datasources).
- Specific features for high-performance encryption in multi-processor/multi-core systems.
- Open API for use with any JCE provider.
- ...and much more
意思其實就是可以把Jasypt這個Java庫當成是一個黑盒子,無需深入了解里面怎么運作的,可以拿來直接用(當然,作為碼農我們還是要去一探究竟的,個人興趣自己看吧)。現在基本上項目都集成spring boot了,因此我們可以使用jasypt-spring-boot-starter。
1. 引入pom包
<dependency> <groupId>com.github.ulisesbocchio</groupId> <artifactId>jasypt-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency>
2. 找到maven倉庫中的 jasypt-1.9.2.jar(路徑是:.m2\repository\org\jasypt\jasypt\1.9.2)
3. 調用jasypt-1.9.2.jar包中一個main方法:org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI ,源碼參考:

package org.jasypt.intf.cli; import java.util.Properties; import org.jasypt.intf.service.JasyptStatelessService; public final class JasyptPBEStringEncryptionCLI { private static final String[][] VALID_REQUIRED_ARGUMENTS = { { "input" }, { "password" } }; private static final String[][] VALID_OPTIONAL_ARGUMENTS = { { "verbose" }, { "algorithm" }, { "keyObtentionIterations" }, { "saltGeneratorClassName" }, { "providerName" }, { "providerClassName" }, { "stringOutputType" } }; public static void main(String[] args) { boolean verbose = CLIUtils.getVerbosity(args); try { String applicationName = null; String[] arguments = null; if ((args[0] == null) || (args[0].indexOf("=") != -1)) { applicationName = JasyptPBEStringEncryptionCLI.class.getName(); arguments = args; } else { applicationName = args[0]; arguments = new String[args.length - 1]; System.arraycopy(args, 1, arguments, 0, args.length - 1); } Properties argumentValues = CLIUtils.getArgumentValues(applicationName, arguments, VALID_REQUIRED_ARGUMENTS, VALID_OPTIONAL_ARGUMENTS); CLIUtils.showEnvironment(verbose); JasyptStatelessService service = new JasyptStatelessService(); String input = argumentValues.getProperty("input"); CLIUtils.showArgumentDescription(argumentValues, verbose); String result = service.encrypt(input, argumentValues.getProperty("password"), null, null, argumentValues.getProperty("algorithm"), null, null, argumentValues.getProperty("keyObtentionIterations"), null, null, argumentValues.getProperty("saltGeneratorClassName"), null, null, argumentValues.getProperty("providerName"), null, null, argumentValues.getProperty("providerClassName"), null, null, argumentValues.getProperty("stringOutputType"), null, null); CLIUtils.showOutput(result, verbose); } catch (Throwable t) { CLIUtils.showError(t, verbose); } } }
然后執行一下語句:
java -cp jasypt-1.9.2.jar org.jasypt.intf.cli.JasyptPBEStringEncryptionCLI input="test" password=salt algorithm=PBEWithMD5AndDES
-- input參數:你想要加密的密碼
-- password參數:jasypt用來加密你的密碼的密碼
結果如下圖,途中的output就是加密后的密文了。
4. 修改配置文件,把配置文件中要加密的字段改為:ENC(密文)
system: mysql: url: xxx.xxx.xx.xx username: username password: ENC(qh8kixDUkvm1DIJrpLFtzw==) jasypt: encryptor: password: salt
以上就可以達到目的。