Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

page_type sample
languages
java
products
azure
msal-java
microsoft-entra-id
entra
urlFragment msal-java-authorization-groups
description This sample demonstrates how to create a Java Servlet web app that signs in users and restricts access to pages based on group membership with the Microsoft identity platform

Enable your Java Servlet web app to sign in users and restrict access to pages using security groups and groups claims with the Microsoft identity platform

Overview

This sample demonstrates how to create a Java Servlet web app that signs in users with Microsoft Authentication Library (MSAL) for Java and restricts access to pages based on Microsoft Entra ID security group membership.

Overview

An Identity Developer session covered Microsoft Entra app roles and security groups, featuring this scenario and how to handle the overage claim. Watch the video Using Security Groups and Application Roles in your apps

Scenario

  1. This web application uses MSAL for Java (MSAL4J) to sign in users a Microsoft Entra tenant and obtains an ID Token from Microsoft Entra ID.
  2. The ID Token proves that a user has successfully authenticated with this tenant.
  3. The web application protects its routes according to user's authentication status and group membership.

Contents

File/folder Description
AppCreationScripts/ Scripts to automatically configure Microsoft Entra app registrations.
src/main/java/com/microsoft/azuresamples/msal4j/groupswebapp/ This directory contains the classes that define the web 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 ending in ____Servlet.java
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.

Prerequisites

Setup

Clone or download this repository

From your shell or command line:

git clone https://github.com/Azure-Samples/ms-identity-java-servlet-webapp-authentication.git
cd 3-Authorization-II/groups

or download and extract the repository .zip file.

⚠️ To avoid file path length limitations on Windows, clone the repository into a directory near the root of your hard drive.

Register the sample application with your Microsoft Entra tenant

There is one project in this sample. To register the app on the portal, you can:

  • either follow manual configuration steps below
  • or use PowerShell scripts that:
    • automatically creates the Microsoft Entra applications and related objects (passwords, permissions, dependencies) for you.
    • modify the projects' configuration files.
    • by default, the automation scripts set up an application that works with accounts in your organizational directory only.
Expand this section if you want to use PowerShell automation.
  1. On Windows, run PowerShell and navigate to the root of the cloned directory

  2. In PowerShell run:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
  3. Run the script to create your Microsoft Entra application and configure the code of the sample application accordingly.

  4. In PowerShell run:

    cd .\AppCreationScripts\
    .\Configure.ps1

    Other ways of running the scripts are described in App Creation Scripts The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios.

Choose the Microsoft Entra tenant where you want to create your applications

As a first step you'll need to:

  1. Sign in to the Microsoft Entra admin center.
  2. If your account is present in more than one Microsoft Entra tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory to change your portal session to the desired Microsoft Entra tenant.

Register the web app (java-servlet-webapp-groups)

Register a new web app in the Microsoft Entra admin center. Following this guide, you must:

  1. Navigate to the Microsoft identity platform for developers App registrations page.
  2. Select New registration.
  3. In the Register an application page that appears, enter your application's registration information:
    • In the Name section, enter a meaningful application name that will be displayed to users of the app, for example java-servlet-webapp-groups.
    • Under Supported account types, select Accounts in this organizational directory only.
    • In the Redirect URI section, select Web in the combo-box and enter the following redirect URI: http://localhost:8080/msal4j-servlet-groups/auth/redirect.
  4. Select Register to create the application.
  5. In the app's registration screen, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
  6. Select Save to save your changes.
  7. In the app's registration screen, click on the Certificates & secrets blade in the left to open the page where we can generate secrets and upload certificates.
  8. In the Client secrets section, click on New client secret:
    • Type a key description (for instance app secret),
    • Select one of the available key durations (In 1 year, In 2 years, or Never Expires) as per your security concerns.
    • The generated key value will be displayed when you click the Add button. Copy the generated value for use in the steps later.
    • You'll need this key later in your code's configuration files. This key value will not be displayed again, and is not retrievable by any other means, so make sure to note it from the Microsoft Entra admin center before navigating to any other screen or blade.
  9. In the app's registration screen, select the API permissions blade in the left to open the page where we add access to the APIs that your application needs.
    • Select the Add a permission button and then:
    • Ensure that the Microsoft APIs tab is selected.
      • In the Commonly used Microsoft APIs section, select Microsoft Graph
      • In the Delegated permissions section, select User.Read and GroupMember.Read.All in the list. Use the search box if necessary.
      • Select the Add permissions button at the bottom.
    • GroupMember.Read.All requires admin consent. Select the Grant/revoke admin consent for {tenant} button, and then select Yes when you are asked if you want to grant consent for the requested permissions for all accounts in the tenant. You need to be a Microsoft Entra tenant admin to do this.

