|
| 1 | +--- |
| 2 | +title: Microsoft identity platform Java web app quickstart | Azure |
| 3 | +description: Learn how to implement Microsoft Sign-In on a Java Web App using OpenID Connect |
| 4 | +services: active-directory |
| 5 | +documentationcenter: dev-center-name |
| 6 | +author: sangonzal |
| 7 | +editor: '' |
| 8 | + |
| 9 | +ms.assetid: 820acdb7-d316-4c3b-8de9-79df48ba3b06 |
| 10 | +ms.service: active-directory |
| 11 | +ms.subservice: develop |
| 12 | +ms.devlang: na |
| 13 | +ms.topic: quickstart |
| 14 | +ms.tgt_pltfrm: na |
| 15 | +ms.workload: identity |
| 16 | +ms.date: 08/11/2019 |
| 17 | +ms.author: sagonzal |
| 18 | +ms.custom: aaddev |
| 19 | +--- |
| 20 | + |
| 21 | +# Quickstart: Add sign-in with Microsoft to a Java web app |
| 22 | + |
| 23 | +[!INCLUDE [active-directory-develop-applies-v2](../../../includes/active-directory-develop-applies-v2.md)] |
| 24 | + |
| 25 | +In this quickstart, you'll learn how to integrate a Java web application with the Microsoft identity platform. Your app |
| 26 | +will sign in a user, get an access token to call the Microsoft Graph API, and make a request to the Microsoft Graph API. |
| 27 | + |
| 28 | +When you've completed the guide, your application will accept sign-ins of personal Microsoft accounts (including outlook.com, |
| 29 | + live.com, and others) and work or school accounts from any company or organization that uses Azure Active Directory. |
| 30 | + |
| 31 | + |
| 32 | + |
| 33 | +> ## Prerequisites |
| 34 | +> To run this sample you will need: |
| 35 | +> - An internet connection. |
| 36 | +> - A working installation of Java and Maven. |
| 37 | +> - 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.](https://docs.microsoft.com/azure/active-directory/develop/quickstart-create-new-tenant) |
| 38 | +
|
| 39 | +> [!div renderon="docs"] |
| 40 | +> ## Register and download your quickstart app |
| 41 | +> You have two options to start your quickstart application: |
| 42 | +> * Express: [Option 1: Register and auto configure your app and then download your code sample.](#option-1-register-and-auto-configure-your-app-and-then-download-your-code-sample) |
| 43 | +> * Manual: [Option 2: Register and manually configure your application and code sample.](#option-2-register-and-manually-configure-your-application-and-code-sample) |
| 44 | +> |
| 45 | +> ### Option 1: Register and auto configure your app and then download your code sample |
| 46 | +> |
| 47 | +> 1. Go to the [Azure portal - App registrations](https://portal.azure.com/#blade/Microsoft_AAD_IAM/ActiveDirectoryMenuBlade/RegisteredApps). |
| 48 | +> 1. Enter a name for your application and select **Register**. |
| 49 | +> 1. Follow the instructions to download and automatically configure your new application. |
| 50 | +> |
| 51 | +> ### Option 2: Register and manually configure your application and code sample |
| 52 | +> |
| 53 | +> |
| 54 | +> #### Step 1: Download the code sample |
| 55 | +> |
| 56 | +> - [Download the Code Sample](https://github.com/Azure-Samples/ms-identity-java-webapp/archive/master.zip) |
| 57 | +> |
| 58 | +> #### Step 2: Open application.properties |
| 59 | +> |
| 60 | +> 1. Extract the zip file to a local folder. |
| 61 | +> 1. (Optional) If you use an integrated development environment, open the sample in your favorite IDE. |
| 62 | +> 1. Open the *application.properties* file. You will insert values for `aad.clientId`, `aad.authority`, and `aad.secretKey` when you register your application in the next step. |
| 63 | +
|
| 64 | + |
| 65 | +> #### Step 3: Register your application |
| 66 | +> To register your application and manually add the app's registration information to your solution, follow these steps: |
| 67 | +> |
| 68 | +> 1. Sign in to the [Azure portal](https://portal.azure.com) using either a work or school account, or a personal Microsoft account. |
| 69 | +> 1. If your account gives you access to more than one tenant, select your account in the top right corner, and set your portal session to the desired Azure AD tenant. |
| 70 | +> 1. Navigate to the Microsoft identity platform for developers [App registrations](https://go.microsoft.com/fwlink/?linkid=2083908) page. |
| 71 | +> 1. Select **New registration**. |
| 72 | +> 1. When the **Register an application** page appears, enter your application's registration information: |
| 73 | +> - In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `java-webapp`. |
| 74 | +> - Leave **Redirect URI** blank for now, and select **Register**. |
| 75 | +> 1. Find the **Application (client) ID** value of the application. Update the value of `Enter_the_Application_Id_here` in the *application.properties* file. |
| 76 | +> 1. Find the **Directory (tenant) ID** value of the application. Update the value of `Enter_the_Tenant_Info_Here` in the *application.properties* file. |
| 77 | +> 1. Select the **Authentication** menu, and then add the following information: |
| 78 | +> - In **Redirect URIs**, add `http://localhost:8080/msal4jsamples/secure/aad` and `https://localhost:8080/msal4jsamples/graph/users`. |
| 79 | +> - Select **Save**. |
| 80 | +> 1. On the left hand menu, choose **Certificates & secrets** and click on **New client secret** in the **Client Secrets** section: |
| 81 | +> |
| 82 | +> - Type a key description (of instance app secret). |
| 83 | +> - Select a key duration of **In 1 year**. |
| 84 | +> - When you click on **Add**, the key value will be displayed. |
| 85 | +> - Copy the value of the key. Open the *application.properties* file that you downloaded earlier and update the value of `Enter_the_Client_Secret_Here` with the key value. |
| 86 | +> |
| 87 | +> [!div class="sxs-lookup" renderon="portal"] |
| 88 | +> #### Step 1: Configure your application in the Azure portal |
| 89 | +> For the code sample for this quickstart to work, you need to: |
| 90 | +> 1. Add reply URLs as `http://localhost:8080/msal4jsamples/secure/aad` and `https://localhost:8080/msal4jsamples/graph/users`. |
| 91 | +> 1. Create a Client Secret. |
| 92 | +> > [!div renderon="portal" id="makechanges" class="nextstepaction"] |
| 93 | +> > [Make this change for me]() |
| 94 | +> |
| 95 | +> > [!div id="appconfigured" class="alert alert-info"] |
| 96 | +> >  Your application is configured with these attributes. |
| 97 | +> |
| 98 | +> #### Step 2: Download the code sample |
| 99 | +> |
| 100 | +> - [Download the Code Sample](https://github.com/Azure-Samples/ms-identity-java-webapp/archive/master.zip) |
| 101 | +> |
| 102 | +> #### Step 3: Configure the code sample |
| 103 | +> |
| 104 | +> 1. Extract the zip file to a local folder. |
| 105 | +> 1. If you use an integrated development environment, open the sample in your favorite IDE (optional). |
| 106 | +> 1. Open the **application.properties** file, which can be found in *src/main/resources/*. |
| 107 | +> 1. Replace application properties. |
| 108 | +> 1. Find `aad.clientId` and update the value of `Enter_the_Application_Id_here` with the **Application (client) ID** value of the application you registered. |
| 109 | +> 1. Find `aad.authority` and update the value of `Enter_the_Tenant_Name_Here` with the **Directory (tenant) ID** value of the application you registered. |
| 110 | +> 1. Find `aad.secretKey` and update the value of `Enter_the_Client_Secret_Here` with the **Client Secret** you created in **Certificates & Secrets** for the application you registered. |
| 111 | +
|
| 112 | +#### Step 4: Run the code sample |
| 113 | +1. Run the code sample, and open a browser and navigate to *http://localhost:8080*. |
| 114 | +1. The front page contains a **sign-in** button. Click on the **sign-in** button to redirect to Azure Active Directory. The user will be prompted for their credentials. |
| 115 | +1. After successfully authenticating on Azure Active Directory, they will be redirected to *http://localhost:8080/msal4jsamples/secure/aad*. They are officially signed in to the application, and the page should show information for the signed in account. It will also contain buttons for: |
| 116 | + - *Sign Out*: Will sign out the current user from the application, and redirect them the home page. |
| 117 | + - *Show Users*: Will acquire a token for the Microsoft Graph, then call the Microsoft Graph with the token attached to the request to get all of the users in the tenant. |
| 118 | + |
| 119 | + |
| 120 | +## More information |
| 121 | + |
| 122 | +### Getting MSAL |
| 123 | +MSAL4J is the library used to sign in users and request tokens used to access an API protected by the Microsoft identity Platform. |
| 124 | +You can add MSAL4J to your application by using Maven or Gradle to manage your dependencies by making the following changes to the pom.xml or build.gradle file in your application. |
| 125 | + |
| 126 | +``` |
| 127 | +<dependency> |
| 128 | + <groupId>com.microsoft.azure</groupId> |
| 129 | + <artifactId>msal4j</artifactId> |
| 130 | + <version>0.5.0-preview</version> |
| 131 | +</dependency> |
| 132 | +``` |
| 133 | + |
| 134 | +```$xslt |
| 135 | +compile group: 'com.microsoft.azure', name: 'msal4j', version: '0.5.0-preview' |
| 136 | +``` |
| 137 | + |
| 138 | + |
| 139 | +### Msal initialization |
| 140 | +You can add the reference to MSAL4J by adding the following code to the top of the file where you will be using MSAL4J: |
| 141 | + |
| 142 | +``` |
| 143 | +import com.microsoft.aad.msal4j.*; |
| 144 | +``` |
| 145 | + |
| 146 | +[!INCLUDE [Help and support](../../../includes/active-directory-develop-help-support-include.md)] |
0 commit comments