在SpringBoot 2.x 中,我們通過yml檔案來進行各種配置,之前我們需要通過編寫一個DESUtil來對擷取的配置進行解密,但是通過下面的方式可以直接對配置檔案進行自動解密
@Configuration
public class EncryptionPropertyConfig {
@Bean(name="encryptablePropertyResolver")
public EncryptablePropertyResolver encryptablePropertyResolver() {
return new EncryptionPropertyResolver();
}
class EncryptionPropertyResolver implements EncryptablePropertyResolver {
private static final String DES = "[email protected]";
@Override
public String resolvePropertyValue(String value) {
if(StringUtils.isBlank(value)) {
return value;
}
// 傳回明文
if(value.startsWith(DES)) {
return resolveDESValue(value.substring(DES.length()));
}
return value;
}
private String resolveDESValue(String value) {
return DESUtil.getDecryptString(value);
}
}
}
這樣,我們隻需要在yml檔案中加上[email protected]為字首,就可以進行自動解密,省去了手動在代碼中進行解密的操作了。
同時附上正常的DESUtil的制作方式
public class DESUtil
{
private static Key key;
private static String KEY_STR="**************";
private static final Logger log = LoggerFactory.getLogger(DESUtil.class);
static{
try {
KeyGenerator generator = KeyGenerator.getInstance("DES");
SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(KEY_STR.getBytes());
generator.init(secureRandom);
key = generator.generateKey();
generator=null;
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new RuntimeException(e);
}
}
/**
* 對字元串進行加密,傳回BASE64的加密字元串
* <功能較長的描述>
* @param str
* @return
* @see [類、類#方法、類#成員]
*/
public static String getEncryptString(String str){
Base64 base64Encoder = new Base64();
try {
byte[] strBytes = str.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return base64Encoder.encodeToString(encryptStrBytes);
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new RuntimeException(e);
}
}
/**
* 對BASE64加密字元串進行解密
* <功能較長的描述>
* @param str
* @return
* @see [類、類#方法、類#成員]
*/
public static String getDecryptString(String encryptStr){
if(encryptStr==null) {
return null;
}
Base64 base64Encoder = new Base64();
try {
byte[] strBytes = base64Encoder.decode(encryptStr);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return new String(encryptStrBytes,"UTF-8");
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new RuntimeException(e);
}
}
public static String getEncryptString(String str, String keyStr){
Base64 base64Encoder = new Base64();
try {
Key key = getKey(keyStr);
byte[] strBytes = str.getBytes("UTF-8");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return base64Encoder.encodeToString(encryptStrBytes);
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new RuntimeException(e);
}
}
public static String getDecryptString(String encryptStr,String keyStr){
if(encryptStr==null) {
return null;
}
Base64 base64Encoder = new Base64();
try {
Key key = getKey(keyStr);
byte[] strBytes = base64Encoder.decode(encryptStr);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptStrBytes = cipher.doFinal(strBytes);
return new String(encryptStrBytes,"UTF-8");
} catch (Exception e) {
log.error(e.getMessage(),e);
throw new RuntimeException(e);
}
}
private static Key getKey(String keyStr) throws NoSuchAlgorithmException{
KeyGenerator generator = KeyGenerator.getInstance("DES");
SecureRandom secureRandom=SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(keyStr.getBytes());
generator.init(secureRandom);
return generator.generateKey();
}
}