如何解决当身份验证设置为 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 举报,一经查实,本站将立刻删除。