这是我的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>
这是我的sessionfactorypackage 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 {
Session session = threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
 * Close the single hibernate session instance.
 * 
 * @throws HibernateException
 */
public static void closeSession() throws HibernateException {
Session session = threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
 * return session factory
 * 
 * session factory will be rebuilded in the next call
 */
public static void setConfigFile(String configFile) {
SessionFactory.configFile = configFile;
sessionFactory = null;
} public static Configuration getConfiguration() {
return configuration;
}}
主要就是刚开始的 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();
}
}
可以得到加密后的密码,然后解密,也可以解出来,再设置到hibernate中的时候就设置不进去了。不知道是什么原因,帮帮忙呀。谢谢。

解决方案 »

  1.   

    问题的原因,看了Configuration源码,对它什么时候去读的properties属性也一直不解。
    不去管它了,不过以下步骤,是可以解决你的问题的。
    解决思路是:跳过让Configuration直接与Hibernate配置文件直接打交道。1.将Hibernate配置文件读到document里
    2.解密放回到document里
    3.myconfiguration.configure(doc);
    4.sessionFactory = myconfiguration.buildSessionFactory();
    要注意的是:
    1.读Hibernate配置文件的方法,借用org.hibernate.cfg.Configuration类里的读的方法。
    它是用xmlHelper.createSAXReader(...)来读的。为了不让你偷懒,就不具体写了。呵呵。因为如果用普通的方式去读Hibernate配置文件方法时,会因有<!DOCTYPE >读不成功或者会去连网络。2.如果Document你是用dom4j的话,
    在第3步的时候,就要写一个org.hibernate.cfg.Configuration的子类,然后重写一个configure(Dcoument)方法,因为org.hibernate.cfg.Configuration的configure(Dcoument)的参数是w3c的Document-------------------------
    LZ记得给分哈。
      

  2.   

    谢谢那位jason941983大哥,这个问题困扰了我一个礼拜。今天终于搞定了。呵呵。