差異處
這裏顯示兩個版本的差異處。
java:jasypt:withproperties [2016/01/16 00:57] tony 建立 |
java:jasypt:withproperties [2023/06/25 09:48] |
||
---|---|---|---|
行 1: | 行 1: | ||
- | {{tag>java Jasypt}} | ||
- | ====== Jasypt With Properties File ====== | ||
- | ===== How to? ===== | ||
- | <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> | ||
- | |||
- | ===== ===== | ||
- | ---- | ||
- | \\ | ||
- | ~~DISQUS~~ |