開心一刻html
一隻被二哈帶偏了的柴犬,我只想弄死隔壁的二哈java
BeanFactoryPostProcessor接口很簡單,只包含一個方法mysql
/** * 經過BeanFactoryPostProcessor,咱們自定義修改應用程序上下文中的bean定義 * * 應用上下文可以在全部的bean定義中自動檢測出BeanFactoryPostProcessor bean, * 並在任何其餘bean建立以前應用這些BeanFactoryPostProcessor bean * * BeanFactoryPostProcessor對自定義配置文件很是有用,能夠覆蓋應用上下文已經配置了的bean屬性 * * PropertyResourceConfigurer就是BeanFactoryPostProcessor的典型應用 * 將xml文件中的佔位符替換成properties文件中相應的key對應的value */ @FunctionalInterface public interface BeanFactoryPostProcessor { /** * 在應用上下文完成了標準的初始化以後,修改其內部的bean工廠 * 將加載全部bean定義,但還沒有實例化任何bean. * 咱們能夠覆蓋或添加bean定義中的屬性,甚至是提早初始化bean */ void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
推薦你們直接去讀它的源碼註釋,說的更詳細、更好理解git
簡單來講,BeanFactoryPostProcessor是spring對外提供的接口,用來拓展spring,可以在spring容器加載了全部bean的信息信息以後、bean實例化以前執行,修改bean的定義屬性;有人可能會問,這有什麼用?你們還記得spring配置文件中的佔位符嗎? 咱們會在spring配置中配置PropertyPlaceholderConfigurer(繼承PropertyResourceConfigurer)bean來處理佔位符, 舉個例子你們就有印象了github
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:mysqldb.properties</value> </list> </property> </bean> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName"value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}"/> <property name="password"value="${jdbc.password}" /> </bean> </beans>
mysqldb.propertiesredis
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://192.168.1.100:3306/mybatis jdbc.username=root jdbc.password=root
PropertyPlaceholderConfigurer類的繼承關係圖算法
怎麼用,這個問題比較簡單,咱們實現BeanFactoryPostProcessor接口,而後將將其註冊到spring容器便可,在spring啓動過程當中,在常規bean實例化以前,會執行BeanFactoryPostProcessor的postProcessBeanFactory方法(裏面有咱們想要的邏輯),完成咱們想要的操做;spring
重點應該是:用來幹什麼sql
上述佔位符的例子是BeanFactoryPostProcessor的應用之一,但這是spring提供的BeanFactoryPostProcessor拓展,不是咱們自定義的;實際工做中,自定義BeanFactoryPostProcessor的狀況確實少,反正至少我是用的很是少的,但我仍是有使用印象的,那就是對敏感信息的解密處理;上述數據庫的鏈接配置中,用戶名和密碼都是明文配置的,這就存在泄漏風險,還有redis的鏈接配置、shiro的加密算法、rabbitmq的鏈接配置等等,凡是涉及到敏感信息的,都須要進行加密處理,信息安全很是重要數據庫
配置的時候以密文配置,在真正用到以前在spring容器中進行解密,而後用解密後的信息進行真正的操做,下面我就舉個簡單的例子,用BeanFactoryPostProcessor來完整敏感信息的解密
加解密工具類:DecryptUtil.java
package com.lee.app.util; import org.apache.commons.lang3.StringUtils; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import java.security.Key; import java.security.SecureRandom; public class DecryptUtil { private static final String CHARSET = "utf-8"; private static final String ALGORITHM = "AES"; private static final String RANDOM_ALGORITHM = "SHA1PRNG"; public static String aesEncrypt(String content, String key) { if (content == null || key == null) { return null; } Key secretKey = getKey(key); try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] p = content.getBytes(CHARSET); byte[] result = cipher.doFinal(p); BASE64Encoder encoder = new BASE64Encoder(); String encoded = encoder.encode(result); return encoded; } catch (Exception e) { throw new RuntimeException(e); } } public static String aesDecrypt(String content, String key) { Key secretKey = getKey(key); try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, secretKey); BASE64Decoder decoder = new BASE64Decoder(); byte[] c = decoder.decodeBuffer(content); byte[] result = cipher.doFinal(c); String plainText = new String(result, CHARSET); return plainText; } catch (Exception e) { throw new RuntimeException(e); } } private static Key getKey(String key) { if (StringUtils.isEmpty(key)) { key = "hello!@#$world";// 默認key } try { SecureRandom secureRandom = SecureRandom.getInstance(RANDOM_ALGORITHM); secureRandom.setSeed(key.getBytes()); KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM); generator.init(secureRandom); return generator.generateKey(); } catch (Exception e) { throw new RuntimeException(e); } } public static void main(String[] args) { // key能夠隨意取,DecryptConfig中decryptKey與此相同便可 String newUserName= aesEncrypt("root", "hello!@#$world"); // QL34YffNntJi1OWG7zGqVw== System.out.println(newUserName); String originUserName = aesDecrypt(newUserName, "hello!@#$world"); System.out.println(originUserName); String newPassword = aesEncrypt("123456", "hello!@#$world"); // zfF/EU6k4YtzTnKVZ6xddw== System.out.println(newPassword); String orignPassword = aesDecrypt(newPassword, "hello!@#$world"); System.out.println(orignPassword); } }
配置文件:application.yml
server: servlet: context-path: /app port: 8888 spring: #鏈接池配置 datasource: type: com.alibaba.druid.pool.DruidDataSource druid: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8 #Enc[:解密標誌前綴,]:解密後綴標誌,中間內容是須要解密的內容 username: Enc[QL34YffNntJi1OWG7zGqVw==] password: Enc[zfF/EU6k4YtzTnKVZ6xddw==] initial-size: 1 #鏈接池初始大小 max-active: 20 #鏈接池中最大的活躍鏈接數 min-idle: 1 #鏈接池中最小的活躍鏈接數 max-wait: 60000 #配置獲取鏈接等待超時的時間 pool-prepared-statements: true #打開PSCache,而且指定每一個鏈接上PSCache的大小 max-pool-prepared-statement-per-connection-size: 20 validation-query: SELECT 1 FROM DUAL validation-query-timeout: 30000 test-on-borrow: false #是否在得到鏈接後檢測其可用性 test-on-return: false #是否在鏈接放回鏈接池後檢測其可用性 test-while-idle: true #是否在鏈接空閒一段時間後檢測其可用性 #mybatis配置 mybatis: type-aliases-package: com.lee.app.entity #config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mapper/*.xml # pagehelper配置 pagehelper: helperDialect: mysql #分頁合理化,pageNum<=0則查詢第一頁的記錄;pageNum大於總頁數,則查詢最後一頁的記錄 reasonable: true supportMethodsArguments: true params: count=countSql decrypt: prefix: "Enc[" suffix: "]" key: "hello!@#$world"
工程中解密:DecryptConfig.java
package com.lee.app.config; import com.lee.app.util.DecryptUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.env.OriginTrackedMapPropertySource; import org.springframework.context.EnvironmentAware; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.stereotype.Component; import java.util.LinkedHashMap; import java.util.stream.Collectors; import java.util.stream.StreamSupport; /** * 敏感信息的解密 */ @Component public class DecryptConfig implements EnvironmentAware, BeanFactoryPostProcessor { private static final Logger LOGGER = LoggerFactory.getLogger(DecryptConfig.class); private ConfigurableEnvironment environment; private String decryptPrefix = "Enc["; // 解密前綴標誌 默認值 private String decryptSuffix = "]"; // 解密後綴標誌 默認值 private String decryptKey = "hello!@#$world"; // 解密能夠 默認值 @Override public void setEnvironment(Environment environment) { this.environment = (ConfigurableEnvironment) environment; } @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { LOGGER.info("敏感信息解密開始....."); MutablePropertySources propSources = environment.getPropertySources(); StreamSupport.stream(propSources.spliterator(), false) .filter(ps -> ps instanceof OriginTrackedMapPropertySource) .collect(Collectors.toList()) .forEach(ps -> convertPropertySource((PropertySource<LinkedHashMap>) ps)); LOGGER.info("敏感信息解密完成....."); } /** * 解密相關屬性 * @param ps */ private void convertPropertySource(PropertySource<LinkedHashMap> ps) { LinkedHashMap source = ps.getSource(); setDecryptProperties(source); source.forEach((k,v) -> { String value = String.valueOf(v); if (!value.startsWith(decryptPrefix) || !value.endsWith(decryptSuffix)) { return; } String cipherText = value.replace(decryptPrefix, "").replace(decryptSuffix, ""); String clearText = DecryptUtil.aesDecrypt(cipherText, decryptKey); source.put(k, clearText); }); } /** * 設置解密屬性 * @param source */ private void setDecryptProperties(LinkedHashMap source) { decryptPrefix = source.get("decrypt.prefix") == null ? decryptPrefix : String.valueOf(source.get("decrypt.prefix")); decryptSuffix = source.get("decrypt.suffix") == null ? decryptSuffix : String.valueOf(source.get("decrypt.suffix")); decryptKey = source.get("decrypt.key") == null ? decryptKey : String.valueOf(source.get("decrypt.key")); } }
主要就是3個文件,DecryptUtil對明文進行加密處理後,獲得的值配置到application.yml中,而後工程啓動的時候,DecryptConfig會對密文進行解密,明文信息存到了spring容器,後續操做都是在spring容器的明文上進行的,因此與咱們平時的不加密的結果一致,可是卻對敏感信息進行了保護;工程測試結果以下:
完整工程地址:spring-boot-BeanFactoryPostProcessor
有興趣的能夠去看下jasypt-spring-boot的源碼,你會發現他的原理是同樣的,也是基於BeanFactoryPostProcessor的拓展
爲何DecryptConfig實現了BeanFactoryPostProcessor,將DecryptConfig註冊到spring以後,DecryptConfig的postProcessBeanFactory方法就會執行?事出必有因,確定是spring啓動過程當中會調用DecryptConfig實例的postProcessBeanFactory方法,具體咱們來看看源碼,咱們從AbstractApplicationContext的refresh方法開始
不得不說,spring的命名、註釋確實寫得好,很明顯咱們從refresh中的invokeBeanFactoryPostProcessors方法開始,你們能夠仔細看下PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors方法,先按PriorityOrdered、Ordered、普通(沒有實現PriorityOrdered和Ordered接口)的順序調用BeanDefinitionRegistryPostProcessor,而後再按先按PriorityOrdered、Ordered、普通的順序調用BeanFactoryPostProcessor,這個順序仍是值得你們注意下的,若是咱們自定義的多個BeanFactoryPostProcessor有順序之分,而咱們又沒有指定其執行順序,那麼可能出現的不是咱們想要的結果
這裏可能會有會有人有這樣的的疑問:bean定義(BeanDefinition)是在何時加載到spring容器的,如何保證BeanFactoryPostProcessor實例起做用以前,全部的bean定義都已經加載到了spring容器
ConfigurationClassPostProcessor實現了BeanDefinitionRegistryPostProcessor,在springboot的createApplicationContext階段註冊到spring容器的,也就是說在spring的refresh以前就有了ConfigurationClassPostProcessor實例;ConfigurationClassPostProcessor被應用的時候(調用其postProcessBeanDefinitionRegistry方法),會加載所有的bean定義(包括咱們自定義的BeanFactoryPostProcessor實例:DecryptConfig)到spring容器,bean的加載詳情可查看:springboot2.0.3源碼篇 - 自動配置的實現,是你想象中的那樣嗎,那麼在應用BeanFactoryPostProcessor實例以前,全部的bean定義就已經加載到spring容器了,BeanFactoryPostProcessor實例也就能修改bean定義了
至此,BeanFactoryPostProcessor的機制咱們就清楚了,爲何能這麼用這個問題也就明瞭了
一、BeanFactoryPostProcessor是beanFactory的後置處理器接口,經過BeanFactoryPostProcessor,咱們能夠自定義spring容器中的bean定義,BeanFactoryPostProcessor是在spring容器加載了bean的定義信息以後、bean實例化以前執行;
二、BeanFactoryPostProcessor類型的bean會被spring自動檢測,在常規bean實例化以前被spring調用;
三、BeanFactoryPostProcessor的經常使用場景包括spring中佔位符的處理、咱們自定義的敏感信息的解密處理,固然不侷限與此;
其實只要咱們明白了BeanFactoryPostProcessor的生效時機,哪些場景適用BeanFactoryPostProcessor也就很清楚了