問題:
java 的ssm對jdbc.properties檔案的資料庫賬號密碼進行加密,現在問題是沒有進行加密時,可以正常的連接查詢;加密后可以正常的啟動連接,但是一查詢就不能查詢,加密的賬號密碼沒有進行解密,
報錯資訊:
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '3wir5v+MgYH2IqEC1j4hDUggrXgXVC2r/Ah4Tm5aqZQ=.encryption'
### The error may exist in file [D:\Java\apache-tomcat-7.0.90\webapps\DataLink\WEB-INF\classes\com\aa\api2\mapper\CustomerMapper_yh.xml]
### The error may involve com.aa.api2.dao.ICustomerDao2.getAllCustomer
### The error occurred while executing a query
### Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '3wir5v+MgYH2IqEC1j4hDUggrXgXVC2r/Ah4Tm5aqZQ=.encryption'
下面是專案的相關資訊:
1、jdbc.properties檔案:
#資料源2
jdbc.driver2=3wir5v+MgYH2IqEC1j4hDUggrXgXVC2r/Ah4Tm5aqZQ=.encryption
jdbc.url2=JJjXfBGoZqt26mFQLlvLGROAOFUUkeoNTZmekjWDYbtXSDJxdBVSnvOJ0wPaX/DK.encryption
jdbc.username2=dGN8ovU9LpfA0omlTZ1img==.encryption
jdbc.password2=/5kb+M7G3ivarVFSVn6SYg==.encryption
2、applicationContext.xml的配置資訊:
<!-- 引入組態檔 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="https://bbs.csdn.net/topics/classpath:jdbc.properties" />
<property name="fileEncoding" value="https://bbs.csdn.net/topics/utf-8"/>
</bean>
<bean class="com.aa.common.DBUtil.EncrypPropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="https://bbs.csdn.net/topics/true"/>
<property name="ignoreResourceNotFound" value="https://bbs.csdn.net/topics/true"/>
<!-- <property name="locations" value="https://bbs.csdn.net/topics/classpath:jdbc.properties"/> -->
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<!-- 資料源2 開始 -->
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="https://bbs.csdn.net/topics/${jdbc.driver2}" />
<property name="url" value="https://bbs.csdn.net/topics/${jdbc.url2}" />
<property name="username" value="https://bbs.csdn.net/topics/${jdbc.username2}" />
<property name="password" value="https://bbs.csdn.net/topics/${jdbc.password2}" />
<!-- 初始化連接大小 -->
<property name="initialSize" value="https://bbs.csdn.net/topics/${jdbc.initialSize}"/>
<!-- 連接池最大數量 -->
<property name="maxActive" value="https://bbs.csdn.net/topics/${jdbc.maxActive}"/>
<!-- 連接池最大空閑 -->
<property name="maxIdle" value="https://bbs.csdn.net/topics/${jdbc.maxIdle}"/>
<!-- 連接池最小空閑 -->
<property name="minIdle" value="https://bbs.csdn.net/topics/${jdbc.minIdle}"/>
<!-- 獲取連接最大等待時間 -->
<property name="maxWait" value="https://bbs.csdn.net/topics/${jdbc.maxWait}"/>
</bean>
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射檔案 -->
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean"
p:dataSource-ref="dataSource2"
p:mapperLocations="classpath:com/aa/api2/mapper/*.xml" />
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="cn.aa.api.mapper" p:sqlSessionFactoryBeanName="sqlSessionFactory2" />
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager2"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource2" />
<!-- 資料源2 結束 -->
<!-- 資料源2 -->
<!-- DAO介面所在包名,Spring會自動查找其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--basePackage指定要掃描的包,在此包之下的映射器都會被搜索到。
可指定多個包,包與包之間用逗號或分號分隔-->
<property name="basePackage" value="https://bbs.csdn.net/topics/com.aa.api2.dao" />
<property name="sqlSessionFactoryBeanName" value="https://bbs.csdn.net/topics/sqlSessionFactory2"></property>
</bean>
3、EncrypPropertyPlaceholderConfigurer.java檔案:
public class EncrypPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
Properties props) throws BeansException {
/**資料源2 開始 */
String driver2 = props.getProperty("jdbc.driver2");
driver2 = driver2.substring(0, driver2.length() - 11);
if (driver2 != null) {// 將加密的driver2解密后塞到props
props.setProperty("jdbc.driver2",AESCoderUtils.AESDncode(driver2));
}
String url2 = props.getProperty("jdbc.url2");
url2 = url2.substring(0, url2.length() - 11);
if (url2 != null) {// 將加密的url2解密后塞到props
props.setProperty("jdbc.url2",AESCoderUtils.AESDncode(url2));
}
String username2 = props.getProperty("jdbc.username2");
username2 = username2.substring(0, username2.length() - 11);
if (username2 != null) {// 將加密的username解密后塞到props
props.setProperty("jdbc.username2",AESCoderUtils.AESDncode(username2));
}
String password2 = props.getProperty("jdbc.password2");
password2 = password2.substring(0, password2.length() - 11);
if (password2 != null) {
props.setProperty("jdbc.password2",AESCoderUtils.AESDncode(password2));
}
/**資料源2 結束*/
super.processProperties(beanFactoryToProcess, props);
}
}
4、AESCoderUtils.java檔案:
public class AESCoderUtils {
/*
* 加密
*/
public static String AESEncode(String content) {
try {
// 1.構造密鑰生成器,指定為AES演算法,不區分大小寫
KeyGenerator keygen = KeyGenerator.getInstance("AES");
// xxx 為密鑰
keygen.init(128, new SecureRandom("xxx".getBytes()));
SecretKey original_key = keygen.generateKey();
byte[] raw = original_key.getEncoded();
SecretKey key = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] byte_encode = content.getBytes("utf-8");
byte[] byte_AES = cipher.doFinal(byte_encode);
String AES_encode = new String(Base64.encodeBase64(byte_AES));
return AES_encode+".encryption";
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}
/*
* 解密
*/
public static String AESDncode(String content) {
try {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
keygen.init(128, new SecureRandom("xxx".getBytes()));
SecretKey original_key = keygen.generateKey();
byte[] raw = original_key.getEncoded();
SecretKey key = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] byte_content = Base64.decodeBase64(content);
byte[] byte_decode = cipher.doFinal(byte_content);
String AES_decode = new String(byte_decode, "utf-8");
return AES_decode;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println("加密后:"+AESEncode("TIGER"));
System.out.println("解密后:"+AESDncode("3wir5v+MgYH2IqEC1j4hDUggrXgXVC2r/Ah4Tm5aqZQ=.encryption"));
}
}
uj5u.com熱心網友回復:
解決了嗎?記得分享,謝謝!uj5u.com熱心網友回復:
把密碼列印出來看看 看看解密正確了沒有uj5u.com熱心網友回復:
樓主解決了,我已經解決了,樓主配置錯了,1.id=propertyConfigurer的bean的class換成自定義的類,
2.下面那個<bean class="com.aa.common.DBUtil.EncrypPropertyPlaceholderConfigurer">的bean洗掉
uj5u.com熱心網友回復:
jasypt了解一下轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/19310.html
標籤:Java相關
上一篇:有sql例外,但是已經注釋掉了connection1.rollback();即根本沒有用代碼做回滾,為什么資料無法插入
