gpt4 book ai didi

java - 由于不支持身份验证类型10,因此无法连接到Postgres DB

转载 作者:行者123 更新时间:2023-12-03 03:06:57 26 4
gpt4 key购买 nike

我最近尝试过Postgres。在本地(PostgreSQL 13.0)上安装了它。
创建一个maven项目并使用Spring Data JPA,效果很好。而当我尝试使用Gradle项目时,我无法连接到数据库并不断出现以下错误。

org.postgresql.util.PSQLException: The authentication type 10 is notsupported. Check that you have configured the pg_hba.conf file toinclude the client's IP address or subnet, and that it is using anauthentication scheme supported by the driver. atorg.postgresql.core.v3.ConnectionFactoryImpl.doAuthentication(ConnectionFactoryImpl.java:614)~[postgresql-42.1.4.jar:42.1.4] atorg.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:222)~[postgresql-42.1.4.jar:42.1.4] atorg.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)~[postgresql-42.1.4.jar:42.1.4] atorg.postgresql.jdbc.PgConnection.(PgConnection.java:194)~[postgresql-42.1.4.jar:42.1.4] atorg.postgresql.Driver.makeConnection(Driver.java:450)~[postgresql-42.1.4.jar:42.1.4] atorg.postgresql.Driver.connect(Driver.java:252)~[postgresql-42.1.4.jar:42.1.4] atjava.sql.DriverManager.getConnection(Unknown Source) [na:1.8.0_261]at java.sql.DriverManager.getConnection(Unknown Source)[na:1.8.0_261] atorg.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:94)[postgresql-42.1.4.jar:42.1.4] atorg.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:79)[postgresql-42.1.4.jar:42.1.4]


我也尝试使用JDBCTemplate。不起作用
修改了 this帖子的pg_hba.cfg文件-不起作用
使用了已弃用的Lib-也不起作用。
请为我建议解决此问题的方法。
我的代码和配置:
    @Configuration
public class DataSourceConfig {


@Bean
public DriverManagerDataSource getDataSource() {
DriverManagerDataSource dataSourceBuilder = new DriverManagerDataSource();
dataSourceBuilder.setDriverClassName("org.postgresql.Driver");
dataSourceBuilder.setUrl("jdbc:postgresql://localhost:5432/postgres");
dataSourceBuilder.setUsername("postgres");
dataSourceBuilder.setPassword("root");
return dataSourceBuilder;
}

}



@Component
public class CustomerOrderJDBCTemplate implements CustomerOrderDao{

private DataSource dataSource;

private JdbcTemplate jdbcTemplateObject;

@Autowired
ApplicationContext context;

public void setDataSource() {
//Getting Bean by Class
DriverManagerDataSource dataSource = context.getBean(DriverManagerDataSource.class);
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(this.dataSource);
}

@Override
public Customer create(Customer customer) {
setDataSource();
String sql = "insert into CustomerOrder (customerType, customerPayment) values (?, ?)";
//jdbcTemplateObject.update(sql, customerOrder.getCustomerOrderType(), customerOrder.getCustomerOrderPayment());

KeyHolder holder = new GeneratedKeyHolder();
jdbcTemplateObject.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, customer.getType());
ps.setString(2, customer.getPayment());
return ps;
}
}, holder);

long customerId = holder.getKey().longValue();
customer.setCustomerID(customerOrderId);
return customer;

}

}
依存关系
implementation('org.springframework.boot:spring-boot-starter-web')
compile("org.springframework.boot:spring-boot-devtools")
compile(group: 'org.postgresql', name: 'postgresql', version: '42.1.4')
compile("org.springdoc:springdoc-openapi-ui:1.4.1")
compile("org.springframework:spring-jdbc:5.2.5.RELEASE")
password_encryption设置如下:
postgres=# show password_encryption;
password_encryption
---------------------
scram-sha-256
(1 row)

最佳答案

我通过在PostgreSQL 版本13 中应用以下步骤解决了类似的问题:

  • 将password_encryption更改为md5。
    文件:C:\Program Files\PostgreSQL\13\data\postgresql.conf
    enter image description here
  • 在主机设置中将scram-sha-256更改为md5
    文件:C:\Program Files\PostgreSQL\13\data\pg_hba.conf。
    托管所有0.0.0.0/0 md5
    enter image description here
  • 更改密码(此恢复密码为md5格式)。
    示例:ALTER ROLE postgres WITH PASSWORD 'root';
  • 如果您非生产性工作,请确保设置listen_addresses = '*'环境。
    文件:C:\Program Files\PostgreSQL\13\data\postgresql.conf
  • 关于java - 由于不支持身份验证类型10,因此无法连接到Postgres DB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64210167/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com