Skip to content

Instantly share code, notes, and snippets.

@unclebean
Last active October 4, 2024 03:10
Show Gist options
  • Save unclebean/d39ac0b78108fad5635b1016ce27b064 to your computer and use it in GitHub Desktop.
Save unclebean/d39ac0b78108fad5635b1016ce27b064 to your computer and use it in GitHub Desktop.
az
ManagedIdentityCredential managedIdentityCredential = new ManagedIdentityCredentialBuilder()
.clientId("your-umi-client-id") // User Managed Identity Client ID
.build();
String token = managedIdentityCredential.getToken(
new TokenRequestContext().addScopes("https://ossrdbms-aad.database.windows.net/.default")
).block().getToken();
azure:
identity:
managed-identity:
client-id: <UAMI-client-id> # Optional, only if you are using User-Assigned Managed Identity
active-directory:
client-id: <client-id> # Your Azure AD application client ID
tenant-id: <tenant-id> # Azure AD tenant ID
@Bean
public DataSource dataSource() {
PGSimpleDataSource dataSource = new PGSimpleDataSource();
dataSource.setURL("jdbc:postgresql://<your-azure-postgres-server>:5432/<db-name>");
dataSource.setUser("<username>");
// Obtain token using MSAL (or Azure SDK for identity)
String accessToken = getAccessTokenForPostgres();
dataSource.setPassword(accessToken);
return dataSource;
}
private String getAccessTokenForPostgres() {
TokenCredential tokenCredential = new ManagedIdentityCredentialBuilder()
.clientId("<UAMI-client-id>")
.build();
AccessToken token = tokenCredential.getToken(new TokenRequestContext()
.addScopes("https://<your-azure-postgres-server>.database.windows.net/.default")).block();
return token.getToken();
}
@Bean
public BlobServiceClient blobServiceClient() {
TokenCredential tokenCredential = new ManagedIdentityCredentialBuilder()
.clientId("<UAMI-client-id>")
.build();
return new BlobServiceClientBuilder()
.endpoint("<your-blob-endpoint>")
.credential(tokenCredential)
.buildClient();
}
@Bean
public SearchClient searchClient() {
TokenCredential tokenCredential = new ManagedIdentityCredentialBuilder()
.clientId("<UAMI-client-id>")
.build();
return new SearchClientBuilder()
.endpoint("<your-search-service-endpoint>")
.credential(tokenCredential)
.indexName("<index-name>")
.buildClient();
}
azure:
postgres:
url: "jdbc:postgresql://<your-azure-postgres-server>:5432/<db-name>"
username: "<db-username>"
tenant-id: "<your-tenant-id>"
client-id: "<UAMI-client-id>"
blob:
endpoint: "<your-blob-endpoint>"
cognitive-search:
endpoint: "<your-search-service-endpoint>"
<dependency>
<groupId>com.azure.spring</groupId>
<artifactId>azure-spring-boot-starter-active-directory</artifactId>
<version>4.3.0</version>
</dependency>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment