当身份验证设置为 md5 时,Hibernate 无法连接到 Postgres 数据库

问题描述

我使用的是 Hibernate 5 和 Postgres 12.5。我正在尝试使用 md5 散列密码进行身份验证。 当我的应用程序尝试使用 md5 身份验证进行连接时,它不起作用并说这是错误的密码。我可以使用密码通过 psql 登录,并且工作正常。我还使用jasypt(加密和解密密码的工具)对存储在hibernate.cfg.xml 文件中的密码进行了解密,以检查加密的密码是什么,这也是正确的。如果我将 postgres 配置设置为信任它就可以正常工作。

我的 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);
         }
     }

如果我能提供更多信息,请告诉我。并感谢您提供的任何帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)