简体   繁体   English

Postgresql JDBC Driver是否支持Pgpass身份验证?

[英]Does the Postgresql JDBC Driver support Pgpass authentication?

I am trying to connect to a Postgresql database from Java using the JDBC Driver and would like to use pgpass to authenticate. 我正在尝试使用JDBC驱动程序从Java连接到Postgresql数据库,并希望使用pgpass进行身份验证。

My Postgresql server is correctly setup for password authentication and I have a local .pgpass file; 我的Postgresql服务器已正确设置密码验证,我有一个本地.pgpass文件; I can connect using psql without having to provide the user's password. 我可以使用psql连接,而无需提供用户的密码。 However, when I try to open a connection in Java, I get the following error: 但是,当我尝试在Java中打开连接时,我收到以下错误:

org.postgresql.util.PSQLException: The server requested password-based authentication, but no password was provided.

I am using the following code: 我使用以下代码:

String url = "jdbc:postgresql://localhost/test";
Properties props = new Properties();
props.setProperty("user","my_user");
Connection conn = DriverManager.getConnection(url, props);

My question is then: does the Postgres JDBC Driver support pgpass? 我的问题是:Postgres JDBC Driver是否支持pgpass?

The only clue I could find is in SO and seems to indicate that since the driver does not use libpq, then it cannot use pgpass . 我能找到的唯一线索是在SO中,似乎表明由于驱动程序不使用libpq,所以它不能使用pgpass I can't seem to find an authoritave answer anywhere though. 我似乎无法在任何地方找到一个authoritave答案。

No, PgJDBC doesn't read .pgpass . 不,PgJDBC不读.pgpass

$ cd projects/pgjdbc
$ git grep pgpass
$ 

Your Java program would need to open and parse .pgpass then do appropriate matching for the connection. 您的Java程序需要打开并解析.pgpass然后对连接进行适当的匹配。

If you write a class to read and parse .pgpass and to match it for a given set of connection parameters, please submit it for inclusion in the PgJDBC driver, where it'd go under the package org.postgresql.util . 如果你编写一个类来读取和解析.pgpass并为一组给定的连接参数匹配它,请提交它以包含在PgJDBC驱动程序中,它将放在包org.postgresql.util

You might find it easier to instead modify the JDBC driver directly, looking up the password if none is specified in the connection parameters once the JDBC driver has already parsed the JDBC URL into host, port, etc. 您可能会发现直接修改JDBC驱动程序更容易,如果JDBC驱动程序已将JDBC URL解析为主机,端口等,则在连接参数中未指定密码时查找密码。

Here is a tool to read password from .pgpass file in Scala, 这是一个从Scala中读取.pgpass文件密码的工具,

object DbUtil {
  def dbPassword(hostname:String, port:String, database:String, username:String ):String = {
    // Usage: val thatPassWord = dbPassword(hostname,port,database,username)
    // .pgpass file format, hostname:port:database:username:password
    val passwdFile = new java.io.File(scala.sys.env("HOME"), ".pgpass")
    var passwd = ""
    val fileSrc = scala.io.Source.fromFile(passwdFile)
    fileSrc.getLines.foreach{line =>
      val connCfg = line.split(":")
      if (hostname == connCfg(0)
        && port == connCfg(1)
        && database == connCfg(2)
        && username == connCfg(3)
      ) { 
        passwd = connCfg(4)
      }
    }
    fileSrc.close
    passwd
  }

  def passwordFromConn(connStr:String) = {
    // Usage: passwordFromConn("hostname:port:database:username")
    val connCfg = connStr.split(":")
    dbPassword(connCfg(0),connCfg(1),connCfg(2),connCfg(3))
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM