Spring的配置文件是用於指導Spring工廠進行Bean生成、依賴關係注入及Bean示例分發的”圖紙”,他是一個或多個標磚的XML文檔,J2EE程序員必須學會靈活應用這份”圖紙”,準確的表達自己的”生成意圖”。 Spring配置文件是一個或多個標準的XML文檔,applicationContext.xml是Spring的默認配置文件,當容器啟動時找不到指定的配置文檔時,將會嘗試加載這個默認的配置文件。
spring框架在一些對安全性要求較高的生產環境下,配置文件不允許出現明文用戶名密碼配置,如數據庫配置等。本文主要用於解決明文用戶名密碼加密。
通過繼承spring配置類並重寫處理方法實現密文解密
public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer { private String[] encryptPropNames = {"username", "password"}; @Overrideprotected void processProperties(ConfigurableListableBeanFactory beanFactory,Properties props) throws BeansException {try {for (int i = 0;i<encryptPropNames.length;i++){ String value = props.getProperty(encryptPropNames[i]); if (value != null) {props.setProperty(encryptPropNames[i],new String(DES.decrypt(new BASE64Decoder().decodeBuffer(value), "解密秘鑰"))); } }super.processProperties(beanFactory, props);} catch (Exception e) { e.printStackTrace(); throw new BeanInitializationException(e.getMessage());}} }配置applicationContext.xml文件,並在jdbc.properties中設置密文(根據解密秘鑰生成)
<!-- class填寫剛才那段代碼的類路徑--><bean id="propertyConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean>
總結
以上就是本文關於spring配置文件加密方法示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站:
Java編程實現springMVC簡單登錄實例
SpringMVC開發restful API之用戶查詢代碼詳解
Maven管理SpringBoot Profile詳解
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持。