我在加密數據庫密碼時遇到問題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);
我有同樣的錯誤,所以我覺得它不註冊,但我不知道如何做到這一點。
我這是怎麼加密
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")})
您可以指定Hibernate的版本和Jasypt版本? –
@Haim'hibernate-4.1'和'jasypt 1.9.1'' jasypt-hibernate4-1。91' – nachokk