Jasypt and hibernate

0

Hi, I wanted to encrypt the key I have to the database in the file hibernate.cfg.xml, and I found Jasypt can do it, I followed these tutorials.

link

link

I used the encrypt.bat and put the parameters to return the key to the encrypted database, the configuration of my hibernate.cfg.xml file stayed this way.

<property name="connection.provider_class">org.jasypt.hibernate4.connectionprovider.EncryptedPasswordDriverManagerConnectionProvider</property>

<property name="connection.encryptor_registered_name">configurationHibernateEncryptor</property> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="connection.url">jdbc:mysql://localhost:3306/aprendiendojsf?autoReconnect=true</property> 
    <property name="connection.username">root</property> 
    <property name="connection.password">ENC(lhFCMXdRkAw7Cz5JU17lvg==)</property> 
    <property name="show_sql">true</property> 
    <property name="current_session_context_class">thread</property>

My HibernateUtil class in this way

public class HibernateUtil { 

private static SessionFactory sessionFactory = null; 

static { 
    try { 
        // Create the SessionFactory from standard (hibernate.cfg.xml)  
        // config file. 
        sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 

        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 
        encryptor.setAlgorithm("PBEWithMD5AndDES"); 
        encryptor.setPassword("clave"); 
        HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance(); 
        registry.registerPBEStringEncryptor("configurationHibernateEncryptor", encryptor); 
        } catch (Throwable ex) { 
        // Log the exception.  
        System.err.println("Initial SessionFactory creation failed." + ex); 
        ex.printStackTrace(); 
    } 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

}

My project gets up correctly, but at the time of login, this appears in the log:

INFO: HHH000046: Connection properties: {user=root, encryptor_registered_name=configurationHibernateEncryptor, password=****} 

WARN: HHH000342: Could not obtain connection to query metadata : Could not create connection to database server. Attempted reconnect 3 times. Giving up.

I do not understand what may be happening, I already have 3 days in this and I can not give, I hope you can help me, it would be a great help!

I made the following change in the Hibernate Util class, which was suggested to me.

public class HibernateUtil { 

private static SessionFactory sessionFactory = null;


static { 
try { 



  EnvironmentStringPBEConfig envConfig = new EnvironmentStringPBEConfig();
  envConfig.setPasswordEnvName(“DB_SIMETRIC_KEY”);

  sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 

  StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 
  encryptor.setAlgorithm("PBEWithMD5AndDES"); 
  encryptor.setConfig(envConfig); 

  HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance(); 
  registry.registerPBEStringEncryptor("configurationHibernateEncryptor", encryptor); 
} catch (Throwable ex) { 
  // Log the exception.  
  System.err.println("Initial SessionFactory creation failed." + ex); 
  ex.printStackTrace(); 
} 


} 
public static SessionFactory getSessionFactory() { 
return sessionFactory; 


 } 
}

However the apache console launches the same thing to me

WARN: HHH000342: Could not obtain connection to query metadata : Could not create connection to database server. Attempted reconnect 3 times. Giving up.

I thought it was easier to configure.

    
asked by Abner 05.04.2016 в 05:16
source

1 answer

0

You must define a variable at the system level with the value so that it performs the encryption of the database password. When you used encript.bat you entered a value to perform to get lhFCMXdRkAw7Cz5JU17lvg == using the indicated algorithm PBEWithMD5AndDES.

For example, I put a system variable with that value called DB_SIMETRIC_KEY = your value .

Now the class becomes like this:

public class HibernateUtil { 

  private static SessionFactory sessionFactory = null; 

  static { 
    try { 

      EnvironmentStringPBEConfig envConfig = new EnvironmentStringPBEConfig();
      envConfig.setPasswordEnvName(“DB_SIMETRIC_KEY”);

      sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); 

      StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); 
      encryptor.setAlgorithm("PBEWithMD5AndDES"); 
      encryptor.setConfig(envConfig); 

      HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance(); 
      registry.registerPBEStringEncryptor("configurationHibernateEncryptor", encryptor); 
    } catch (Throwable ex) { 
      // Log the exception.  
      System.err.println("Initial SessionFactory creation failed." + ex); 
      ex.printStackTrace(); 
    } 
  } 

  public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
  } 

}

Start your application and set the value of the system variable to null. This way lhFCMXdRkAw7Cz5JU17lvg == will be decrypted by its symmetric key DB_SIMETRIC_KEY getting the value the password with which you connect.

    
answered by 06.04.2016 в 22:22