You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-33
Original file line number
Diff line number
Diff line change
@@ -28,13 +28,13 @@ This sample demonstrates a Java web application signing-in a user with the Micro
28
28
### Scenario
29
29
30
30
1. The Java web application uses the [Microsoft Authentication Library for Java (MSAL4J)](https://github.com/AzureAD/microsoft-authentication-library-for-java) to obtain an Access token from the Microsoft identity platform for the authenticated user.
31
-
1. The access token is then used as a bearer token to authorize the caller in the Java web API and then subsequently exchanged for another access token for the Microsoft Graph API.
31
+
1. The access token is then used as a bearer token to the request to the Java web API. The web API validates the access token using Spring Security, exchanges the incoming access token for a Microsoft Graph access token using OAuth2.0 On-behalf-of flow, and uses the new access token to request information from the Graph Me endpoint.
32
32
33
33
The flow is as follows:
34
34
35
35
1. Sign-in the user in the client(web) application.
36
36
1. Acquire an access token for the Java Web API and call it.
37
-
1. The Java Web API authorizes the caller and then calls another downstream Web API ([The Microsoft Graph](https://graph.microsoft.com)) after obtaining another [access token](https://docs.microsoft.com/azure/active-directory/develop/access-tokens) using the [on-behalf-of](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow) flow.
37
+
1. The Java Web API validates the access token using Spring Security and then calls another downstream Web API ([The Microsoft Graph](https://graph.microsoft.com)) after obtaining another [access token](https://docs.microsoft.com/azure/active-directory/develop/access-tokens) using the [on-behalf-of](https://docs.microsoft.com/azure/active-directory/develop/v2-oauth2-on-behalf-of-flow) flow.
38
38
39
39
## How to run this sample
40
40
@@ -48,7 +48,10 @@ To run this sample, you'll need:
48
48
49
49
To successfully use this sample, you need a working installation of [Java](https://openjdk.java.net/install/) and [Maven](https://maven.apache.org/).
50
50
51
-
### Step 2: Clone or download this repository
51
+
### Step 2: Clone or download this repository your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then **switch directory**.
52
+
Change your portal session to the desired Azure AD tenant.
53
+
1. In the portal menu, select the **Azure Active Directory** service, and then select **App registrations**.
54
+
52
55
53
56
From your shell or command line:
54
57
@@ -65,10 +68,8 @@ There are two projects in this sample. Each needs to be registered separately in
65
68
As a first step you'll need to:
66
69
67
70
1. Sign in to the [Azure portal](https://portal.azure.com) using either a work or school account or a personal Microsoft account.
68
-
1. If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then **switch directory**.
69
-
Change your portal session to the desired Azure AD tenant.
70
-
1. In the portal menu, select the **Azure Active Directory** service, and then select **App registrations**.
71
-
71
+
1. If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory. Change your portal session to the desired Azure AD tenant.
72
+
In the portal menu, select the Azure Active Directory service, and then select App registrations.
72
73
> 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*
73
74
74
75
#### Register the Web Api app (Java-webapi)
@@ -116,12 +117,12 @@ Open `application.properties` in the src/main/resources folder. Fill in with you
116
117
-*Enter_the_Application_Id_here* with the **Application (client) ID**.
117
118
-*Enter_the_Client_Secret_Here* with the **key value** noted earlier.
118
119
119
-
#### Register the client web app (Java_webapp)
120
+
#### Register the client web app (Java-webapp)
120
121
121
122
1. Navigate to the Microsoft identity platform for developers [App registrations](https://go.microsoft.com/fwlink/?linkid=2083908) page.
122
123
1. Click **New registration**.
123
124
1. In the **Register an application page** that appears, enter your application's registration information:
124
-
- In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `java_webapp`.
125
+
- In the **Name** section, enter a meaningful application name that will be displayed to users of the app, for example `java-webapp`.
125
126
- Change **Supported account types** to **Accounts in any organizational directory and personal Microsoft accounts (e.g. Skype, Xbox, Outlook.com)**.
126
127
> Note that there are more than one redirect URIs used in this sample. You'll need to add them from the **Authentication** tab later after the app has been created successfully.
127
128
1. Click on the **Register** button to create the application.
@@ -130,7 +131,6 @@ Open `application.properties` in the src/main/resources folder. Fill in with you
130
131
- In the **Platform configurations** section select **Add a platform** and create a new **Web** application
131
132
- Enter the following as the redirect URI: `http://localhost:8080/msal4jsample/secure/aad`
132
133
- Click on **Configure** to save your changes.
133
-
- Do the same for: `http://localhost:8080/msal4jsample/graph/me`
134
134
- Click the **Save** button to save the the redirect URI changes.
135
135
1. In the Application menu blade, click on the **Certificates & secrets** to open the page where we can generate secrets and upload certificates.
136
136
1. In the **Client secrets** section, click on **New client secret**:
@@ -151,7 +151,7 @@ Open `application.properties` in the msal-web-sample/src/main/resources folder.
151
151
152
152
- Replace *Enter_the_Application_Id_here* with the **Application (client) ID**.
153
153
- Replace *Enter_the_Client_Secret_Here* with the **key value** noted earlier.
154
-
- Replace *OboApi* with the API exposed in the `Web Api app`**(api://{clientId})**.
154
+
- Replace *Enter_the_Api_Scope_Here* with the API exposed in the `Web Api app`**(api://{clientId})**.
155
155
156
156
#### HTTPS on localhost
157
157
@@ -322,25 +322,12 @@ There are many key points in this sample to make the On-Behalf-Of-(OBO) flow wor
322
322
323
323
1. **ApiController** class
324
324
325
-
Contains the api(graphMeApi) to trigger the obo flow. The graphMeApi method gets the obo access token using **MsalAuthHelper**. The `callMicrosoftGraphEndPoint` method calls the Microsoft graph API using obo token.
Uses the [Java Microsoft Graph SDK](https://github.com/microsoftgraph/msgraph-sdk-java) to call the the api(graphMeApi). The `GraphServiceClient` uses the `oboAuthProvider` to acquire the necessary tokens to access the Graph Me endpoint.
334
326
335
-
- The **scope** [.default](https://docs.microsoft.com/azure/active-directory/developv2-permissions-and-consent#the-default-scope) is a built-in scope for every application that refers to the static list of permissions configured on the application registration. In our scenario here, it enables the user to grant consent for permissions for both the Web API and the downstream API (Microsoft Graph). For example, the permissions for the Web API and the downstream API (Microsoft Graph) are listed below:
336
-
- Web Api sample (access_as_user)
337
-
- Microsoft Graph (user.read)
338
-
339
-
- When you use the `.default` scope, the end user is prompted for a combined set of permissions that include scopes from both the **Web Api** and **Microsoft Graph**.
340
327
341
328
2. **SecurityResourceServerConfig** class
342
329
343
-
Token Validation of the caller happens in this class, where the access token presented by the client app is validated using Spring Security and another access token is obtained using the on-behalf-of flow
330
+
Token Validation of the caller happens in this class, where the access token presented by the client app is validated using Spring Security.
344
331
345
332
```Java
346
333
http
@@ -351,20 +338,50 @@ There are many key points in this sample to make the On-Behalf-Of-(OBO) flow wor
throw new AuthException(String.format("Error acquiring token from AAD: %s", ex.getMessage()),
374
+
ex.getCause());
375
+
}
376
+
}
367
377
```
378
+
Important things to notice:
379
+
- `application.acquireTokenSilently` is attempted first to try and use the cached tokens. If the silent call fails, the sample falls back to trying to acquire a token via obo.
380
+
- The **scope** [.default](https://docs.microsoft.com/azure/active-directory/developv2-permissions-and-consent#the-default-scope) is a built-in scope for every application that refers to the static list of permissions configured on the application registration. In our scenario here, it enables the user to grant consent for permissions for both the Web API and the downstream API (Microsoft Graph). For example, the permissions for the Web API and the downstream API (Microsoft Graph) are listed below:
381
+
- Web Api sample (access_as_user)
382
+
- Microsoft Graph (user.read)
383
+
384
+
- When you use the `.default` scope, the end user is prompted for a combined set of permissions that include scopes from both the **Web Api** and **Microsoft Graph**.
0 commit comments