日期:2014-05-20  浏览次数:21035 次

hibernate配置文件中数据库密码加密
这是我的hibernate配置文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!--7B9Vd2+uU37of7S0vSjb/A== -->
<hibernate-configuration>

<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/proxy_test
</property>
<property name="connection.username">root</property>
<property name="connection.password">7B9Vd2+uU37of7S0vSjb/A==</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
 
<mapping resource="com/techown/tbproxy/model/Process.hbm.xml" />

<!--用户管理和设备管理 -->
<mapping resource="com/techown/tbproxy/model/Device.hbm.xml" />
<mapping resource="com/techown/tbproxy/model/Users.hbm.xml" />
<mapping resource="com/techown/tbproxy/model/Flowses.hbm.xml" />
<mapping resource="com/techown/tbproxy/model/Publish.hbm.xml" />
<mapping resource="com/techown/tbproxy/model/Mapper.hbm.xml" />
</session-factory>

</hibernate-configuration>


这是我的sessionfactory

package com.techown.tbproxy.util;

import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import com.techown.proxy.inner.appoint.SimpleDESCrypto;


public class SessionFactory {


private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
private static String configFile = CONFIG_FILE_LOCATION;

static {
try {
configuration.configure(configFile);
  //得到hibernate配置文件中的密码
String password = configuration.getProperty("connection.password");
System.out.println("//-配置文件中password:"+password);
//调用密钥解密
String keyword = SimpleDESCrypto.APPOINT_KEY_WORD;
//先用base64解码
byte[] data = SimpleDESCrypto.decodeByBase64(password);
//decrypt解密
String realPassword = new String(SimpleDESCrypto.decrypt(keyword, data));
System.out.println("//---解密后的密码-------realPassword:"+realPassword);
//将解密后的密码设置到hibernate配置文件中
configuration.setProperty("connection.password", realPassword);

String pass=configuration.getProperty("connection.password");
System.out.println("从配置文件中得到的密码"+pass);

sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}

private SessionFactory() {
}

/**
* Returns the ThreadLocal Session instance. Lazy initialize the
* <code>SessionFactory</code> if needed.

* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {