【问题标题】:Is it possible to create a jdbc connection without a password (using postgresql 'trust')?是否可以在没有密码的情况下创建 jdbc 连接(使用 postgresql 'trust')?
【发布时间】:2010-05-14 09:11:32
【问题描述】:

我正在使用 jdbc 连接到 java 应用程序中的 postgresql 数据库(实际上该应用程序是用 Groovy 编写的)。我已将 postgresql 设置为使用“信任”身份验证方法。是否可以在不指定密码的情况下打开 jdbc 连接?当我尝试使用带有空白密码的普通构造函数时,它会失败并显示

Exception in thread "Thread-2" org.postgresql.util.PSQLException: FATAL: password authentication failed for user "myuser"

即使在命令行中,这也可以正常工作

psql -U myuser mydatabase
Welcome to psql 8.3.5, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms
      \h for help with SQL commands
      \? for help with psql commands
      \g or terminate with semicolon to execute query
      \q to quit

【问题讨论】:

  • 首先使用 psql 检查身份验证,但来自您尝试连接 JDBC 的同一台机器 - 我感觉“信任”身份验证仅针对本地连接启用,您正在尝试从另一台机器连接 JDBC。
  • @Milen“信任”身份验证(即无密码)也可用于 ip 连接
  • @leonbloy - 请重新阅读我写的内容。

标签: java postgresql jdbc


【解决方案1】:

是的,你可以。

请注意,Postres JDBC 驱动程序始终使用 IP 套接字(pg_hba.conf 中的host),即使数据库在本地计算机上,psql 也可以使用本地套接字(local 中的pg_hba.conf)。因此,如果psqltrust 身份验证一起使用而JDBC 不能,您可能应该为IP 套接字配置trust 身份验证see documentation

【讨论】:

  • 感谢您的链接。那么我可以使用普通的 jdbc 构造函数并只传递一个空白密码吗?
  • 我添加了一个新行,例如:host DATABASE USERNAME 127.0.0.1/32 trust,但我不得不删除上面的这一行:host all all 127.0.0.1/32 md5
【解决方案2】:

已经回答但是:

当您使用psql 客户端程序进行连接并且不指定主机 (-h) 时,默认是使用 本地套接字(至少在 Linux 中)。相反,在 JDBC 中,您将使用 TCP/IP 套接字。然后,要检查您的连接问题,您应该使用您在 JDBC 中使用的相同设置调用 psql,包括主机。

例如

 psql -U myuser -h 127.0.0.1 mydatabase    # uses TCP/IP

不一样的

 psql -U myuser mydatabase      # uses local socket (non TCP/IP)

【讨论】:

    【解决方案3】:

    GlassFish 至少包括 3.1 版有一个issue with specifying empty JDBC passwords

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-10
      • 2018-01-19
      • 2014-06-16
      • 2011-10-18
      • 2019-04-06
      • 1970-01-01
      • 1970-01-01
      • 2012-01-08
      相关资源
      最近更新 更多