当前位置: 首页 > 知识库问答 >
问题:

无法连接到Postgres DB,因为不支持身份验证类型10

隆钊
2023-03-14

我也尝试使用JDBCTemplate。不起作用

修改了引用此帖子的pg_hba.cfg文件-不起作用

使用了不推荐的Lib of-也不起作用。

请建议我解决这个问题的办法。

我的代码和配置:

    @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")
postgres=# show password_encryption;
 password_encryption
---------------------
 scram-sha-256
(1 row)

共有1个答案

许安邦
2023-03-14

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

>

  • 将password_encryption更改为MD5。

    文件:C:\Program Files\PostgreSQL\13\data\PostgreSQL L.conf

    在主机设置中,将SCRAM-SHA-256更改为MD5

    文件:C:\Program Files\PostgreSQL\13\data\pg_hba.conf。

    host all all 0.0.0.0/0 md5

    更改密码(此还原密码为md5格式)。

    示例:使用密码'root'更改角色postgres;

    如果您在非生产环境中工作,请确保设置listen_addresses='*'

  •  类似资料:
    • 我需要支持3 s的身份验证方式 经过几个小时的搜索,我发现最好的 我不了解spring security,所以im

    • 问题内容: 我试图连接到http://bluesql.net上的mySQL数据库,但是当我尝试连接时,出现此错误: 我已经研究过了,它与MySQL 4.1之前使用的一些旧密码方案有关。较新的版本可以选择使用旧密码,我已阅读过此密码可能会导致此问题。 我正在运行php 5.3,并与mySQLi(新的mysqli(…))连接。我希望我可以在代码中做一些事情以连接到bluesql.net上的数据库- 显

    • 我在Ubuntu上安装了自己的Jitsi实例,遵循使用官方软件包的文档。这很有魅力。但我不想有一个完全开放的实例,所以我遵循了https://github.com/jitsi/jicofo#secure-域需要登录才能创建会议。 据我所知,我完全遵循了文档,但在重新启动Jitsi后,我无法再创建会议。我看到以下行为: 私人网址:我得到一个消息,会议还没有开始,还有一个“我是主持人”按钮。如果我按下

    • 我通过play framework和mariadb客户端使用hikari pool connection,由于我定期更新它们(play 2.6.5->2.6.6和mariadb 2.1.1->2.1.2,但不确定是否相关),我出现了以下错误: 我在这里发现了一个相关的问题,并试图将idleTimeout和maxLifetime更改为2分钟和5分钟,但错误仍然发生。 我使用的是HikariCP 2.

    • 我收到一个错误“无法建立SSL连接,请参阅内部异常。身份验证失败,请参阅内部异常。”当尝试通过C#HttpWebRequest通过SSL发布请求时(已尝试RestSharp和HttpClient,结果相同)。我还尝试了网络上提到的所有可能的解决方案,如: 他们都不为我工作。另一个问题是,我在另一台计算机上运行相同的项目,它的工作没有任何问题。所以看起来问题的来源是环境,也许. net框架或注册表中

    • Keycloak是否支持基本身份验证(包含单词basic word后跟空格和base64编码的字符串username:password的授权头),如果支持,我如何为它配置领域和客户端设置?我想用Keycloak保护我的rest api,并支持基本身份验证作为一个选项。