我在hibernate.cfg.xml中加密数据库密码时遇到问题
这是我的属性文件.
com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc:sqlserver://localhost:1433;databaseName=TEST;
sa
ENC(vMO/j5jfpaU2cUhPVoOk5Q==)
org.jasypt.hibernate4.connectionprovider.EncryptedPasswordDriverManagerConnectionProvider
hibernateEncryptor
然后在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: Login failed for user
‘sa’. 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”,我想我需要注册
hibernateEncryptor
但是,当我把它放在hibernate.cfg.xml中说无效映射时,所以我将它添加到带注释的类但没有任何事情发生因为我认为这是在数据库连接之后读取的,这是我想要加密的. 🙁
@TypeDef(name="encryptedString",typeClass=org.jasypt.hibernate4.type.EncryptedStringType.class,
parameters= {@Parameter(name="encryptorRegisteredName",value="hibernateEncryptor")})