Edit

Share via


Authenticate Azure-hosted Java apps to Azure resources by using a system-assigned managed identity

The recommended approach to authenticate an Azure-hosted app to other Azure resources is to use a managed identity. Most Azure services support this approach, including apps hosted on Azure App Service, Azure Container Apps, and Azure Virtual Machines. For more information, see Azure services and resource types supporting managed identities. For more information about different authentication techniques and approaches, see Authenticate Java apps to Azure services by using the Azure Identity library.

In the following sections, you learn:

  • Essential managed identity concepts.
  • How to create a system-assigned managed identity for your app.
  • How to assign roles to the system-assigned managed identity.
  • How to authenticate by using the system-assigned managed identity from your app code.

Essential managed identity concepts

A managed identity enables your app to securely connect to other Azure resources without the use of secret keys or other application secrets. Internally, Azure tracks the identity and which resources it's allowed to connect to. Azure uses this information to automatically obtain Microsoft Entra tokens for the app to allow it to connect to other Azure resources.

There are two types of managed identities to consider when configuring your hosted app:

  • System-assigned managed identities are enabled directly on an Azure resource and are tied to its life cycle. When the resource is deleted, Azure automatically deletes the identity for you. System-assigned identities provide a minimalistic approach to using managed identities.
  • User-assigned managed identities are created as standalone Azure resources and offer greater flexibility and capabilities. They're ideal for solutions involving multiple Azure resources that need to share the same identity and permissions. For example, if multiple virtual machines need to access the same set of Azure resources, a user-assigned managed identity provides reusability and optimized management.

Tip

Learn more about selecting and managing system-assigned and user-assigned managed identities in the Managed identity best practice recommendations article.

The following sections describe the steps to enable and use a system-assigned managed identity for an Azure-hosted app. If you need to use a user-assigned managed identity, see Authenticate Azure-hosted Java apps to Azure resources by using a user-assigned managed identity.

Enable a system-assigned managed identity on the Azure hosting resource

To get started using a system-assigned managed identity with your app, enable the identity on the Azure resource hosting your app, such as an Azure App Service, Azure Container Apps, or Azure Virtual Machines instance.

You can enable a system-assigned managed identity for an Azure resource using either the Azure portal or the Azure CLI.

  1. In the Azure portal, navigate to the resource that hosts your application code, such as an Azure App Service or Azure Container Apps instance.

  2. From the resource's Overview page, expand Settings and select Identity from the navigation.

  3. On the Identity page, toggle the Status slider to On.

  4. Select Save to apply your changes.

    A screenshot showing how to enable a system-assigned managed identity on a container app.

Assign roles to the managed identity

Next, determine which roles your app needs and assign those roles to the managed identity. You can assign roles to a managed identity at the following scopes:

  • Resource: The assigned roles only apply to that specific resource.
  • Resource group: The assigned roles apply to all resources contained in the resource group.
  • Subscription: The assigned roles apply to all resources contained in the subscription.

The following example shows how to assign roles at the resource group scope, since many apps manage all their related Azure resources using a single resource group.

  1. Navigate to the Overview page of the resource group that contains the app with the system-assigned managed identity.

  2. Select Access control (IAM) on the left navigation.

  3. On the Access control (IAM) page, select + Add on the top menu and then choose Add role assignment to navigate to the Add role assignment page.

    A screenshot showing how to access the identity role assignment page.

  4. The Add role assignment page presents a tabbed, multi-step workflow to assign roles to identities. On the initial Role tab, use the search box at the top to locate the role you want to assign to the identity.

  5. Select the role from the results and then choose Next to move to the Members tab.

  6. For the Assign access to option, select Managed identity.

  7. For the Members option, choose + Select members to open the Select managed identities panel.

  8. On the Select managed identities panel, use the Subscription and Managed identity dropdowns to filter the search results for your identities. Use the Select search box to locate the system-identity you enabled for the Azure resource hosting your app.

    A screenshot showing the managed identity assignment process.

  9. Select the identity and choose Select at the bottom of the panel to continue.

  10. Select Review + assign at the bottom of the page.

  11. On the final Review + assign tab, select Review + assign to complete the workflow.

Authenticate to Azure services from your app

The Azure Identity library provides various credentials as implementations of TokenCredential. Each implementation supports different scenarios and Microsoft Entra authentication flows. For Azure-hosted apps, use DefaultAzureCredential, which automatically discovers managed identity credentials when running in Azure.

Implement the code

Add the azure-identity dependency to your pom.xml file:

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-identity</artifactId>
</dependency>

You access Azure services by using specialized client classes from the Azure SDK client libraries. The following code examples show you how to configure the credential for system-assigned managed identity authentication.

Use DefaultAzureCredential

Use DefaultAzureCredential for Azure-hosted apps because it automatically discovers managed identity credentials when running in Azure. For system-assigned managed identities, no extra configuration is required.

import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;

// DefaultAzureCredential automatically discovers managed identity when running in Azure
DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();

// Azure SDK client builders accept the credential as a parameter
SecretClient client = new SecretClientBuilder()
    .vaultUrl("https://<your-key-vault-name>.vault.azure.net")
    .credential(credential)
    .buildClient();

Use ManagedIdentityCredential

If you want to explicitly use the managed identity credential and avoid the credential chain lookup in DefaultAzureCredential, use ManagedIdentityCredential directly. For system-assigned managed identities, don't specify a client ID:

import com.azure.identity.ManagedIdentityCredential;
import com.azure.identity.ManagedIdentityCredentialBuilder;
import com.azure.security.keyvault.secrets.SecretClient;
import com.azure.security.keyvault.secrets.SecretClientBuilder;

// For system-assigned managed identity, don't specify a client ID
ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder().build();

// Azure SDK client builders accept the credential as a parameter
SecretClient client = new SecretClientBuilder()
    .vaultUrl("https://<your-key-vault-name>.vault.azure.net")
    .credential(credential)
    .buildClient();

Next steps

This article covered authentication using a system-assigned managed identity. This form of authentication is one of multiple ways you can authenticate in the Azure SDK for Java. The following articles describe other ways:

If you run into issues related to Azure-hosted application authentication, see Troubleshoot Azure-hosted application authentication.

After you master authentication, see Configure logging in the Azure SDK for Java for information on the logging functionality provided by the SDK.