2010-08-06 29 views
14

Şu anda bir özellik dosyasında şifreyi [şifrelenmemiş] tutuyorum. Bu şifre karınca kullanarak xml yapılandırmasında olduğu gibi yerleştirilir.
Apache'de nasıl şifreli şifre kullanılır? BasicDataSource?

Şimdi, karınca hedef sonra şifre şifrelenmiş biçimde kopyalanır mümkündür [yapılandırma XML bu dbcp.BasicDataSource nesne yaratıyor, veri kaynağı içindir]. Jasypt'in bunu yapabildiğini duydum! Şimdiye kadar bunu denemedim. Ama sorun burada bitmiyor. BasicDataSource şifreli şifreyi kabul etmiyor. BasicDatasource için herhangi bir değiştirme var mı.

FYI: Eğer önemliyse, Spring kullanıyorum.

cevap

2

(dosya kopyalama sorumlu) mevcut görev Copy uzatarak yeni bir görev oluşturun:

yüzden yeni BasicDataSource sınıfı yalnızca yöntemini böyle createDataSource() şey geçersiz kılmak gerekir. FilterSet'u genişleterek yeni bir tür oluşturun (belirteçlerin filtrelenmesinden sorumludur). How to create nested element for ant task?

build.xml

<target name="encrypted-copy" > 
     <CopyEncrypted todir="dist/xyz/config" overwrite="true"> 
      <fileset dir="config"/>     
      <encryptionAwareFilterSet> 
       <filtersfile file="conf/properties/blah-blah.properties" /> 
      </encryptionAwareFilterSet> 
     </CopyEncrypted> 
    </target> 

blah-blah.properties