Configure the web app (java-servlet-webapp-groups) to use your app registration

Open the project in your IDE to configure the code.

In the steps below, "ClientID" is the same as "Application ID" or "AppId". Remove the curly braces from the placeholder values as well.

  1. Open the ./src/main/resources/authentication.properties file
  2. Find the string {enter-your-tenant-id-here}. Replace the existing value with:
    • Your Microsoft Entra tenant ID if you registered your app with the Accounts in this organizational directory only option.
  3. Find the string {enter-your-client-id-here} and replace the existing value with the application ID (clientId) of the java-servlet-webapp-groups application copied from the Microsoft Entra admin center.
  4. Find the string {enter-your-client-secret-here} and replace the existing value with the key you saved during the creation of the java-servlet-webapp-groups app, in the Microsoft Entra admin center.

Configure Security Groups

You have two different options available to you on how you can further configure your application(s) to receive the groups claim.

  1. Receive all the groups that the signed-in user is assigned to in a Microsoft Entra tenant, including nested groups.
  2. Receive the groups claim values from a filtered set of groups that your application is programmed to work with (Not available in the Microsoft Entra ID Free edition).

To get the on-premise group's samAccountName or On Premises Group Security Identifier instead of Group ID, please refer to the document Configure group claims for applications with Microsoft Entra ID.

Configure your application to receive all the groups the signed-in user is assigned to, including nested groups

  1. In the app's registration screen, select the Token Configuration blade in the left to open the page where you can configure the claims provided tokens issued to your application.
  2. Select the Add groups claim button on top to open the Edit Groups Claim screen.
  3. Select Security groups or the All groups (includes distribution lists but not groups assigned to the application) option. Choosing both negates the effect of Security Groups option.
  4. Under the ID section, select Group ID. This will result in Microsoft Entra ID sending the Object ID of the groups the user is assigned to in the groups claim of the ID Token that your app receives after signing-in a user.

Configure your application to receive the groups claim values from a filtered set of groups a user may be assigned to

Prerequisites, benefits and limitations of using this option
  1. This option is useful when your application is interested in a selected set of groups that a signing-in user may be assigned to and not every security group this user is assigned to in the tenant. This option also saves your application from running into the overage issue.
  2. This feature is not available in the Microsoft Entra ID Free edition.
  3. Nested group assignments are not available when this option is utilized.
Steps to enable this option in your app
  1. In the app's registration screen, select the Token Configuration blade in the left to open the page where you can configure the claims provided tokens issued to your application.
  2. Select the Add groups claim button on top to open the Edit Groups Claim screen.
  3. Select Groups assigned to the application.
    1. Choosing additional options like Security Groups or All groups (includes distribution lists but not groups assigned to the application) will negate the benefits your app derives from choosing to use this option.
  4. Under the ID section, select Group ID. This will result in Microsoft Entra ID sending the Object ID of the groups the user is assigned to in the groups claim of the ID Token.
  5. If you are exposing a web API using the Expose an API option, then you can also choose the Group ID option under the Access section. This will result in Microsoft Entra ID sending the Object ID of the groups the user is assigned to in the groups claim of the Access Token.
  6. In the app's registration screen, select on the Overview blade in the left to open the Application overview screen. Select the hyperlink with the name of your application in Managed application in local directory (note this field title can be truncated for instance Managed application in ...). When you select this link you will navigate to the Enterprise Application Overview page associated with the service principal for your application in the tenant where you created it. You can navigate back to the app registration page by using the back button of your browser.
  7. Select the Users and groups blade in the left to open the page where you can assign users and groups to your application.
    1. Select the Add user button on the top row.
    2. Select User and Groups from the resultant screen.
    3. Choose the groups that you want to assign to this application.
    4. Click Select in the bottom to finish selecting the groups.
    5. Select Assign to finish the group assignment process.
    6. Your application will now receive these selected groups in the groups claim when a user signing in to your app is a member of one or more these assigned groups.
  8. Select the Properties blade in the left to open the page that lists the basic properties of your application.Set the User assignment required? flag to Yes.

💡 Important security tip

When you set User assignment required? to Yes, Microsoft Entra ID will check that only users assigned to your application in the Users and groups blade are able to sign-in to your app. You can assign users directly or by assigning security groups they belong to.

Configure the web app (java-servlet-webapp-groups) to recognize Group IDs

