如何使用SCRAM-SHA-256在PostgresSQL中通过jdbc连接数据库

问题描述 投票:1回答:1

有我正在尝试在jdbc中测试SCRAM-SHA-256。

  public static void main(String[] args) throws SQLException {
        String url = "jdbc:postgresql://192.168.56.101:1521/gisdb";
        Properties props = new Properties();
        props.setProperty("user", "pguser");
        String scram = "SCRAM-SHA-256$4096:QNTBYMpbvZjbOx5RLM7rvA==$tuBD7Ek7niK8jyiuwjCGLH2EurqMNoIaclZhOXohyik=:2b0Ow951/1OPKsqzaGLWPyT+wVIXOs9dvY8TF2eTCVM=";
        props.setProperty("password", scram);
//        props.setProperty("password", "*******");

        Connection conn = DriverManager.getConnection(url, props);
        String databaseProductVersion = conn.getMetaData().getDatabaseProductVersion();
        System.out.println(databaseProductVersion);
    }

我的pg_hba.conf

hostssl all             all             192.168.0.0/16          scram-sha-256
#hostssl all             all             192.168.0.0/16          trust

select rolname,rolpassword from pg_authid;它显示:

          rolname          |                                                              rolpassword
---------------------------+---------------------------------------------------------------------------------------------------------------------------------------
 pg_monitor                |
 pg_read_all_settings      |
 pg_read_all_stats         |
 pg_stat_scan_tables       |
 pg_read_server_files      |
 pg_write_server_files     |
 pg_execute_server_program |
 pg_signal_backend         |
 pguser                    | SCRAM-SHA-256$4096:PZNbiF6I5G1SVcoN9sTjJw==$xe8jrBS9iUn0ldoIV8moaAod06sYRbxsyyQaUbiuSQE=:2tpLS+eL1brme0Il0wcnsllkDBfDkaQ/II7iVJ3ecxM=
 test                      |
 postgres                  | SCRAM-SHA-256$4096:o1lyjT/acTglIuLsp6TF3Q==$pdWLZ8DuceZDwr9jla0WPzXXa3N3kWrjh9cPnrloP3w=:6hd8Ib7Od+ZZenItVoH4L+26oSiBxqp63WxO82PeonM=

当我使用java代码中的scram-sha-256加密密码字符串或prue密码进行连接时,在日志文件中显示FATAL: password authentication failed for user "pguser"。当我在trust中将方法更改为pg_hba.conf时,它可以工作。

如何使用scram-sha-256方法进行密码连接?我的JDBC驱动程序版本为postgresql-42.2.12.jar

java postgresql jdbc
1个回答
1
投票

您必须提供明文密码,而不是SCRAM哈希。

© www.soinside.com 2019 - 2024. All rights reserved.