2013-09-05 86 views
4

我在加密數據庫密碼時遇到問題hibernate.cfg.xml無法在配置文件中加密密碼

這是我的屬性文件。

<!-- Database connection settings --> 
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property> 
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=TEST;</property> 
<property name="connection.username">sa</property> 
<!-- Encryption --> 
<property name="connection.password">ENC(vMO/j5jfpaU2cUhPVoOk5Q==)</property> 
<property name="connection.provider_class">org.jasypt.hibernate4.connectionprovider.EncryptedPasswordDriverManagerConnectionProvider</property> 
<property name="connection.encryptor_registered_name">hibernateEncryptor</property> 

然後在HiberanteUtil.java我有這個

// Builds session factory. 
private static SessionFactory configureSessionFactory() 
    throws HibernateException { 

    Configuration configuration = new Configuration().configure(); 
    StandardPBEStringEncryptor encryptor = 
     new StandardPBEStringEncryptor(); 
    encryptor.setPassword("pass"); 

    HibernatePBEEncryptorRegistry registry = 
     HibernatePBEEncryptorRegistry.getInstance(); 

    registry.registerPBEStringEncryptor("hibernateEncryptor", encryptor); 

    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder() 
     .applySettings(configuration.getProperties()).buildServiceRegistry(); 

    return configuration.buildSessionFactory(serviceRegistry); 
} 

我已經encrypt.bat創建加密的密碼。

然後錯誤我已經是

com.microsoft.sqlserver.jdbc.SQLServerException:登錄失敗,用戶 '山'。 ClientConnectionId:8033573f-5f52-4fe9-A728-fbe4f57d89c4

如果我刪除了這部分

StandardPBEStringEncryptor encryptor = 
     new StandardPBEStringEncryptor(); 
encryptor.setPassword("someKey"); 
HibernatePBEEncryptorRegistry registry = 
     HibernatePBEEncryptorRegistry.getInstance(); 

registry.registerPBEStringEncryptor(
     "hibernateEncryptor", encryptor); 

我有同樣的錯誤,所以我覺得它不註冊,但我不知道如何做到這一點。

我這是怎麼加密

jasypt problem image

UPDATE

我可以做得到它的工作是這樣的,但不是我的思考方式的唯一的事情。

StandardPBEStringEncryptor encryptor = 
       new StandardPBEStringEncryptor(); 
     encryptor.setPassword("somePass"); 
     encryptor.setAlgorithm("PBEWITHMD5ANDDES"); 
     String pass=encryptor.decrypt("HhpmA/XmJoLro8TYYu4YyA=="); 
     HibernatePBEEncryptorRegistry registry = 
       HibernatePBEEncryptorRegistry.getInstance(); 
     registry.registerPBEStringEncryptor(
       "hibernateEncryptor", encryptor); 

     Configuration configuration = new Configuration().configure() 
       .setProperty("hibernate.connection.encryptor_registered_name","hibernateEncryptor") 
       .setProperty("hibernate.connection.password",pass); 

所以,我認爲這個問題是與"hibernateEncryptor",我想我需要註冊

<typedef name="encryptedString" class="org.jasypt.hibernate4.type.EncryptedStringType"> 
    <param name="encryptorRegisteredName">hibernateEncryptor</param> 
    <typedef> 

但是,當我把它放在hibernate.cfg.xml說無效的映射,所以我把它添加到與註解類但沒有任何事情發生,因爲我認爲這是在我想要加密的數據庫連接之後讀取的。 :(

@TypeDef(name="encryptedString",typeClass=org.jasypt.hibernate4.type.EncryptedStringType.class, 
     parameters= {@Parameter(name="encryptorRegisteredName",value="hibernateEncryptor")}) 
+0

您可以指定Hibernate的版本和Jasypt版本? –

+0

@Haim'hibernate-4.1'和'jasypt 1.9.1'' jasypt-hibernate4-1。91' – nachokk

回答

5

這不是做,而是解決有道

StandardPBEStringEncryptor encryptor =new StandardPBEStringEncryptor(); 
encryptor.setPassword("somePass"); 
encryptor.setAlgorithm("PBEWITHMD5ANDDES"); 
Configuration configuration = new Configuration().configure(); 
String pass=encryptor.decrypt(configuration.getProperty("hibernate.connection.password")); 
configuration.setProperty("hibernate.connection.password",pass); 

而且在hibernate.cfg

<property name="connection.username">sa</property> 
    <property name="connection.password">Nzuyhu5PJJwsVH3mdw==</property> 
2

你可以試試這個:

StandardPBEStringEncryptor strongEncryptor = new StandardPBEStringEncryptor(); 
strongEncryptor.setPassword("jasypt"); 
strongEncryptor.setAlgorithm("PBEWITHMD5ANDDES"); 
HibernatePBEEncryptorRegistry registry =       HibernatePBEEncryptorRegistry.getInstance(); 
registry.registerPBEStringEncryptor("strongHibernateStringEncryptor", strongEncryptor); 

Configuration configuration = new Configuration(); 
configuration.configure("hibernate.cfg.xml"); 
configuration.setProperty("hibernate.connection.password", strongEncryptor.decrypt(configuration.getProperty("hibernate.connection.password"))); 
ServiceRegistryBuilder serviceRegistryBuilder = new ServiceRegistryBuilder().applySettings(configuration.getProperties()); 
sessionFactory = configuration.buildSessionFactory(serviceRegistryBuilder.buildServiceRegistry()); 
+0

+1實際上,與我正在做的類似,但你採取了通過xml獲取屬性的方法,以便在不重建的情況下更改。如果以這種方式進行操作,則不需要「HibernatePBEEncryptor」。 – nachokk