Cannot encrypt password in configuration file

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

I'm having trouble encrypting the database password in hibernate.cfg.xml

This is my property file.

 com.microsoft.sqlserver.jdbc.SQLServerDriverjdbc:sqlserver://localhost:1433;databaseName=TEST;saENC(vMO/j5jfpaU2cUhPVoOk5Q==)org.jasypt.hibernate4.connectionprovider.EncryptedPasswordDriverManagerConnectionProviderhibernateEncryptor

Then in the HiberanteUtil.java I have this

// 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); }

I've created the encrypted password with encrypt.bat.

Then the error i have is

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'sa'. ClientConnectionId:8033573f-5f52-4fe9-a728-fbe4f57d89c4

If I remove this part

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

I have the same error, so I think it doesn't register but I have no idea how to do it.

This is how i encrypt

UPDATE

The only thing i can made to get it work is something like this, but is not the way i think.

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);

So i think the problem is with the "hibernateEncryptor", i think i need to register

  hibernateEncryptor   

But when i put it in hibernate.cfg.xml says invalid mapping, so i add it to a class with annotation but nothing happen cause i think this is read after database connection that is what i want to encrypt. :(

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

回答1:

This is not the proper way to do it but solves.

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);   

And in hibernate.cfg

    saNzuyhu5PJJwsVH3mdw==


回答2:

You might try this:

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());


回答3:

http://www.jasypt.org/hibernate.html

Why not switch algorithms to: PBEWithMD5AndTripleDES

Take a look at this post on StackOverflow: Error implementing Jasypt with Hibernate 3 and Struts 2



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!