Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
This article demonstrates a Java Tomcat app that signs in users to your Microsoft Entra ID tenant using the Microsoft Authentication Library (MSAL) for Java.
The following diagram shows the topology of the app:
The client app uses MSAL for Java (MSAL4J) to sign in users to their own Microsoft Entra ID tenant and obtain an ID token from Microsoft Entra ID. The ID token proves that a user is authenticated with this tenant. The app protects its routes according to the user's authentication status.
The following sections show you how to set up the sample application.
To clone the sample, open a Bash window and use the following command:
git clone https://github.com/Azure-Samples/ms-identity-msal-java-samples.git
cd 3-java-servlet-web-app/1-Authentication/sign-in
Alternatively, navigate to the ms-identity-msal-java-samples repository, then download it as a .zip file and extract it to your hard drive.
Important
To avoid file path length limitations on Windows, clone or extract the repository into a directory near the root of your hard drive.
There's one project in this sample. This section shows you how to register the app.
First, register the app in the Azure portal by following the instructions in Quickstart: Register an application with the Microsoft identity platform.
Then, use the following steps to complete the registration:
Navigate to the Microsoft identity platform for developers App registrations page.
Select New registration.
In the Register an application page that appears, enter the following application registration information:
In the Name section, enter a meaningful application name for display to users of the app - for example, java-servlet-webapp-authentication
.
Under Supported account types, select one of the following options:
In the Redirect URI section, select Web in the combo-box and enter the following redirect URI: http://localhost:8080/msal4j-servlet-auth/auth/redirect
.
Select Register to create the application.
On the app's registration page, find and copy the Application (client) ID value to use later. You use this value in your app's configuration file or files.
On the app's registration page, select Certificates & secrets on the navigation pane to open the page to generate secrets and upload certificates.
In the Client secrets section, select New client secret.
Type a description - for example, app secret.
Select one of the available durations: In 1 year, In 2 years, or Never Expires.
Select Add. The generated value is displayed.
Copy and save the generated value for use in later steps. You need this value for your code's configuration files. This value isn't displayed again, and you can't retrieve it by any other means. So, be sure to save it from the Azure portal before you navigate to any other screen or pane.
Use the following steps to configure the app:
Note
In the following steps, ClientID
is the same as Application ID
or AppId
.
Open the project in your IDE.
Open the ./src/main/resources/authentication.properties file.
Find the string {enter-your-tenant-id-here}
. Replace the existing value with one of the following values:
organizations
if you registered your app with the Accounts in any organizational directory option.common
if you registered your app with the Accounts in any organizational directory and personal Microsoft accounts option.consumers
if you registered your app with the Personal Microsoft accounts option.Find the string {enter-your-client-id-here}
and replace the existing value with the application ID or clientId
of the java-servlet-webapp-authentication
application copied from the Azure portal.
Find the string {enter-your-client-secret-here}
and replace the existing value with the value you saved during the creation of the java-servlet-webapp-authentication
app, in the Azure portal.
To build the sample using Maven, navigate to the directory containing the pom.xml file for the sample, and then run the following command:
mvn clean package
This command generates a .war file that you can run on various application servers.
The following sections show you how to deploy the sample to Azure App Service.
Maven Plugin for Azure App Service apps
If Maven isn't your preferred development tool, see the following similar tutorials that use other tools:
When you deploy to Azure App Service, the deployment automatically uses your Azure credentials from the Azure CLI. If the Azure CLI isn't installed locally, then the Maven plugin authenticates with OAuth or device sign-in. For more information, see authentication with Maven plugins.
Use the following steps to configure the plugin:
Run the following command to configure the deployment. This command helps you to set up the Azure App Service operating system, Java version, and Tomcat version.
mvn com.microsoft.azure:azure-webapp-maven-plugin:2.13.0:config
For Create new run configuration, press Y, then press Enter.
For Define value for OS, press 1 for Windows, or 2 for Linux, then press Enter.
For Define value for javaVersion, press 2 for Java 11, then press Enter.
For Define value for webContainer, press 4 for Tomcat 9.0, then press Enter.
For Define value for pricingTier, press Enter to select the default P1v2 tier.
For Confirm, press Y, then press Enter.
The following example shows the output of the deployment process:
Please confirm webapp properties
AppName : msal4j-servlet-auth-1707209552268
ResourceGroup : msal4j-servlet-auth-1707209552268-rg
Region : centralus
PricingTier : P1v2
OS : Linux
Java Version: Java 11
Web server stack: Tomcat 9.0
Deploy to slot : false
Confirm (Y/N) [Y]: [INFO] Saving configuration to pom.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 37.112 s
[INFO] Finished at: 2024-02-06T08:53:02Z
[INFO] ------------------------------------------------------------------------
After you've confirmed your choices, the plugin adds the required plugin element and settings to your project's pom.xml file to configure your app to run in Azure App Service.
The relevant portion of the pom.xml file should look similar to the following example:
<build>
<plugins>
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>>azure-webapp-maven-plugin</artifactId>
<version>x.xx.x</version>
<configuration>
<schemaVersion>v2</schemaVersion>
<resourceGroup>your-resourcegroup-name</resourceGroup>
<appName>your-app-name</appName>
...
</configuration>
</plugin>
</plugins>
</build>
You can modify the configurations for App Service directly in your pom.xml. Some common configurations are listed in the following table:
Property | Required | Description |
---|---|---|
subscriptionId |
false | The subscription ID. |
resourceGroup |
true | The Azure resource group for your app. |
appName |
true | The name of your app. |
region |
false | The region in which to host your app. The default value is centralus . For valid regions, see Supported Regions. |
pricingTier |
false | The pricing tier for your app. The default value is P1v2 for a production workload. The recommended minimum value for Java development and testing is B2 . For more information, see App Service Pricing. |
runtime |
false | The runtime environment configuration. For more information, see Configuration Details. |
deployment |
false | The deployment configuration. For more information, see Configuration Details. |
For the complete list of configurations, see the plugin reference documentation. All the Azure Maven plugins share a common set of configurations. For these configurations, see Common Configurations. For configurations specific to Azure App Service, see Azure app: Configuration Details.
Be sure to save aside the appName
and resourceGroup
values for later use.
When you deploy your application to App Service, your redirect URL changes to the redirect URL of your deployed app instance. Use the following steps to change these settings in your properties file:
Navigate to your app's authentication.properties file and change the value of app.homePage
to your deployed app's domain name, as shown in the following example. For example, if you chose example-domain
for your app name in the previous step, you must now use https://example-domain.azurewebsites.net
for the app.homePage
value. Be sure that you've also changed the protocol from http
to https
.
# app.homePage is by default set to dev server address and app context path on the server
# for apps deployed to azure, use https://your-sub-domain.azurewebsites.net
app.homePage=https://<your-app-name>.azurewebsites.net
After saving this file, use the following command to rebuild your app:
mvn clean package
Important
In this same authentication.properties file, you have a setting for your aad.secret
. It isn't a good practice to deploy this value to App Service. Neither is it a good practice to leave this value in your code and potentially push it up to your git repository. For removing this secret value from your code, you can find more detailed guidance in the Deploy to App Service - Remove secret section. This guidance adds extra steps for pushing the secret value to Key Vault and to use Key Vault References.
Because the redirect URI changes to your deployed app to Azure App Service, you also need to change the redirect URI in your Microsoft Entra ID app registration. Use the following steps to make this change:
Navigate to the Microsoft identity platform for developers App registrations page.
Use the search box to search for your app registration - for example, java-servlet-webapp-authentication
.
Open your app registration by selecting its name.
Select Authentication from the menu.
In the Web - Redirect URIs section, select Add URI.
Fill out the URI of your app, appending /auth/redirect
- for example, https://<your-app-name>.azurewebsites.net/auth/redirect
.
Select Save.
You're now ready to deploy your app to Azure App Service. Use the following command to make sure you're signed in to your Azure environment to execute the deployment:
az login
With all the configuration ready in your pom.xml file, you can now use the following command to deploy your Java app to Azure:
mvn package azure-webapp:deploy
After deployment is completed, your application is ready at http://<your-app-name>.azurewebsites.net/
. Open the URL with your local web browser, where you should see the start page of the msal4j-servlet-auth
application.
Use the following steps to explore the sample:
401: unauthorized
error instead of the ID token claims when the user isn't authorized.This sample shows how to use MSAL for Java (MSAL4J) to sign in users into your Microsoft Entra ID tenant. If you'd like to use MSAL4J in your own applications, you must add it to your projects using Maven.
If you want to replicate this sample's behavior, you can copy the pom.xml file and the contents of the helpers and authservlets folders in the src/main/java/com/microsoft/azuresamples/msal4j folder. You also need the authentication.properties file. These classes and files contain generic code that you can use in a wide array of applications. You can copy the rest of the sample as well, but the other classes and files are built specifically to address this sample's objective.
The following table shows the contents of the sample project folder:
File/folder | Description |
---|---|
src/main/java/com/microsoft/azuresamples/msal4j/authwebapp/ | This directory contains the classes that define the app's backend business logic. |
src/main/java/com/microsoft/azuresamples/msal4j/authservlets/ | This directory contains the classes that are used for sign in and sign out endpoints. |
*Servlet.java | All of the endpoints available are defined in Java classes with names ending in Servlet . |
src/main/java/com/microsoft/azuresamples/msal4j/helpers/ | Helper classes for authentication. |
AuthenticationFilter.java | Redirects unauthenticated requests to protected endpoints to a 401 page. |
src/main/resources/authentication.properties | Microsoft Entra ID and program configuration. |
src/main/webapp/ | This directory contains the UI - JSP templates |
CHANGELOG.md | List of changes to the sample. |
CONTRIBUTING.md | Guidelines for contributing to the sample. |
LICENSE | The license for the sample. |
A ConfidentialClientApplication
instance is created in the AuthHelper.java file, as shown in the following example. This object helps craft the Microsoft Entra ID authorization URL and also helps exchange the authentication token for an access token.
// getConfidentialClientInstance method
IClientSecret secret = ClientCredentialFactory.createFromSecret(SECRET);
confClientInstance = ConfidentialClientApplication
.builder(CLIENT_ID, secret)
.authority(AUTHORITY)
.build();
The following parameters are used for instantiation:
In this sample, these values are read from the authentication.properties file using a properties reader in the Config.java file.
The following steps provide a walkthrough of the app's functionality:
The first step of the sign-in process is to send a request to the /authorize
endpoint on for your Microsoft Entra ID tenant. The MSAL4J ConfidentialClientApplication
instance is used to construct an authorization request URL. The app redirects the browser to this URL, which is where the user signs in.
final ConfidentialClientApplication client = getConfidentialClientInstance();
AuthorizationRequestUrlParameters parameters = AuthorizationRequestUrlParameters.builder(Config.REDIRECT_URI, Collections.singleton(Config.SCOPES))
.responseMode(ResponseMode.QUERY).prompt(Prompt.SELECT_ACCOUNT).state(state).nonce(nonce).build();
final String authorizeUrl = client.getAuthorizationRequestUrl(parameters).toString();
contextAdapter.redirectUser(authorizeUrl);
The following list describes the features of this code:
AuthorizationRequestUrlParameters
: Parameters that must be set in order to build an AuthorizationRequestUrl.
REDIRECT_URI
: Where Microsoft Entra ID redirects the browser - along with the auth code - after collecting the user credentials. It must match the redirect URI in the Microsoft Entra ID app registration in the Azure portal.
SCOPES
: Scopes are permissions requested by the application. Normally, the three scopes openid profile offline_access
suffice for receiving an ID token response.
You can find a full list of scopes requested by the app in the authentication.properties file. You can add more scopes, such as User.Read
.
The user is presented with a sign-in prompt by Microsoft Entra ID. If the sign-in attempt is successful, the user's browser is redirected to the app's redirect endpoint. A valid request to this endpoint contains an authorization code.
The ConfidentialClientApplication
instance then exchanges this authorization code for an ID token and access token from Microsoft Entra ID.
// First, validate the state, then parse any error codes in response, then extract the authCode. Then:
// build the auth code params:
final AuthorizationCodeParameters authParams = AuthorizationCodeParameters
.builder(authCode, new URI(Config.REDIRECT_URI)).scopes(Collections.singleton(Config.SCOPES)).build();
// Get a client instance and leverage it to acquire the token:
final ConfidentialClientApplication client = AuthHelper.getConfidentialClientInstance();
final IAuthenticationResult result = client.acquireToken(authParams).get();
The following list describes the features of this code:
AuthorizationCodeParameters
: Parameters that must be set in order to exchange the Authorization Code for an ID and/or access token.authCode
: The authorization code that was received at the redirect endpoint.REDIRECT_URI
: The redirect URI used in the previous step must be passed again.SCOPES
: The scopes used in the previous step must be passed again.If acquireToken
is successful, the token claims are extracted. If the nonce check passes, the results are placed in context
- an instance of IdentityContextData
- and saved to the session. The application can then instantiate the IdentityContextData
from the session by way of an instance of IdentityContextAdapterServlet
whenever it needs access to it, as shown in the following code:
// parse IdToken claims from the IAuthenticationResult:
// (the next step - validateNonce - requires parsed claims)
context.setIdTokenClaims(result.idToken());
// if nonce is invalid, stop immediately! this could be a token replay!
// if validation fails, throws exception and cancels auth:
validateNonce(context);
// set user to authenticated:
context.setAuthResult(result, client.tokenCache().serialize());
For information about how the sample app filters access to routes, see AuthenticationFilter.java. In the authentication.properties file, the app.protect.authenticated
property contains the comma-separated routes that only authenticated users can access, as shown in the following example:
# for example, /token_details requires any user to be signed in and does not require special roles claim(s)
app.protect.authenticated=/token_details
Scopes tell Microsoft Entra ID the level of access that the application is requesting.
Based on the requested scopes, Microsoft Entra ID presents a consent dialogue to the user upon sign-in. If the user consents to one or more scopes and obtains a token, the scopes-consented-to are encoded into the resulting access_token
.
For the scopes requested by the application, see authentication.properties. These three scopes are requested by MSAL and given by Microsoft Entra ID by default.
Events
17 Mar, 23 - 21 Mar, 23
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Sign in users with Microsoft Entra ID in a Java web app - Training
Learn how to authenticate users with Microsoft Entra ID and get authorized access to data in a Java web app using Microsoft Authentication Library.
Certification
Microsoft Certified: Identity and Access Administrator Associate - Certifications
Demonstrate the features of Microsoft Entra ID to modernize identity solutions, implement hybrid solutions, and implement identity governance.
Documentation
Enable sign-in for Java Tomcat apps using MSAL4J - Azure Active Directory B2C
Shows you how to develop a Java Tomcat app that supports sign-in using Azure Active Directory B2C.
Enable Tomcat app sign-in and access to Microsoft Graph - Azure
Shows you how to develop a Java Tomcat app to sign in users and call Microsoft Graph with the Microsoft identity platform.
Secure Java apps using the Microsoft identity platform - Azure
Provides an overview of recommended strategies for securing Java applications with the Microsoft identity platform.