Springで外部プロパティファイルを暗号化する方法
最近、Struts 2のリモートアクセスセキュリティに関するネット上の脆弱性の問題を見て、プロジェクトで構成された機密情報がハッカーに入手されたらよくないので、jdbc.propertiesのファイルの中のデータベースのユーザー名を構成するなどの情報に対して明文を使うのはあまり安全ではありません.Webアプリケーションシステムのクライアントユーザはサービス側のプロファイルを見ることができないが,サーバにログインできる人は容易に見ることができる.セキュリティ要件の高いシステムでは、暗号化を採用することが望ましい.
情報の暗号化は対称と非対称の2つの方式に分けられ、前者は暗号化後の情報が元の値に復号できることを示し、後者は暗号化後の情報に基づいて復元できない.MD 5は非対称暗号化に属し、DESは対称暗号化に属し、DESを使用して属性値を暗号化する.属性値が読み込まれたら、DESで復号します.
以下は暗号化ツールクラスです
SpringでPropertyPlaceholderConfigurerを使用して復号化します.PropertyPlaceholderConfigurer自体は復号化をサポートしていません.拡張により、String convertProperty(String propertyName,String ropertyValue)メソッドを上書きし、ユーザー名とパスワードの属性を時行解密します.
Springプロファイルに次のように設定します.
ps; BASE 64 Encoder暗号化jarパッケージの解決方法が見つかりません参照してくださいhttp://yzhw.iteye.com/blog/1700778
情報の暗号化は対称と非対称の2つの方式に分けられ、前者は暗号化後の情報が元の値に復号できることを示し、後者は暗号化後の情報に基づいて復元できない.MD 5は非対称暗号化に属し、DESは対称暗号化に属し、DESを使用して属性値を暗号化する.属性値が読み込まれたら、DESで復号します.
以下は暗号化ツールクラスです
package com.demo.utils;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class DESUtils {
private static Key key;
private static String KEY_STR = "key";
static {
try{
KeyGenerator generator = KeyGenerator.getInstance("DES");
generator.init(new SecureRandom(KEY_STR.getBytes()));
key = generator.generateKey();
generator = null;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* DES
* @param str
* @return BASE64
*/
public static String getEncryptString(String str) {
BASE64Encoder base64en = new BASE64Encoder();
try {
byte[] strBytes = str.getBytes("UTF8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return base64en.encode(encryptStrBytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* BASE64
* @param str
* @return
*/
public static String getDecryptString(String str){
BASE64Decoder base64De = new BASE64Decoder();
try {
byte[] strBytes = base64De.decodeBuffer(str);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptStrBytes = cipher.doFinal(strBytes);
return new String(decryptStrBytes, "UTF8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String str = "abcd";
System.out.println(DESUtils.getEncryptString(str));
String enStr = "BhykG14EE7o=";
System.out.println(DESUtils.getDecryptString(enStr));
}
}
SpringでPropertyPlaceholderConfigurerを使用して復号化します.PropertyPlaceholderConfigurer自体は復号化をサポートしていません.拡張により、String convertProperty(String propertyName,String ropertyValue)メソッドを上書きし、ユーザー名とパスワードの属性を時行解密します.
public class DemoPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigrer {
protected String converProperty(String propertyName, String propertyValue) {
if() { //
String decryptValue = DESUtils.getDecryptString(propertyValue);
return decryptValue;
} else {
return propertyValue;
}
}
}
Springプロファイルに次のように設定します.
<bean class="com.demo.DemoPropertyPlaceholderConfigurer" p:location="classpath:jdbc.properties" p:fileEncoding="utf-8" />
ps; BASE 64 Encoder暗号化jarパッケージの解決方法が見つかりません参照してくださいhttp://yzhw.iteye.com/blog/1700778