⚠️ During Token Configuration, if you have chosen any other option except groupID (e.g. like DNSDomain\sAMAccountName) you should enter the group name (for example contoso.com\Test Group) instead of the object ID below:

  1. Open the ./src/main/resources/authentication.properties file.
  2. Find the string {enter-your-admins-group-id-here} and replace the existing value with the object ID of the GroupAdmin group copied from the Microsoft Entra admin center. Remove the curly braces from the placeholder value as well.
  3. Find the string {enter-your-users-group-id-here} and replace the existing value with the object ID of the GroupMember group copied from the Microsoft Entra admin center. Remove the curly braces from the placeholder value as well.

Running The Sample

Build .war File Using Maven

  1. Navigate to the directory containing the pom.xml file for this sample (the same directory as this README), and run the following Maven command:
    mvn clean package
    
  2. This should generate a .war file which can be run on a variety of application servers

Deploying the Sample

Our samples can be deployed to a number of application servers, such as Tomcat, WebLogic, or Webshpere, and MSAL Java itself can generally be integrated into existing applications without changes to your existing deployment set up.

You can find instructions for deploying our samples here on MSAL Java's Github wiki.

Experience

Explore the sample

  • Note the signed-in or signed-out status displayed at the center of the screen.
  • Click the context-sensitive button at the top right (it will read Sign In on first run)
  • Follow the instructions on the next page to sign in with an account in the Microsoft Entra tenant.
  • On the consent screen, note the scopes that are being requested.
  • Note the context-sensitive button now says Sign out and displays your username to its left.
  • The middle of the screen now has an option to click for ID Token Details: click it to see some of the ID token's decoded claims.
  • Click the Groups button to see any information about security group membership for the signed in user.
  • Click the Admin Only or Regular User buttons to access the groups claim protected endpoints.
    • If your signed in user is in the GroupAdmin group, the user will be able to enter both pages.
    • If your signed in user is in the GroupMember group, the user will be able to enter the Regular User page only.
    • If your signed in user is in neither group, the user will be able to access to none of the two pages.
  • You can also use the button on the top right to sign out.
  • After signing out, click the link to ID Token Details to observe that the app displays a 401: unauthorized error instead of the ID token claims when the user is not authorized.

ℹ️ Did the sample not work for you as expected? Did you encounter issues trying this sample? Then please reach out to us using the GitHub Issues page.

Processing Groups claim in tokens, including handling overage

The groups claim

The object id of the security groups the signed in user is member of is returned in the groups claim of the token.

{
  ...
  "groups": [
    "aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb",
    "bbbbbbbb-1111-2222-3333-cccccccccccc",]
  ...
}

The groups overage claim

To ensure that the token size doesn’t exceed HTTP header size limits, the Microsoft Identity Platform limits the number of object Ids that it includes in the groups claim.

If a user is member of more groups than the overage limit (150 for SAML tokens, 200 for JWT tokens, 6 for Single Page applications), then the Microsoft Identity Platform does not emit the group IDs in the groups claim in the token. Instead, it includes an overage claim in the token that indicates to the application to query the MS Graph API to retrieve the user’s group membership.

