我正在使用Hibernate5和Postgres12.5。我正在尝试使用md5哈希密码进行身份验证。当我的应用程序尝试使用md5身份验证进行连接时,它不起作用,并且说是错误的密码。我可以通过psql登录使用密码,它的作品很好。我还使用jasypt(加密和解密密码的工具)解密了hibernate.cfg.xml文件中存储的密码,以检查加密的密码是什么,这也是正确的。如果我将postgres配置设置为trust,它就可以正常工作。
我的hibernate.cfg.xml文件如下所示:
<session-factory>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/mydatabasename</property>
<property name="hibernate.connection.username">myusername</property>
<property name="hibernate.connection.password">ENC(5aCn6qexVnhdt6BVIkqROYITzwxp9K)</property>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.connection.encryptor_registered_name">configurationHibernateEncryptor</property>
<property name="hibernate.connection.provider_class">org.jasypt.hibernate5.connectionprovider.EncryptedPasswordC3P0ConnectionProvider</property>
Other non important properties...
</session-factory>
</hibernate-configuration>
我的pg.hba.conf如下:
host all postgres ::1/128 md5
host all postgres localhost md5
host databasename myusername all md5
host databasename myusername 127.0.0.1/32 md5
host databasename myusername ::1/128 md5
我的hibernateutil.java如下:
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.hibernate5.encryptor.HibernatePBEEncryptorRegistry;
public class HibernateUtil
{
private static Logger logger = Logger.getLogger(HibernateUtil.class);
private static SessionFactory sessionFactory;
private static Configuration configuration = null;
private HibernateUtil()
{
}
static
{
try
{
StandardPBEStringEncryptor senc = new StandardPBEStringEncryptor();
if (senc != null)
{
senc.setAlgorithm("PBEWithMD5AndDES");
senc.setPassword("privatekey");
HibernatePBEEncryptorRegistry hibReg = HibernatePBEEncryptorRegistry.getInstance();
if (hibReg != null)
{
hibReg.registerPBEStringEncryptor("configurationHibernateEncryptor", senc);
}
else
{
logger.error(new Throwable().fillInStackTrace().getStackTrace()[0].getMethodName()+
" Unable to register encryptor.");
}
configuration = new Configuration();
sessionFactory = configuration.configure().buildSessionFactory();
}
}
catch (Throwable ex)
{
logger.error(new Throwable().fillInStackTrace().getStackTrace()[0].getMethodName()+
" Initial SessionFactory creation failed. " + ex);
throw new ExceptionInInitializerError(ex);
}
}
如果有任何更多的信息,我可以提供请让我知道。谢谢你的帮助。
暂无答案!
目前还没有任何答案,快来回答吧!