mybatis-plus的SafetyEncryptProcessor安全加密處理示例解析
序
本文主要研究一下mybatis-plus的SafetyEncryptProcessor
SafetyEncryptProcessor
mybatis-plus-boot-starter/src/main/java/com/baomidou/mybatisplus/autoconfigure/SafetyEncryptProcessor.java
public class SafetyEncryptProcessor implements EnvironmentPostProcessor { @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { /** * 命令行中獲取密鑰 */ String mpwKey = null; for (PropertySource<?> ps : environment.getPropertySources()) { if (ps instanceof SimpleCommandLinePropertySource) { SimpleCommandLinePropertySource source = (SimpleCommandLinePropertySource) ps; mpwKey = source.getProperty("mpw.key"); break; } } /** * 處理加密內(nèi)容 */ if (StringUtils.isNotBlank(mpwKey)) { HashMap<String, Object> map = new HashMap<>(); for (PropertySource<?> ps : environment.getPropertySources()) { if (ps instanceof OriginTrackedMapPropertySource) { OriginTrackedMapPropertySource source = (OriginTrackedMapPropertySource) ps; for (String name : source.getPropertyNames()) { Object value = source.getProperty(name); if (value instanceof String) { String str = (String) value; if (str.startsWith("mpw:")) { map.put(name, AES.decrypt(str.substring(4), mpwKey)); } } } } } // 將解密的數(shù)據(jù)放入環(huán)境變量,并處于第一優(yōu)先級上 if (CollectionUtils.isNotEmpty(map)) { environment.getPropertySources().addFirst(new MapPropertySource("custom-encrypt", map)); } } } }
SafetyEncryptProcessor實(shí)現(xiàn)了EnvironmentPostProcessor接口,在postProcessEnvironment方法中先是找到mpw.key,然后遍歷所有PropertySource的所有屬性,找到mpw:開頭的,然后進(jìn)行解密并替換到密文,最后放在environment的第一個PropertySource
spring.factories
mybatis-plus-boot-starter/src/main/resources/META-INF/spring.factories
# Auto Configure org.springframework.boot.env.EnvironmentPostProcessor=\ com.baomidou.mybatisplus.autoconfigure.SafetyEncryptProcessor
小結(jié)
之前的文章聊聊springboot的EnvironmentPostProcessor提到springboot提供了EnvironmentPostProcessor接口,該接口有postProcessEnvironment方法,其中envrionment參數(shù)類型為ConfigurableEnvironment,即應(yīng)用可以通過實(shí)現(xiàn)這個接口進(jìn)行env環(huán)境變量的操作。而mybatis-plus的SafetyEncryptProcessor正是一個實(shí)戰(zhàn)的好例子。
以上就是mybatis-plus的SafetyEncryptProcessor安全加密處理示例解析的詳細(xì)內(nèi)容,更多關(guān)于mybatis-plus SafetyEncryptProcessor的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的全過程
我們開發(fā)業(yè)務(wù)系統(tǒng)的時(shí)候,經(jīng)常有那種文檔文件在線預(yù)覽的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot實(shí)現(xiàn)文件在線預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下2021-11-11JavaCV使用ffmpeg實(shí)現(xiàn)錄屏功能
這篇文章主要介紹了JavaCV如何使用ffmpeg實(shí)現(xiàn)錄屏功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-06-06maven tomcat plugin實(shí)現(xiàn)熱部署
這篇文章主要介紹了maven tomcat plugin實(shí)現(xiàn)熱部署,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07springboot如何讀取application.yml文件
這篇文章主要介紹了springboot如何讀取application.yml文件的方法,幫助大家更好的理解和使用springboot框架,感興趣的朋友可以了解下2020-12-12IntelliJ IDEA中查看文件內(nèi)所有已聲明的方法(類似eclipse的outline)
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中查看文件內(nèi)所有已聲明的方法(類似eclipse的outline),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10