Spring的配置文件是用于指導(dǎo)Spring工廠進(jìn)行Bean生成、依賴關(guān)系注入及Bean示例分發(fā)的”圖紙”,他是一個或多個標(biāo)磚的XML文檔,J2EE程序員必須學(xué)會靈活應(yīng)用這份”圖紙”,準(zhǔn)確的表達(dá)自己的”生成意圖”。Spring配置文件是一個或多個標(biāo)準(zhǔn)的XML文檔,applicationContext.xml是Spring的默認(rèn)配置文件,當(dāng)容器啟動時找不到指定的配置文檔時,將會嘗試加載這個默認(rèn)的配置文件。
spring框架在一些對安全性要求較高的生產(chǎn)環(huán)境下,配置文件不允許出現(xiàn)明文用戶名密碼配置,如數(shù)據(jù)庫配置等。本文主要用于解決明文用戶名密碼加密。
通過繼承spring配置類并重寫處理方法實現(xiàn)密文解密
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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ù)解密秘鑰生成)
1
2
3
4
5
6
7
8
|
<!-- 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)容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持。
原文鏈接:http://www.open-open.com/code/view/1453520072183