Skip to content

Commit 34a28f2

Browse files
Update README.md file in the azure-spring-boot-sample-active-directory-resource-server (Azure#18361)
1 parent ae0c6a1 commit 34a28f2

17 files changed

+49
-73
lines changed

sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-active-directory-resource-server/README.md

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@
33
## Key concepts
44
This sample illustrates how to protect a Java web API by restricting access to its resources to authorized accounts only.
55

6-
1. The bearer token is obtained from the request header.
7-
2. `JwtDecoder` is used to parse the token into `Jwt`.
8-
3. Claims, headers etc in `Jwt` will be extracted, they will be wrapped in `AzureOAuth2AuthenticatedPrincipal` object.
9-
4. `AzureOAuth2AuthenticatedPrincipal` will eventually be set into SecurityContext.
6+
1. Obtain the access token from the HTTP request header.
7+
2. Use `JwtDecoder` to parse the access token into `Jwt`.
8+
3. Verify `aud`, `iss`, `nbf`, `exp` claims in access token.
9+
4. Extract information from JWT in `AADOAuth2AuthenticatedPrincipal` object after a successful verification.
10+
5. Save the `AADOAuth2AuthenticatedPrincipal` into SecurityContext.
1011

12+
### Protocol diagram
13+
![Aad resource server protocol diagram](docs/image-add-resource-server.png "Aad resource server protocol diagram")
1114

1215
## Getting started
13-
1416
### Environment checklist
1517
We need to ensure that this [environment checklist][ready-to-run-checklist] is completed before the run.
1618

17-
To run this sample, you'll need:
18-
- 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][How to get an Azure AD tenant]
19-
- You register your web APP in App registrations in the Azure portal.
20-
- A Web APP runtime that requires access to a Web API.
21-
22-
23-
## Include the package
19+
### Include the package
2420
```xml
25-
<dependencies>
21+
<dependencies>
2622
<dependency>
2723
<groupId>com.azure.spring</groupId>
2824
<artifactId>azure-spring-boot-starter-active-directory</artifactId>
@@ -35,64 +31,46 @@ To run this sample, you'll need:
3531
<groupId>org.springframework.security</groupId>
3632
<artifactId>spring-security-oauth2-jose</artifactId>
3733
</dependency>
38-
</dependencies>
34+
</dependencies>
3935
```
4036

