We are having a PostgreSQL single service instance running in Azure, which is Azure AD integrated. So to connect via psql
I follow this steps:
- Log in via
az login
- Retrieve access token
$env:PGPASSWORD=$(az account get-access-token --resource-type oss-rdbms --query accessToken --output tsv)
- Login
psql "host=single-server-instance.postgres.database.azure.com user=aad_group@single-server-instance dbname=demodb"
So far so good. But how would I do that with Spring Data JPA?
This is what my current application.properties
file looks like. Of course I don't want to insert the access token over and over again.
logging.level.org.hibernate.SQL=DEBUG
spring.datasource.url=jdbc:postgresql://single-server-instance.postgres.database.azure.com:5432/demodb
spring.datasource.username=aad_group@single-server-instance
spring.datasource.password=eyJ...there goes the access token
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
This are my dependencies from build.gradle
.
// ...
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
// ...
Questions:
- Should I move the
spring.datasource
andspring.jpa
configuration part into my code and supply the access token to thespring.datasource.password
from there? If so, how can I do this? Where should it go? - How can I retrieve an access token and pass it to Spring Data?
- How should I deal with the caching of the access token? Do I have to take care of the refresh token and deal with access token expiry?