差異處
這裏顯示兩個版本的差異處。
java:jasypt:withproperties [2016/01/16 01:01] tony |
java:jasypt:withproperties [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>java Jasypt}} | ||
- | ====== Jasypt With Properties File ====== | ||
- | ===== How to? ===== | ||
- | Jasypt的EncryptableProperties類別,提供讀取加密過後的properties檔案,其中密文以ENC()包住。 | ||
- | <code> | ||
- | # | ||
- | #Sat Jan 16 00:52:05 CST 2016 | ||
- | password=ENC(xWWu6PWYHwfVmNMTXCJG5A\=\=) | ||
- | account=root | ||
- | </code> | ||
- | 以下為測試範例,存檔是透過一般的Properties類別,需要加密的部分透過PropertyValueEncryptionUtils類別去做轉換;讀檔則使用EncryptableProperties類別: | ||
- | <code java> | ||
- | @Test | ||
- | public void test() throws IOException{ | ||
- | StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); | ||
- | encryptor.setPassword("jasypt"); | ||
- | |||
- | Properties p = new Properties(); | ||
- | p.setProperty("account", "root"); | ||
- | p.setProperty("password", PropertyValueEncryptionUtils.encrypt("123456", encryptor)); | ||
- | |||
- | File file = new File("test.properties"); | ||
- | file.createNewFile(); | ||
- | |||
- | FileOutputStream fos = null; | ||
- | try { | ||
- | fos = new FileOutputStream(file); | ||
- | p.store(fos, "gg"); | ||
- | } finally { | ||
- | close(fos); | ||
- | } | ||
- | |||
- | EncryptableProperties ep = new EncryptableProperties(encryptor); | ||
- | FileInputStream fis = null; | ||
- | try { | ||
- | fis = new FileInputStream(file); | ||
- | ep.load(fis); | ||
- | } finally { | ||
- | close(fis); | ||
- | } | ||
- | |||
- | Assert.assertEquals("root", ep.getProperty("account")); | ||
- | Assert.assertEquals("123456", ep.getProperty("password")); | ||
- | } | ||
- | </code> | ||
- | ===== Reference ===== | ||
- | * [[http://www.jasypt.org/encrypting-configuration.html|encrypting-configuration]] | ||
- | ===== ===== | ||
- | ---- | ||
- | \\ | ||
- | ~~DISQUS~~ |