hibernate配置文件中数据库密码加密

yuzhoulangzi102423 2011-04-26 03:08:44
这是我的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 {
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中的时候就设置不进去了。不知道是什么原因,帮帮忙呀。谢谢。
...全文
1007 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hanyile 2011-09-18
  • 打赏
  • 举报
回复
在吗,我也有个问题
yuzhoulangzi102423 2011-04-27
  • 打赏
  • 举报
回复
谢谢那位jason941983大哥,这个问题困扰了我一个礼拜。今天终于搞定了。呵呵。
jason941983 2011-04-27
  • 打赏
  • 举报
回复
问题的原因,看了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记得给分哈。

yuzhoulangzi102423 2011-04-26
  • 打赏
  • 举报
回复
引用的都对着了呀,我是已经可以得到数据了。就是设置不进去
yuzhoulangzi102423 2011-04-26
  • 打赏
  • 举报
回复
保证我的数据的安全么。就是因为要加密才出现这样的结果的。自己弄了好几天了,就是弄不好。
teddy000 2011-04-26
  • 打赏
  • 举报
回复
引用的不对吧,或者是不兼容
小绵羊 2011-04-26
  • 打赏
  • 举报
回复
。。。飘过,话说有必要加密么

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