欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring配置文件加密方法示例

 更新時(shí)間:2017年11月22日 15:20:55   作者:hwl0420  
這篇文章主要介紹了spring配置文件加密方法示例,簡單介紹了什么是配置文件,然后分享了在實(shí)際生產(chǎn)環(huán)境中,對(duì)配置文件不允許出現(xiàn)明文用戶名及密碼等信息需求的Java實(shí)現(xiàn)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。

Spring的配置文件是用于指導(dǎo)Spring工廠進(jìn)行Bean生成、依賴關(guān)系注入及Bean示例分發(fā)的”圖紙”,他是一個(gè)或多個(gè)標(biāo)磚的XML文檔,J2EE程序員必須學(xué)會(huì)靈活應(yīng)用這份”圖紙”,準(zhǔn)確的表達(dá)自己的”生成意圖”。Spring配置文件是一個(gè)或多個(gè)標(biāo)準(zhǔn)的XML文檔,applicationContext.xml是Spring的默認(rèn)配置文件,當(dāng)容器啟動(dòng)時(shí)找不到指定的配置文檔時(shí),將會(huì)嘗試加載這個(gè)默認(rèn)的配置文件。

spring框架在一些對(duì)安全性要求較高的生產(chǎn)環(huán)境下,配置文件不允許出現(xiàn)明文用戶名密碼配置,如數(shù)據(jù)庫配置等。本文主要用于解決明文用戶名密碼加密。

通過繼承spring配置類并重寫處理方法實(shí)現(xiàn)密文解密

public class EncryptPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
	 private String[] encryptPropNames = {"username", "password"}; 

	@Override
	protected 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中設(shè)置密文(根據(jù)解密秘鑰生成)

<!-- class填寫剛才那段代碼的類路徑-->
<bean id="propertyConfigurer" class="com.**.EncryptPropertyPlaceholderConfigurer"> 
      <property name="locations">
        <list>
          <value>classpath:jdbc.properties</value>
        </list>
      </property>
  </bean>

總結(jié)

以上就是本文關(guān)于spring配置文件加密方法示例的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:

Java編程實(shí)現(xiàn)springMVC簡單登錄實(shí)例

SpringMVC開發(fā)restful API之用戶查詢代碼詳解

Maven管理SpringBoot Profile詳解

如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持。

相關(guān)文章

最新評(píng)論