property1=value1 
property2=value2 
PASSWORD=^&YUII%%&*(
USERNAME=rjuyal 
CONNECTION_URL=... 
someotherproperty=value 

yapılandırma xml

-:
burada kodunu görmek
<bean id="dataSource" 
     class="com.xyz.datasource.EncryptionAwareDataSource" 
     destroy-method="close" autowire="byName"> 
     <property name="driverClassName"> 
      <value>com.ibm.db2.jcc.DB2Driver</value> 
     </property> 
     <property name="url"> 
      <value>@[email protected]</value> 
     </property> 
     <property name="username"> 
      <value>@[email protected]</value> 
     </property> 
     <property name="password"> 
      <value>@[email protected]</value> 
     </property> 
     <property name="poolPreparedStatements"> 
      <value>true</value> 
     </property> 
     <property name="maxActive"> 
      <value>10</value> 
     </property> 
     <property name="maxIdle"> 
      <value>10</value> 
     </property>  
    </bean> 
... 
... 
... 

Hedefin yürütülmesinden sonra, xml, özellikler dosyasındaki değerlerle kopyalanır. Şifre şifrelenecektir.

Bu, şifrelenmiş parolayı işleyecektir. EncryptionAwareDataSource

public class EncryptionAwareDataSource extends BasicDataSource{ 
    @Override 
    public synchronized void setPassword(String password) {  
     super.setPassword(Encryptor.getDecryptedValue(password)); 
    } 
} 

'Hepsi bu;)

3

aşağıdaki jasypt bağlantı şifreli içeriği içeren bir özellikler dosyası Uygulama içerisindeki okunabilir açıklar:

http://www.jasypt.org/encrypting-configuration.html

özellikleri ANT içinde benim önerim olarak harika bir görevi kullanmaktır dosyasını oluşturmak için aşağıdaki gibidir:

<taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/> 

<groovy> 
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor 

def encryptor = new StandardPBEStringEncryptor(); 
encryptor.setPassword("secret"); 

def f = new File("config.properties") 
f.println "datasource.driver=com.mysql.jdbc.Driver" 
f.println "datasource.url=jdbc:mysql://localhost/reportsdb" 
f.println "datasource.username=reportsUser" 
f.println "datasource.password=ENC("+encryptor.encrypt("dbpassword")+")"  

</groovy> 
2

, BasicDataSource uzatın SetPassword ve setUserName yöntemleri geçersiz kılar. Bu yöntemlerdeki değerleri şifreler ve bunları süper sınıf yöntemlerine iletir.

16

İlkbaharda daha iyi bir yol var: PropertyPlaceholderConfigurer sınıfını kullanın. Eğer mülk yer tutucuya PropertiesPersister bir alt sınıfını belirlerken

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <value>classpath:com/foo/jdbc.properties</value> 
    </property> 
    <property name="propertiesPersister"> 
     <bean class="com.mycompany.MyPropertyPersister" /> 
    </property>   
</bean> 

<bean id="dataSource" destroy-method="close" 
     class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="${jdbc.driverClassName}"/> 
    <property name="url" value="${jdbc.url}"/> 
    <property name="username" value="${jdbc.username}"/> 
    <property name="password" value="${jdbc.password}"/> 
</bean> 

, Bahar jdbc.properties yüklemek ve o sınıfı kullanarak dosyanın şifresini. Belki bir şey gibi:
public class MyPropertyPersister extends DefaultPropertiesPersister 
{ 
    // ... initializing stuff... 

    public void load(Properties props, InputStream is) throws IOException 
    { 
     Cipher decrypter = getCipher(); 
     InputStream cis = new CipherInputStream(is, decrypter); 
     super.load(props, cis); 
    } 

    public void load(Properties props, Reader reader) throws IOException 
    { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     IOUtils.copy(reader, baos); 
     ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 

     Cipher decrypter = getCipher(); 
     InputStream cis = new CipherInputStream(bais, decrypter); 

     InputStreamReader realReader = new InputStreamReader(cis); 
     super.load(props, realReader); 
    } 

    public void loadFromXml(Properties props, InputStream is) throws IOException 
    { 
     Cipher decrypter = getCipher(); 
     InputStream cis = new CipherInputStream(is, decrypter); 
     super.loadFromXml(props, cis); 
    } 

    private Cipher getCipher() 
    { 
     // return a Cipher to read the encrypted properties file 
     ... 
    } 
    ... 
} 
Bu yardımcı olur umarım.

DÜZENLEME Eğer Jasypt kullanırsanız, herhangi PropertiesPersister tanımlamak gerekmez. Gönderen Jasypt documentation:

Jasypt şifrelenmiş değerler içeren dosyaları .properties okuyabilir bu yapılandırma ile ilgili İlkbahar sınıfların bir uygulamasını sağlar (EncryptableProperties sınıfı tarafından yönetilen olanlar gibi) ve Bahar geri kalanına şeffaf bunları işlemek uygulama kuru fasulye Gizli özelliğini şifresini için Bununla

, bu

jdbc.driver=com.mysql.jdbc.Driver 
jdbc.url=jdbc:mysql://localhost/reportsdb 
jdbc.username=reportsUser 
jdbc.password=ENC(G6N718UuyPE5bHyWKyuLQSm02auQPUtm) 

gibi jdbc.properties tanımlayabilir ve Bahar yapılandırma bu

<bean class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer"> 
    <constructor-arg> 
    <bean class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"> 
     <property name="config"> 
     <bean class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig"> 
      <property name="algorithm" value="PBEWithMD5AndDES" /> 
      <property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" /> 
     </bean> 
     </property> 
    </bean> 
    </constructor-arg> 
    <property name="locations"> 
    <list> 
     <value>/WEB-INF/classes/jdbc.properties</value> 
    </list> 
    </property> 
</bean> 

<bean id="dataSource" destroy-method="close" 
     class="org.apache.commons.dbcp.BasicDataSource"> 
    <property name="driverClassName" value="${jdbc.driverClassName}"/> 
    <property name="url" value="${jdbc.url}"/> 
    <property name="username" value="${jdbc.username}"/> 
    <property name="password" value="${jdbc.password}"/> 
</bean> 

Bu yol gibi olabilir, parolayı koyabilirsiniz uygulamayı başlattığınızda ve daha sonra ayıkladığınızda bir ortam değişkeninde.

+2

Çok yararlı. Sadece önemsiz bir düzeltme, özellikler dosyasında jdbc.driver ama fasulye tanımında $ {jdbc.driverClassName}. – jbird

3

BasicDataSource durumunda tam olarak doğru değil.

BasicDataSource için javadocs okursanız, havuz başlatıldıktan sonra setPassword() hiçbir etkisi yoktur.Havuz, ilk olarak aşağıdaki yöntemlerden biri başlatılır: getConnection, setLogwriter, setLoginTimeout, getLoginTimeout, getLogWriter.

Ref: http://www.docjar.com/html/api/org/apache/commons/dbcp/BasicDataSource.java.html

Bütün bu yöntemler sonuçta createDataSource() diyoruz.

public class NewBasicDataSource extends BasicDataSource { 

    protected synchronized DataSource createDataSource() throws SQLException { 
     String decryptedPassword = decryptPassword(super.getPassword()); 
     super.setPassword(decryptedPassword); 
     return super.createDataSource(); 
    } 

    private String decryptPassword(String password) { 
     return //logic to decrypt current password 
    } 
} 
İlgili konular