在hibernate.cfg.xml中加密hibernate密码

时间:2017-11-02 08:00:51

标签: java spring hibernate

我想使用Jasypt加密配置文件中的密码。为此,我使用library based in jasypt,因为使用hibernate 4.3时似乎存在一些问题。

密码存储在名为PSS_RES的环境变量中,并使用PBEWithMD5AndTripleDes算法进行编码

配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
  <session-factory> 
    <property name="connection.provider_class">net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedDriverManagerConnectionProviderImpl</property> 
    <property name="connection.encryptor_registered_name">configurationHibernateEncryptor</property>  

    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost/DB</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">ENC(RcnYVTTYFbrVB346Lp4txlbj2oI0TMglCtQ3GNvopFU=)</property>
    <property name="hibernate.connection.pool_size">1</property>

    <property name="hibernate.current_session_context_class">thread</property>

</session-factory>

的HibernateUtil

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;
import org.jasypt.hibernate4.encryptor.HibernatePBEEncryptorRegistry;
public class HibernateUtil {
private static final SessionFactory sessionFactory =buildSessionFactory();

private static SessionFactory buildSessionFactory() {    
    try {
        // TODO Auto-generated method stub
        EnvironmentStringPBEConfig envConfig = new EnvironmentStringPBEConfig();
        envConfig.setPasswordEnvName("PSS_RES");
        System.out.println(envConfig.getPasswordEnvName());
        System.out.println(envConfig.getPassword());
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 
        HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance(); 
        encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); 
        encryptor.setConfig(envConfig); 

        registry.registerPBEStringEncryptor("configurationHibernateEncryptor", encryptor); 

        Configuration configuration=new Configuration();
        configuration.configure("hibernate.cfg.xml");
        ServiceRegistry serviceRegistry=new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        SessionFactory  sessionFactory= configuration.buildSessionFactory(serviceRegistry);             

        return sessionFactory;

    } catch (Throwable e) {
        // TODO: handle exception
        System.err.println("error en la conexion "+e );
        throw new ExceptionInInitializerError();
    }

}

public static SessionFactory getSessionfactory() {
    return sessionFactory;
}   

}

这是我启动应用程序时遇到的异常。

[main] INFO net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedDriverManagerConnectionProviderImpl - configure :  Using Encryptor registered with name: configurationHibernateEncryptor
[main] INFO net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedDriverManagerConnectionProviderImpl - configure :  Found registered Encryptor, checking connection properties ..
[main] INFO net.lizalab.util.jasypt.h4.ext.connectionprovider.EncryptedDriverManagerConnectionProviderImpl - decryptIfEncrypted :  Decrypting value for encrypted property: hibernate.connection.password
error en la conexion org.jasypt.exceptions.EncryptionOperationNotPossibleException
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.Eu.controladores.HibernateUtil.buildSessionFactory(HibernateUtil.java:37)
at com.Eu.controladores.HibernateUtil.<clinit>(HibernateUtil.java:11)
at com.Eu.dao.PersonaDao.listaPersonas(PersonaDao.java:47)
at com.Eu.paneles.PanelFiltros.RellenarTablaPersonas(PanelFiltros.java:382)
at com.Eu.paneles.PanelFiltros.<init>(PanelFiltros.java:84)
at com.Eu.formularios.FrmPrincipal.<init>(FrmPrincipal.java:126)
at com.Eu.controladores.Application.main(Application.java:11)

0 个答案:

没有答案