{
  ...
  "_claim_names": {
    "groups": "src1"
    },
    {
   "_claim_sources": {
    "src1": {
        "endpoint":"[Graph Url to get this user's group membership from]"
        }
    }
  ...
}

Create the overage scenario in this sample for testing

  1. You can use the BulkCreateGroups.ps1 provided in the App Creation Scripts folder to create a large number of groups and assign users to them. This will help test overage scenarios during development. Remember to change the user's objectId provided in the BulkCreateGroups.ps1 script.
  2. When you run this sample and an overage occurred, then you'd see the _claim_names in the home page after the user signs-in.
  3. We strongly advise you use the group filtering feature (if possible) to avoid running into group overages.
  4. In case you cannot avoid running into group overage, we suggest you use the following logic to process groups claim in your token.
    1. Check for the claim _claim_names with one of the values being groups. This indicates overage.
    2. If found, make a call to the endpoint specified in _claim_sources to fetch user’s groups.
    3. If none found, look into the groups claim for user’s groups.

When attending to overage scenarios, which requires a call to Microsoft Graph to read the signed-in user's group memberships, your app will need to have the GroupMember.Read.All for the getMemberObjects function to execute successfully.

Developers who wish to gain good familiarity of programming for Microsoft Graph are advised to go through the An introduction to Microsoft Graph for developers recorded session.

We'd love your feedback!

Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us.

About the code

This sample uses MSAL for Java (MSAL4J) to sign a user in and obtain an ID token that may contain the groups claim. If there are too many groups for emission in the ID token, the sample leverages Microsoft Graph SDK for Java to obtain the group membership data from Microsoft Graph. Based on the groups the user belongs to, the signed in user will be able to access either none, one, or both of the protected pages, Admins Only and Regular Users.

If you want to replicate this sample's behavior, you must add these libraries (MSAL4J and MS Graph SDK) your projects using Maven. As a developer, you may choose to copy the pom.xml file, and the contents of the helpers and authservlets packages in the src/main/java/com/microsoft/azuresamples/msal4j package. You'll also need the authentication.properties file. These classes and files contain generic code that can be used in a wide array of applications. The rest of the sample may be copied as well, but the other classes and files are built specifically to address this sample's objective.

A ConfidentialClientApplication instance is created in the AuthHelper.java class. This object helps craft the Microsoft Entra 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 need to be provided upon instantiation:

  • The Client ID of the app
  • The Client Secret, which is a requirement for Confidential Client Applications
  • The Microsoft Entra authority, which includes your Microsoft Entra tenant ID.

In this sample, these values are read from the authentication.properties file using a properties reader in the class Config.java.

Step-by-step walkthrough

  1. The first step of the sign-in process is to send a request to the /authorize endpoint on for our Microsoft Entra tenant. Our MSAL4J ConfidentialClientApplication instance is leveraged to construct an authorization request URL. Our app redirects the browser to this URL, which is where the user will sign 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);
    • AuthorizationRequestUrlParameters: Parameters that must be set in order to build an AuthorizationRequestUrl.
    • REDIRECT_URI: Where Microsoft Entra ID will redirect the browser (along with auth code) after collecting user credentials. It must match the redirect URI in the Microsoft Entra app registration on Microsoft Entra admin center
    • SCOPES: Scopes are permissions requested by the application.
      • Normally, the three scopes openid profile offline_access suffice for receiving an ID Token response.
      • Full list of scopes requested by the app can be found in the authentication.properties file. You can add more scopes like User.Read and so on.
  2. 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 our app's redirect endpoint. A valid request to this endpoint will contain an authorization code.

  3. Our 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();
    • 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.
  4. 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 this from the session (by way of an instance of IdentityContextAdapterServlet) whenever it needs access to it:

    // 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());
    
    // handle groups overage if it has occurred.
    handleGroupsOverage(contextAdapter);
  5. After previous step, group memberships may be extracted by calling context.getGroups() (an instance of IdentityContextData).

  6. If the user is a member of too many groups (>200), a call to context.getGroups() might have been empty if it weren't for the call to handleGroupsOverage(). Meanwhile, context.getGroupsOverage() will return true, signalling that an overage has occurred, and that getting the full list of groups will require a call to Microsoft Graph. See handleGroupsOverage() method in AuthHelper.java for this application uses context.setGroups() when there is an overage.

Protecting the routes

See AuthenticationFilter.java for how the sample app filters access to routes. In the authentication.properties file, the key app.protect.authenticated contains the comma-separated routes that are to be accessed by authenticated users only.

# e.g., /token_details requires any user to be signed in and does not require special groups claim
app.protect.authenticated=/token_details

Any of the routes listed in the comma-separated rule sets under the app.protect.groups are also off-limits to non-authenticated authenticated users.

However, these routes also contain a space-separated list of group memberships: only users belonging to at least one of the corresponding groups will be able to access these routes after authenticating.

# define short names for group IDs here for the app. This will be useful in the next key (app.protect.groups).
# EXCLUDE the curly braces, they are in this file only as delimiters.
# example:
# app.groups=groupA abcdef-qrstuvw-xyz groupB abcdef-qrstuv-wxyz
app.groups=admin {enter-your-admins-group-id-here}, user {enter-your-users-group-id-here}

# A route and its corresponding group(s) that can view it, <space-separated>; the start of the next route & its group(s) is delimited by a <comma-and-space-separator>
# this says: /admins_only can be accessed by admin group, /regular_user can be accessed by admin group and user group
app.protect.groups=/admin_only admin, /regular_user admin user

Scopes

  • 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 signing 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.
  • Note the scope requested by the application by referring to authentication.properties. By default, the application sets the scopes value to GroupMember.Read.All.
  • This particular MS Graph API scope is required in case the application needs to call Graph for getting the user's group memberships.

Deploy to Azure

Follow this guide to deploy this app to Azure App Service.

More information

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 [microsoft-entra-id ms-identity adal msal].

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.

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com