Integrating Azure AD into a Java web application
About this sample
This sample demonstrates a Java web application calling a Microsoft Graph that is secured using Azure Active Directory.
- The Java web application uses the Active Directory Authentication Library for Java(ADAL4J) to obtain a JWT access token from Azure Active Directory (Azure AD):
- The access token is used as a bearer token to authenticate the user when calling the Microsoft Graph.
This sample shows how to build a Java web app(confidential client) that uses OpenID Connect to sign-in users from a single Azure Active Directory (Azure AD) tenant using ADAL4J. For more information about how the protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
How to run this sample
To run this sample, you'll need:
- Working installation of Java and Maven
- Tomcat or any other J2EE container solution
- An Internet connection
- An Azure Active Directory (Azure AD) tenant. For more information on how to get an Azure AD tenant, see How to get an Azure AD tenant
- A user account in your Azure AD tenant. This sample will not work with a Microsoft account (formerly Windows Live account). Therefore, if you signed in to the Azure portal with a Microsoft account and have never created a user account in your directory before, you need to do that now.
Step 1: Download Java (8 and above) for your platform
Step 2: Clone or download this repository
From your shell or command line:
git clone https://github.com/Azure-Samples/active-directory-java-webapp-openidconnect.git
Step 3: Register the sample with your Azure Active Directory tenant
To register these projects, you can:
- either follow the steps in the paragraphs below
- or use PowerShell scripts that:
- automatically create for you the Azure AD applications and related objects (passwords, permissions, dependencies)
If you want to use this automation, read the instructions in App Creation Scripts. Please note that the configuration of your code (Step 4) still needs to be done manually.
First step: choose the Azure AD tenant where you want to create your applications
As a first step you'll need to:
- Sign in to the Azure portal.
- On the top bar, click on your account and under the Directory list, choose the Active Directory tenant where you wish to register your application.
- Click on All services in the left-hand nav, and choose Azure Active Directory.
In the next steps, you might need the tenant name (or directory name) or the tenant ID (or directory ID). These are presented in the Properties of the Azure Active Directory window respectively as Name and Directory ID
Register the app app (Webapp-Openidconnect)
- In the Azure Active Directory pane, click on App registrations and choose New application registration.
- Enter a friendly name for the application, for example 'Webapp-Openidconnect' and select 'Web app / API' as the Application Type.
- For the sign-on URL, enter the base URL for the sample, which is by default
- Click on Create to create the application.
- In the succeeding page, Find the Application ID value and copy it to the clipboard. You'll need it to configure the Visual Studio configuration file for this project.
- Then click on Settings, and choose Properties.
- For the App ID URI, replace the guid in the generated URI 'https://<your_tenant_name>/<guid>', with the name of your service, for example, 'https://<your_tenant_name>/Webapp-Openidconnect' (replacing
<your_tenant_name>with the name of your Azure AD tenant)
- Click on Redirect URIs and set it to
From the Settings menu, choose Keys and add a new entry in the Password section:
- Type a key description (of instance
- Select a key duration of either In 1 year, In 2 years, or Never Expires.
- When you save this page, the key value will be displayed, copy, and save the value in a safe location.
- You'll need this key later to configure the project. This key value will not be displayed again, nor retrievable by any other means, so record it as soon as it is visible from the Azure portal.
- Type a key description (of instance
Configure Permissions for your application. To that extent, in the Settings menu, choose the 'Required permissions' section and then, click on Add, then Select an API, and type
Microsoft Graphin the textbox. Then, click on Select Permissions and select Directory.Read.All.
- Note that for Directory.Read.All requires the user grating the permission to be a tenant administrator. If you created an AzureAD tenant as part of the sample, you will be an administrator by default.
Step 4: Configure the sample to use your Azure AD tenant
web.xml in the webapp/WEB-INF/ folder. Fill in with your tenant and app registration information noted in registration step. Replace 'YOUR_TENANT_NAME' with the tenant domain name, 'YOUR_CLIENT_ID' with the Application Id and 'YOUR_CLIENT_SECRET' with the key value noted.
Step 5: Package and then deploy the adal4jsample.war file.
$ mvn compile -DgroupId='com.microsoft.azure' -DartifactId=adal4jsample -DinteractiveMode=false
$ mvn package
This will generate a
adal4jsample.war file in your /targets directory. Deploy this war file using Tomcat or any other J2EE container solution. To deploy on Tomcat container, copy the .war file to the webapps folder under your Tomcat installation and then start the Tomcat server.
This WAR will automatically be hosted at
Click on "Show users in the tenant" to start the process of logging in.
Community Help and Support
Use Stack Overflow to get support from the community. Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before. Make sure that your questions or comments are tagged with [
If you find a bug in the sample, please raise the issue on GitHub Issues.
To provide a recommendation, visit the following User Voice page.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
For more information, see ADAL4J conceptual documentation
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.