2015-10-01 23 views
2

Jasypt建議將主密鑰傳遞給Java應用程序的一種方法是通過environment variable如何清除Jasypt Java庫建議的環境變量?

在該網頁上,jasypt建議:

這將允許用戶,例如,在環境變量設置加密密碼,啓動應用程序或應用程序服務器,讓jasypt加密對象初始化,然後取消設置變量(從而隱藏它)

如何完成「取消設置變量」部分?

+1

你不能。建議您將密碼放在一個文件中,並閱讀文件。 – Andreas

+0

感謝您的評論。所以,基本上Jasypt提供了一個不安全的Spring集成?似乎Jasypt-Spring應該提供基於文件的方法,除非我錯過了一些東西。 –

回答

0

我結束了以下@Andreas的建議,那就是,從文件中讀取主鍵(在理想情況下應該有嚴格的權限)。

爲了保持Jasypt Spring集成的優勢,我做了一個自定義「FilePBEConfig」:

package com.my.product; 

import org.jasypt.encryption.pbe.config.SimplePBEConfig; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class FilePBEConfig extends SimplePBEConfig { 
    private String passwordFilePath = null; 

    public FilePBEConfig() { 
     super(); 
    } 

    public String getPasswordFilePath() { 
     return passwordFilePath; 
    } 

    public void setPasswordFilePath(final String passwordFilePath) throws IOException { 
     this.passwordFilePath = passwordFilePath; 
     if (passwordFilePath == null) { 
      super.setPassword(null); 
     } else { 
      try (BufferedReader br = new BufferedReader(new FileReader(passwordFilePath))) { 
       super.setPassword(br.readLine()); 
      } 
     } 
    } 
} 

,然後你可以用它在Spring XML文件作爲建議的Jasypt Spring Integration

<bean id="fileConfiguration" 
    class="com.my.product.FilePBEConfig"> 
    <property name="algorithm" value="PBEWithMD5AndDES" /> 
    <property name="passwordFilePath" value="/path/to/masterfile" /> 
</bean>