Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.2.2</version>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.DirectConnectionConfig;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.spring.data.cosmos.config.AbstractCosmosConfiguration;
import com.azure.spring.data.cosmos.config.CosmosConfig;
import com.azure.spring.data.cosmos.core.ResponseDiagnostics;
Expand Down Expand Up @@ -33,10 +35,15 @@ public class SampleAppConfiguration extends AbstractCosmosConfiguration {
@Bean
public CosmosClientBuilder cosmosClientBuilder() {
DirectConnectionConfig directConnectionConfig = DirectConnectionConfig.getDefaultConfig();
// DefaultAzureCredential uses environment variables as explained in the below link to build corresponding
// credential. For example setting the environment variables AZURE_CLIENT_ID, AZURE_USERNAME, AZURE_PASSWORD
// will create a UsernamePasswordCredential. Read the link below for further info
// https://docs.microsoft.com/en-us/java/api/overview/azure/identity-readme?view=azure-java-stable#service-principal-with-secret
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
return new CosmosClientBuilder()
.endpoint(properties.getUri())
.key(properties.getKey())
.directMode(directConnectionConfig);
.endpoint(properties.getUri())
.credential(credential)
.directMode(directConnectionConfig);
}

@Bean
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cosmos.uri=${ACCOUNT_HOST}
cosmos.key=${ACCOUNT_KEY}
# cosmos.key=${ACCOUNT_KEY} -- Not necessary when using Azure identity credentials
cosmos.secondaryKey=${SECONDARY_ACCOUNT_KEY}

dynamic.collection.name=spel-property-collection
Expand Down