【问题标题】:Spring JPA cannot connect to PostgresqlSpring JPA 无法连接到 Postgresql
【发布时间】:2017-02-23 18:53:17
【问题描述】:

我正在尝试使用 Postgres 编写 Spring Data JPA 应用程序。

我收到一个错误:FATAL: password authentication failed for user

我尝试使用常规 JDBC 编写连接,一切正常,用户名/密码/数据库/主机名相同。

我用于 Spring Data JPA 的属性文件包含以下内容:

spring.jpa.database=POSTGRESQL
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/testdb
spring.datasource.username=test_java 
spring.datasource.password=easy_password
server.port=8080

与我用于 java jdbc 测试的属性文件相比:

db.url=jdbc:postgresql://localhost:5432/testdb
db.user=test_java
db.passwd=easy_password

Java 代码如下所示:

public void testSelectWithPropertyFile() {
    Logger lgr = Logger.getLogger(getClass().getName());
    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;

    Properties props = new Properties();
    FileInputStream in = null;

    try {
        in = new FileInputStream("target/classes/properties/database.properties");
        props.load(in);

    } catch (IOException ex) {
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
        return;
    } finally {

        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    String url = props.getProperty("db.url");
    String user = props.getProperty("db.user");
    String passwd = props.getProperty("db.passwd");

    try {

        con = DriverManager.getConnection(url, user, passwd);
        pst = con.prepareStatement("SELECT * FROM Authors");
        rs = pst.executeQuery();

        while (rs.next()) {
            System.out.print(rs.getInt(1));
            System.out.print(": ");
            System.out.println(rs.getString(2));
        }

    } catch (Exception ex) {
        lgr.log(Level.SEVERE, ex.getMessage(), ex);

    } finally {

        try {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            if (con != null) {
                con.close();
            }

        } catch (SQLException ex) {
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        }
    }
}

这是我在 Spring Boot JPA 中得到的错误日志:

2016-10-14 11:09:21.593  INFO 6948 --- [           main] com.example.JpaPostgresApplication       : Starting JpaPostgresApplication on DESKTOP-53J32BH with PID 6948 (C:\Users\charb\workspace\JPA_POSTGRES\target\classes started by charb in C:\Users\charb\workspace\JPA_POSTGRES)
    2016-10-14 11:09:21.595  INFO 6948 --- [           main] com.example.JpaPostgresApplication       : No active profile set, falling back to default profiles: default
    2016-10-14 11:09:21.679  INFO 6948 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@72f926e6: startup date [Fri Oct 14 11:09:21 BST 2016]; root of context hierarchy
    2016-10-14 11:09:23.402  INFO 6948 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$72d9bd3b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2016-10-14 11:09:23.922  INFO 6948 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
    2016-10-14 11:09:23.933  INFO 6948 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    2016-10-14 11:09:23.934  INFO 6948 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.5
    2016-10-14 11:09:24.062  INFO 6948 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2016-10-14 11:09:24.062  INFO 6948 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2387 ms
    2016-10-14 11:09:24.230  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2016-10-14 11:09:24.234  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2016-10-14 11:09:24.235  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2016-10-14 11:09:24.235  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2016-10-14 11:09:24.235  INFO 6948 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2016-10-14 11:09:24.478  INFO 6948 --- [           main] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
    2016-10-14 11:09:24.492  INFO 6948 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
    2016-10-14 11:09:24.554  INFO 6948 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.11.Final}
    2016-10-14 11:09:24.555  INFO 6948 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
    2016-10-14 11:09:24.557  INFO 6948 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
    2016-10-14 11:09:24.602  INFO 6948 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
    2016-10-14 11:09:24.828 ERROR 6948 --- [           main] o.a.tomcat.jdbc.pool.ConnectionPool      : Unable to create initial connections of pool.

    org.postgresql.util.PSQLException: FATAL: password authentication failed for user "test_java "
        at org.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:446) ~[postgresql-9.4.1211.jre7.jar:9.4.1211.jre7]
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:220) ~[postgresql-9.4.1211.jre7.jar:9.4.1211.jre7]

我不明白 Spring Data JPA 为何无法在普通 java JDBC 正常工作时进行身份验证。

【问题讨论】:

  • 在您的 spring 配置属性文件中,...username=test_java 后面有一个空格(而在您的测试属性中,没有空格)。
  • 谢谢!就是这样!你应该已经回答了,所以我可以接受!还是谢谢你!
  • 在运行所有 Spring Boot 应用程序上下文以及在 jdbc 之上使用 Spring JPA 抽象层时测试 jdbc 的逻辑是什么?

标签: postgresql spring-boot spring-data spring-data-jpa


【解决方案1】:

spring.database.driverClassName=org.postgresql.Driver 替换为 spring.datasource.driverClassName=org.postgresql.Driver

【讨论】:

    【解决方案2】:

    您的问题是身份验证,

    发送消息

    用户“test_java”的密码验证失败

    确定此信息连接正确吗?因为那是你的问题 如果正确,则进行测试,将您基地中的其他客户端与本地连接。

    • 如果没问题。问题不是远程连接能力。
    • 对于连接远程访问 pg_hba.config 的能力,然后是远程连接能力(或其他配置文件,您的数据库)

    请参阅:我的文件配置的那部分:

    # IPv4 local connections:
    host    all             all             127.0.0.1/32            md5
    host    all             all             192.168.2.0/24            md5
    

    允许连接本地和网络192.168.2.0,有多种配置方式

    【讨论】:

    • 如果 pg_hba.conf 是问题所在,则错误消息会有所不同
    • 你尝试远程连接还是本地连接?
    • @pozs 评论回答了,是用户名中 java_user 后面的空格
    猜你喜欢
    • 2020-07-06
    • 2012-11-01
    • 2019-11-02
    • 1970-01-01
    • 2018-10-22
    • 2020-01-01
    • 2019-09-02
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多