I'm trying to configure my Quarkus application to connect to Microsoft Entra authentication on a postgres database. Before I was using the token in the application properties and it was working properly, but now I want the token to be automatically generated so that I can take the application to Azure. This is currently my application properties:
# Basic JPA config
quarkus.datasource.db-kind=postgresql
quarkus.datasource.jdbc=false
# Hibernate ORM
quarkus.hibernate-orm.log.sql=true
quarkus.hibernate-orm.database.generation=update
quarkus.http.port=9000
and this is the class to automatically generate the token:
package com.xpto.hibernate.Utils;
import com.xpto.hibernate.Annotation.AzurePostgres;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Named;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.credential.AccessToken;
import .postgresql.ds.PGSimpleDataSource;
import javax.sql.DataSource;
@ApplicationScoped
public class AzurePostgresDataSourceProducer {
@Produces
@ApplicationScoped
@AzurePostgres
@Named("default")
public DataSource produceDataSource() throws Exception {
// Use DefaultAzureCredential to get token
var credential = new DefaultAzureCredentialBuilder().build();
TokenRequestContext request = new TokenRequestContext()
.addScopes("/.default");
AccessToken token = credential.getToken(request).block();
// Set up PostgreSQL DataSource
PGSimpleDataSource ds = new PGSimpleDataSource();
ds.setServerNames(new String[]{"xpto-int.postgres.database.azure"});
ds.setDatabaseName("postgres");
ds.setPortNumbers(new int[]{5432});
ds.setSsl(true);
ds.setUser("[email protected]");
ds.setPassword(token.getToken());
return ds;
}
}
But I'm getting the following error:
2025-03-25 19:35:09,651 WARN [io.qua.agr.dep.AgroalProcessor] (build-3) The Agroal dependency is present but no JDBC datasources have been defined. 2025-03-25 19:35:09,938 INFO [io.qua.dep.dev.IsolatedDevModeMain] (main) Attempting to start live reload endpoint to recover from previous Quarkus startup failure 2025-03-25 19:35:10,383 ERROR [io.qua.dep.dev.IsolatedDevModeMain] (main) Failed to start quarkus: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors [error]: Build step io.quarkus.hibernate.orm.deployment.HibernateOrmProcessor#configurationDescriptorBuilding threw an exception: io.quarkus.runtime.configuration.ConfigurationException: Unable to find datasource '' for persistence unit '': Datasource '' is not configured. To solve this, configure datasource ''. Refer to for guidance.
If I add the line quarkus.datasource.jdbc=true to application.properties then the error disappears but it's not generating the token automatically.
From what I read the decorator @Named("default") should have done the trick, but it's not working.
My thanks in advance
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744171400a4561552.html
评论列表(0条)