hibernate的hibernate.cfg.xml的用户名密码加密

原文:http://blog.csdn.net/sdbany/article/details/3132809

hibernate的cfg.xml配置文件用户名密码是明文存放,配置文件里的参数信息解析都是通过provider_class负责。定义一个子类继承provider_class连接供应类。


1、这里我用的是c3p0的连接供应类,故要继承C3P0ConnectionProvider

package utils;

import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.c3p0.internal.C3P0ConnectionProvider;
import org.hibernate.cfg.Environment;

public class CustomDriverManagerConnectionProvider extends C3P0ConnectionProvider {
	
	public CustomDriverManagerConnectionProvider() {
		super();
	}
	/*
	* (non-Javadoc)
	* 
	* @see org.hibernate.connection.DriverManagerConnectionProvider#configure(java.util.Properties)
	*/
	@Override
	public void configure(Map props) throws HibernateException {
		String user = (String)props.get(Environment.USER);
		String password = (String)props.get(Environment.PASS);
		if(ToolUtils.stringIsNull(user)){
			 throw new HibernateException("数据库帐号不能为空!");
		}
		if(ToolUtils.stringIsNull(password)){
			 throw new HibernateException("数据库密码不能为空!");
		}
		//解密
		AESUtils aes = new AESUtils();
		props.put(Environment.USER, aes.decode(user));
		props.put(Environment.PASS, aes.decode(password));
		super.configure(props);
	}
}


重写configure方法,并在configure中对用户名、密码进行解密(可以随意用任何加解密方式),再调用父类的configure方法


2、把自定义的连接提供类配置到hibernate.cfg.xml

<property name="hibernate.connection.provider_class">utils.CustomDriverManagerConnectionProvider</property>



求大神支招啊。。。 **这是的hibernate.xml** <?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"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property> <property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:orcl</property> <!-- c3lzdGVt cGFuaGFv --> <property name="connection.username">c3lzdGVt</property> <property name="connection.password">cGFuaGFv</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="myeclipse.connection.profile">a</property> <mapping resource="pojo/Nametable.hbm.xml" /> </session-factory> </hibernate-configuration> **这是的Sessionfactory里面加载hibernate.xml的部分** 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中的时候就设置不进去了。不知道是什么原因,帮帮忙呀。谢谢。
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页