2016-07-29 9 views
3

私たちはSpring 4.2.5バージョンです。基本的にPropertyPlaceholderConfigurerのカスタム実装を使用して、データを使用する前にデータを復号化する必要があります。これは正常に動作します。しかし、私はさらにプロパティ(通常のコンテキスト:property-placeholderを使用して読み込まれる)に基づいて、このカスタム実装で使用される暗号化メカニズムを変更できる必要があります。これを稼働させる方法はありますか?カスタムPropertyPlaceholderConfigurer内のプロパティにアクセス

答えて

1

これを行う最も簡単な方法は、カスタムPropertyPlaceholderConfigurerではなく、カスタムDefaultPropertiesPersisterです。これは、このように設定されている:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations"> 
     <list> 
      <value>classpath:...</value> 
      <value>...</value> 
     </list> 
    </property> 
    <property name="propertiesPersister"> 
     <bean class="yourPropertiesPersister"/> 
    </property> 
    ... 
</bean> 

は、その後に必要あなたが実装可能DefaultPropertiesPersisterを拡張yourPropertiesPersister

public void load(Properties props, InputStream is) throws IOException { 
    super.load(props, is); 
    decrypt(props); 
} 

@Override 
public void load(Properties props, Reader reader) throws IOException { 
    super.load(props, reader); 
    decrypt(props); 
} 

private void decrypt(Properties props) { 
    // your logic here 
} 

super.load(...)への呼び出しは、生のプロパティをロードします(内容が解読されていません)。いくつかのプロパティの内容に基づいて、ロジックをメソッドdecrypt(props)に追加するだけです。復号化されたプロパティをpropsに追加します。

0

プロジェクトに依存関係をもう1つ追加しても構わない場合は、jasyptライブラリでユースケースがカバーされます。それがうまくいく理由は、Springとシームレスに統合され、暗号化のパラメータ化、つまり使用されるアルゴリズムやマスター暗号化鍵の位置の変更を容易にするということです。

クールなのは、プロパティファイル内の特定のキーに対応する値を注入するスプリングの設定/クラス注釈のコードは、プレーンテキストのプロパティを使用している場合と同じように見えます。

application.properties =>あなたの特性が

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

を提出あなたの春の設定は、(同じに動作します:彼らのウェブサイトでチュートリアル(http://www.jasypt.org/spring31.html)ごととしてここ

は、小さな例です注釈を使用して注入する場合)

<!--                  --> 
<!-- Configuration for encryptor, based on environment variables.   --> 
<!--                  --> 
<!-- In this example, the encryption password will be read from an  --> 
<!-- environment variable called "APP_ENCRYPTION_PASSWORD" which, once --> 
<!-- the application has been started, could be safely unset.    --> 
<!--                  --> 
<bean id="environmentVariablesConfiguration" 
    class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig"> 
    <property name="algorithm" value="PBEWithMD5AndDES" /> 
    <property name="passwordEnvName" value="APP_ENCRYPTION_PASSWORD" /> 
</bean> 


<!--                  --> 
<!-- The will be the encryptor used for decrypting configuration values. --> 
<!--                  --> 
<bean id="configurationEncryptor" 
    class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"> 
    <property name="config" ref="environmentVariablesConfiguration" /> 
</bean> 


<!--                  --> 
<!-- The EncryptablePropertyPlaceholderConfigurer will read the   --> 
<!-- .properties files and make their values accessible as ${var}.  --> 
<!--                  --> 
<!-- Our "configurationEncryptor" bean (which implements     --> 
<!-- org.jasypt.encryption.StringEncryptor) is set as a constructor arg. --> 
<!--                  --> 
<bean id="propertyConfigurer" 
    class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer"> 
    <constructor-arg ref="configurationEncryptor" /> 
    <property name="locations"> 
    <list> 
     <value>/WEB-INF/classes/application.properties</value> 
    </list> 
    </property> 

</bean> 


<!--                  --> 
<!-- Our datasource is configured here, in the usual way. Jasypt's  --> 
<!-- EncryptedPropertyPlaceholderConfigurer will make sure that the  --> 
<!-- ${datasource.password} file gets decrypted and the DBCP DataSource --> 
<!-- will be correctly initialised.          --> 
<!--                  --> 
<bean id="dataSource" 
    class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close"> 
    <property name="driverClassName"> 
    <value>${datasource.driver}</value> 
    </property> 
    <property name="url"> 
    <value>${datasource.url}</value> 
    </property> 
    <property name="username"> 
    <value>${datasource.username}</value> 
    </property> 
    <property name="password"> 
    <value>${datasource.password}</value> 
    </property> 
</bean> 

Jasyptは、指定したプロパティファイルの内容を解析し、ENC()の間の値を復号化します。上記のリンクで詳細を見つけることができます。これは本当に実用的な例です。

jasyptのもう1つの興味深い機能は、command line toolを提供しています。これは、元のプレーンテキスト値を暗号化するのに使用できるアルゴリズムが多数あります。このツールの簡潔な文書は、http://www.jasypt.org/cli.htmlです。

+0

このポインタありがとうございます。しかし、私は、通常のパスワードベースの暗号化とHSMキーに基づく暗号化とを切り替えることができる必要があります。 –

関連する問題