JAVA,hibernate配置文件加密

来源:互联网 发布:传奇霸业经验数据 编辑:程序博客网 时间:2024/05/16 16:12


1.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">          <hibernate-configuration><session-factory><property name="dialect">org.hibernate.dialect.SQLServerDialect</property><property name="connection.url"><!-- 414E20F534FEB8B270FD9DF02F0DA99D1C918DF8CDED7E3E3933110B5E802474283418AAA2A7F28F16BCAD898CCE81F80A8D3E7328FADD702757C425E1012BBB --></property><property name="connection.username"><!-- 45D9B350F2020A1C65CC1D57903A98F7 --></property><property name="connection.password"><!-- 9DC0AB340B2201765A9B34AF567064B9 --></property><property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property><property name="connection.provider_class">com.softicloud.generic.configencrypt.CustomDriverManagerConnectionProvider  //这个是解析回填的类,以下有介绍</property><property name="myeclipse.connection.profile">breakdown</property><property name="hibernate.show_sql">true</property><property name="hibernate.format_sql">true</property><!-- <mapping resource="com/imcc/breakdown/entity/mapping/CMember.hbm.xml" /> --></session-factory></hibernate-configuration>

2.解析配置文件,并且回填访问数据库package com.softicloud.generic.configencrypt;import java.util.Properties;import org.hibernate.HibernateException;import org.hibernate.cfg.Environment;import org.hibernate.connection.C3P0ConnectionProvider;import com.softicloud.generic.safe.aes.AESEncrypt;import com.softicloud.generic.scope.Scope;public class CustomDriverManagerConnectionProvider extends C3P0ConnectionProvider {/*** //启动读取*/@Overridepublic void configure(Properties props) throws HibernateException {/*String url =props.getProperty(Environment.URL); //获取hibernate配置文件的地址String user = props.getProperty(Environment.USER);  //获取Hibernate配置文件用户名        String password = props.getProperty(Environment.PASS);   //获取hibernate配置文件密码*/                String url=Scope.getInitialLise().readValue("/config/softicloud/generic/properties/hibernate/jdbcConfig.properties", "connection.url");        String user=Scope.getInitialLise().readValue("/config/softicloud/generic/properties/hibernate/jdbcConfig.properties", "connection.username");        String password=Scope.getInitialLise().readValue("/config/softicloud/generic/properties/hibernate/jdbcConfig.properties", "connection.password");        //解密用户名,添加回去读取连接数据库        props.setProperty(Environment.USER,AESEncrypt.decode(user, Password.PWD));          //解密密码,添加回去读取连接数据库        props.setProperty(Environment.PASS, AESEncrypt.decode(password, Password.PWD));          //解密地址,添加回去读取连接数据库        props.setProperty(Environment.URL, AESEncrypt.decode(url, Password.PWD));                 //把所有加密地址解密完了,添加回去        super.configure(props);  }}



0 0