Quarkus中ConfigSourceInterceptor的加密配置實現(xiàn)
前言
加密配置是一個很常見的需求,在spring boot生態(tài)中,已經(jīng)有非常多的第三方starter實現(xiàn)了,博主所在公司也有這種強制要求,一些敏感配置信息必須加密,比如第三方賬號,數(shù)據(jù)庫密碼等等。所以研究了下怎么在Quarkus中實現(xiàn)類似的配置加密功能。在前文 Quarkus集成apollo配置中心 中,已經(jīng)有介紹過Quarkus中的配置架構(gòu)了,配置加密功能也是基于smallrye-config來實現(xiàn)。
Eclipse MicroProfile Config:https://github.com/eclipse/microprofile-config/
smallrye-config:https://github.com/smallrye/smallrye-config
配置攔截器 ConfigSourceInterceptor
在實現(xiàn)功能前,先看下smallrye-config1.8版本新增的配置攔截器功能。ConfigSourceInterceptor攔截器定義如下:
public interface ConfigSourceInterceptor extends Serializable { ConfigValue getValue(ConfigSourceInterceptorContext context, String name); //省略、、、 }
實現(xiàn)這個接口,可以在配置加載的時候通過context拿到當前配置的值,然后進行任意邏輯操作。
攔截器是通過java.util.ServiceLoader機制加載的,可以通過提供名為io.smallrye.config.ConfigSourceInterceptor的文件進行注冊,該資源META-INF/services/io.smallrye.config.ConfigSourceInterceptor包含完全限定的ConfigSourceInterceptor實現(xiàn)類名稱作為其內(nèi)容。
前文 Quarkus集成apollo配置中心 中,我們已了解Quarkus的配置基于Eclipse MicroProfile Config的規(guī)范和smallrye-config的實現(xiàn),但是ConfigSourceInterceptor的接口設計卻沒有包含在MicroProfile Config的配置規(guī)范中,smallrye團隊正在努力參與規(guī)范的制定,所以后期這個接口很有可能會遷移到 MicroProfile Config包中,不過目前來看,你可以放心的使用smallrye-config1.8版本體驗配置攔截器功能
內(nèi)置的實現(xiàn)
smallrye-config內(nèi)置了如下配置攔截器實現(xiàn):
RelocateConfigSourceInterceptor
ProfileConfigSourceInterceptor
ExpressionConfigSourceInterceptor
FallbackConfigSourceInterceptor
LoggingConfigSourceInterceptor
SecretKeyConfigSourceInterceptor
默認情況下,并非每個攔截器都已注冊。只有ProfileConfigSourceInterceptor, ExpressionConfigSourceInterceptor、SecretKeyConfigSourceInterceptor默認已注冊。
其他攔截器需要通過ServiceLoader機制進行手動注冊。配置中的${}表達式功能正是ExpressionConfigSourceInterceptor來實現(xiàn)的
加密配置實現(xiàn)
基于ConfigSourceInterceptor的機制,實現(xiàn)一個加密的攔截器,在配置時,標記需要被解密的配置,在應用啟動時,攔截配置加載,做解密處理即可。這里使用了AES加解密算法,將aesKey配置在配置文件中,將vi向量直接寫死在代碼里,這樣,即使別人拿到了你的完整配置,不知道vi向量值,也無法解密。
ConfigSourceInterceptor實現(xiàn)類可以通過標準javax.annotation.Priority 注釋指定優(yōu)先級。如果未明確指定優(yōu)先級,則采用io.smallrye.config.Priorities.APPLICATION默認優(yōu)先級值 。指定優(yōu)先級時,value值越小,優(yōu)先級越高,這里指定為PLATFORM早期攔截,代碼如下:
/** * 1、使用方式為 正常配置值的前面拼接Encrypt=>字符串,如 * quarkus.datasource.password = Encrypt=>xxxx * 2、配置解密的aeskey值,如 * config.encrypt.aeskey = 11111111111111111 * * @author kl : http://kailing.pub * @version 1.0 * @date 2020/7/10 9:46 */ //value 值越低優(yōu)先級越高 @Priority(value = Priorities.PLATFORM) public class EncryptConfigInterceptor implements ConfigSourceInterceptor { private static final String CONFIG_ENCRYPT_KEY = "config.encrypt.aeskey"; private static final int AES_KEY_LENGTH = 16; /** * 需要加密值的前綴標記 */ private static final String ENCRYPT_PREFIX_NAME = "Encrypt=>"; /** * AES加密模式 */ private static final String AES_MODE = "AES/CBC/PKCS5Padding"; /** * AES的iv向量值 */ private static final String AES_IV = "1234567890123456"; @Override public ConfigValue getValue(ConfigSourceInterceptorContext context, String name) { ConfigValue config = context.proceed(name); if (config != null && config.getValue().startsWith(ENCRYPT_PREFIX_NAME)) { String encryptValue = config.getValue().replace(ENCRYPT_PREFIX_NAME, ""); String aesKey = context.proceed(CONFIG_ENCRYPT_KEY).getValue(); String value = AesEncyptUtil.decrypt(encryptValue, aesKey); return config.withValue(value); } return config; } public static void main(String[] args) { System.out.println("加密后的配置:"+ AesEncyptUtil.encrypt("office#123", "1111111111111111")); } static class AesEncyptUtil{ public static Cipher getCipher(int mode, String key) { if (key == null || key.length() != AES_KEY_LENGTH) { throw new RuntimeException("config.encrypt.key不能為空,且長度為16位"); } SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), "AES"); //使用CBC模式,需要一個向量iv,可增加加密算法的強度 IvParameterSpec iv = new IvParameterSpec(AES_IV.getBytes()); Cipher cipher = null; try { cipher = Cipher.getInstance(AES_MODE); cipher.init(mode, skeySpec, iv); } catch (InvalidKeyException | InvalidAlgorithmParameterException | NoSuchPaddingException | NoSuchAlgorithmException e) { e.printStackTrace(); } return cipher; } /** * AES加密函數(shù) * @param plaintext 被加密的字符串 * @param key AES key * @return 加密后的值 */ public static String encrypt(final Object plaintext, String key) { if (null == plaintext) { return null; } byte[] encrypted = new byte[0]; try { Cipher encryptCipher = getCipher(Cipher.ENCRYPT_MODE, key); encrypted = encryptCipher.doFinal(String.valueOf(plaintext).getBytes(StandardCharsets.UTF_8)); } catch (IllegalBlockSizeException | BadPaddingException e) { e.printStackTrace(); } //此處使用BASE64做轉(zhuǎn)碼。 return Base64.getEncoder().encodeToString(encrypted); } /** * AES 解密函數(shù) * * @param ciphertext 被解密的字符串 * @param key AES key * @return 解密后的值 */ public static String decrypt(final String ciphertext, String key) { if (null == ciphertext) { return null; } try { Cipher decryptCipher = getCipher(Cipher.DECRYPT_MODE, key); //先用base64解密 byte[] encrypted1 = Base64.getDecoder().decode(ciphertext); byte[] original = decryptCipher.doFinal(encrypted1); return new String(original, StandardCharsets.UTF_8); } catch (Exception ex) { ex.printStackTrace(); return null; } } } }
記得將完整的類名寫入到META-INF/services/io.smallrye.config.ConfigSourceInterceptor這個文件中。使用時先配置好加密的key,在application.properties中添加如下配置:
config.encrypt.aeskey = xxxxxxxxxxxxxx
配置值一定要16位,然后將需要加密的值,使用AesEncyptUtil.encrypt(final Object plaintext, String key)方法先得到加密的值,然后做如下配置,以數(shù)據(jù)庫密碼為例:
quarkus.datasource.username=mobile_office quarkus.datasource.password=Encrypt=>/8wYwbxokEleEZzT4niJew==
使用Encrypt=>標記了這個值是加密的,應用程序加載時會被攔截到,然后做解密處理
結(jié)語
總的來說,Quarkus中使用的一些api設計是非常優(yōu)秀的的,通過預留的這種擴展機制,可以非常輕松的實現(xiàn)擴展功能。
以上就是Quarkus中ConfigSourceInterceptor的加密配置實現(xiàn)的詳細內(nèi)容,更多關(guān)于Quarkus中ConfigSourceInterceptor加密的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
sentinel?整合spring?cloud限流的過程解析
這篇文章主要介紹了sentinel?整合spring?cloud限流,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03Java開發(fā)中synchronized的定義及用法詳解
這篇文章主要介紹了Java開發(fā)中synchronized的定義及用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07