41-
## Register your web API
42-
In this section, you register your web API in App registrations in the Azure portal.
43-
44-
### Choose your Azure AD tenant
45-
46-
To register your apps manually, choose the Azure Active Directory (Azure AD) tenant where you want to create your apps.
47-
48-
1. Sign in to the [Azure portal](https://portal.azure.com/) with either a work or school account or a personal Microsoft account.
49-
2. If your account is present in more than one Azure AD tenant, select your profile at the upper right, and then select **Switch directory**.
50-
3. Change your portal session to the Azure AD tenant you want to use.
51-
52-
### Register the web API
53-
54-
1. Go to the Microsoft identity platform for developers App registrations portal.
37+
### Configure Web API
38+
1. In this section, you register your web API in App registrations in the Azure portal.
39+
2. Search for and select your tenant in **Azure Active Directory**.
40+
3. Under **Manage** In the same tenant, select **App registrations** -> **New registration**.![Protal manage](docs/image-protal-manage.png "Protal manage")
41+
4. The registered application name is filled into `webapi`, select **Accounts in this organizational directory only**, click the **register** button.![Register a web api](docs/image-register-a-web-api.png "Register a web api")
42+
5. Under **webapi** application, select **Certificates & secrets** -> **new client secret**, expires select **Never**, click the **add** button, remember to save the secrets here and use them later.![Creat secrets](docs/image-creat-secrets-api.png "Creat secrets")
43+
6. Under **webapi** application, select **Expose an API** -> **Add a scope**, Use the default Application ID URI, click **Save and continue** button.![Set application id url](docs/image-set-application-id-url.png "Set application id url")
44+
7. After step five, the page will refresh again. Then set the **Scope name** to `File.Read`.![Add a scope](docs/image-add-a-scope.png "Add a scope")
45+
8. Finally, the api exposed in `webapi`.![Finally, the API exposed in webAPI](docs/image-expose-api.png "Finally, the API exposed in webAPI")
5546

56-
2. Select New registration.
57-
![Select New registration](docs/image-select-new-registration.png "Select new registration")
58-
59-
3. When the Register an application page opens, enter your application's registration information:
60-
![Scope Config](docs/image-register-an-application.png "Register an application")
61-
62-
4. In the **Expose an API** section, select **Add a scope**, accept the proposed Application ID URI `(api://{clientId})` (back up the Application ID URI here, which will be used in the properties file) by selecting **Save and Continue**.
63-
![App-Id-Uri Config](docs/image-app-id-uri-config.png "App-id-uri Config")
64-
Then enter the following information:
65-
- For **Scope name**, enter **File.read**.
66-
- For **Who can consent**, ensure that the **Admins and users** option is selected.
67-
- In the **Admin consent display name** box, enter **Access File.read as a user**.
68-
- In the **Admin consent description** box, enter **Accesses the File.read web API as a user**.
69-
- In the **User consent display name** box, enter **Access File.read as a user**.
70-
- In the **User consent description** box, enter **Accesses the File.read web API as a user**.
71-
- For **State**, keep **Enabled**.
72-
- Select **Add scope**.
73-
![Scope Config](docs/image-scope-configurations.png "Scope Config")
74-
75-
If you still don't understand, you can look at this [register app or web api][Register app or web API] and another [expose scoped permission to web api][Expose scoped permission to web API]. I believe it will also help you.
47+
See [Expose scoped permission to web api] for more information about web api.
7648

7749
## Examples
78-
79-
### Configure application.properties
80-
81-
```properties
82-
azure.activedirectory.client-id=xxxxxx-your-client-id-xxxxxx
83-
azure.activedirectory.app-id-uri=xxxxxxxx-app-id-uri-xxxxxxxxxx
84-
azure.activedirectory.session-stateless=true
50+
### Configure application.yml
51+
```yaml
52+
#If we configure the azure.activedirectory.client-id or azure.activedirectory.app-id-uri will be to check the audience.
53+
#In v2.0 tokens, this is always client id of the app, while in v1.0 tokens it can be the client id or the application id url used in the request.
54+
#If you are using v1.0 tokens, configure both to properly complete the audience validation.
55+
56+
azure:
57+
activedirectory:
58+
client-id: <client-id>
59+
app-id-uri: <app-id-uri>
8560
```
8661
87-
### Run with Maven
62+
### Run with Maven
8863
```shell
8964
# Under sdk/spring project root directory
90-
cd azure-spring-boot-samples/azure-spring-boot-sample-active-directory-spring-oauth2-resource-server
65+
cd azure-spring-boot-samples/azure-spring-boot-sample-active-directory-resource-server
9166
mvn spring-boot:run
9267
```
9368

9469
### Access the Web API
9570
We could use Postman to simulate a Web APP to send a request to a Web API.
71+
72+
**NOTE**: The `aud` in access token should be the current Web API.
73+
9674
```http request
9775
GET /file HTTP/1.1
9876
Authorization: Bearer eyJ0eXAiO ... 0X2tnSQLEANnSPHY0gKcgw
@@ -102,10 +80,9 @@ GET /user HTTP/1.1
10280
Authorization: Bearer eyJ0eXAiO ... 0X2tnSQLEANnSPHY0gKcgw
10381
```
10482

105-
### Check authorization
106-
107-
1. Access `file read` link, should success.
108-
2. Access `user read` link, should fail.
83+
### Check the authentication and authorization
84+
1. Access `http://localhost:<your-Configured-server-port>/file` link: success.
85+
2. Access `http://localhost:<your-Configured-server-port>/user` link: fail with error message.
10986

11087
## Troubleshooting
11188

@@ -114,6 +91,4 @@ Authorization: Bearer eyJ0eXAiO ... 0X2tnSQLEANnSPHY0gKcgw
11491
<!-- LINKS -->
11592
[jdk_link]: https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable
11693
[ready-to-run-checklist]: https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/spring/azure-spring-boot-samples/README.md#ready-to-run-checklist
117-
[Register app or web API]: https://docs.microsoft.com/azure/active-directory/develop/quickstart-register-app
118-
[Expose scoped permission to web API]: https://docs.microsoft.com/azure/active-directory/develop/quickstart-configure-app-expose-web-apis
119-
[How to get an Azure AD tenant]: https://azure.microsoft.com/documentation/articles/active-directory-howto-tenant
94+
[Expose scoped permission to web api]: https://docs.microsoft.com/azure/active-directory/develop/quickstart-configure-app-expose-web-apis
77 KB
Loading
Loading
333 KB
Loading
49.1 KB
Loading
57.1 KB
Loading
70.5 KB
Loading
67.5 KB
Loading
67.5 KB
Loading

0 commit comments

Comments
 (0)