|
| 1 | +# Sample for Azure AD B2C Resource server Spring Boot client library for Java |
| 2 | + |
| 3 | +## Key concepts |
| 4 | +This sample illustrates how to use `azure-spring-boot-starter-active-directory-b2c` package to work in a resource server and valiate tokens. |
| 5 | + |
| 6 | +1. Constructs trusted iss by configuring tenant id. |
| 7 | +2. Obtain the access token from the HTTP request header. |
| 8 | +3. Analyze access token to `iss` and construct `JwtDecoder` by `AADIssuerJWSKeySelector`. |
| 9 | +4. Use `JwtDecoder` to parse the access token into `Jwt`. |
| 10 | +5. Verify `aud`, `iss`, `nbf`, `exp` claims in access token. |
| 11 | + |
| 12 | +## Getting started |
| 13 | +### Environment checklist |
| 14 | +We need to ensure that this [environment checklist][ready-to-run-checklist] is completed before the run. |
| 15 | + |
| 16 | +### Create and consent Application permissions |
| 17 | +1. On the **Azure AD B2C** Portal, select the application that requires roles to be added, select **Manifest**. |
| 18 | +2. Find the `appRoles` configuration item, and add the following configuration, then click the **Save** button. |
| 19 | +```json |
| 20 | + { |
| 21 | + "allowedMemberTypes": [ |
| 22 | + "Application" |
| 23 | + ], |
| 24 | + "description": "Task.read", |
| 25 | + "displayName": "Task.read", |
| 26 | + "id": "d2bec026-b75f-418d-9493-8462f54f25d9", |
| 27 | + "isEnabled": true, |
| 28 | + "value": "Test.read" |
| 29 | + }, |
| 30 | + { |
| 31 | + "allowedMemberTypes": [ |
| 32 | + "Application" |
| 33 | + ], |
| 34 | + "description": "Task.wirte", |
| 35 | + "displayName": "Task.wirte", |
| 36 | + "id": "1ab4eeda-d07e-4bce-8f77-b0a84c97c34f", |
| 37 | + "isEnabled": true, |
| 38 | + "value": "Test.wirte" |
| 39 | + } |
| 40 | +``` |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +3. Find the application permissions need to use. |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +4. Consent Application permissions. |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | +5. In the end, configuration is as follows. |
| 54 | + |
| 55 | + |
| 56 | + |
| 57 | +## Examples |
| 58 | +### Configure the sample |
| 59 | +#### application.yml |
| 60 | + |
| 61 | +```yaml |
| 62 | +# In v2.0 tokens, `aud` is always the client ID of the API, while in v1.0 tokens it can be the app id uri. |
| 63 | +azure: |
| 64 | + activedirectory: |
| 65 | + b2c: |
| 66 | + tenant-id: ${your-tenant-id} |
| 67 | + app-id-uri: ${your-app-id-uri} # If you are using v1.0 token, please configure app-id-uri for `aud` verification |
| 68 | + client-id: ${your-client-id} # If you are using v2.0 token, please configure client-id for `aud` verification |
| 69 | +``` |
| 70 | +
|
| 71 | +### Run with Maven |
| 72 | +``` |
| 73 | +cd azure-spring-boot-samples/azure-spring-boot-sample-active-directory-b2c-resource-server |
| 74 | +mvn spring-boot:run |
| 75 | +``` |
| 76 | + |
| 77 | +### Access the Web API |
| 78 | +We could use Postman to simulate a Web APP to send a request to a Web API. |
| 79 | + |
| 80 | +```http request |
| 81 | +GET /write HTTP/1.1 |
| 82 | +Authorization: Bearer eyJ0eXAiO ... 0X2tnSQLEANnSPHY0gKcgw |
| 83 | +``` |
| 84 | +```http request |
| 85 | +GET /read HTTP/1.1 |
| 86 | +Authorization: Bearer eyJ0eXAiO ... 0X2tnSQLEANnSPHY0gKcgw |
| 87 | +``` |
| 88 | + |
| 89 | +## Troubleshooting |
| 90 | +- `WWW-Authenticate: Bearer error="invalid_token", error_description="An error occurred while attempting to decode the Jwt: Couldn't retrieve remote JWK set: Read timed out",` |
| 91 | + |
| 92 | + While running sample, if error occurs with logs above: |
| 93 | + - `azure-activedirectory-b2c:jwt-read-timeout` to set longer read time in `application.yml`. |
| 94 | + |
| 95 | +### FAQ |
| 96 | +#### How do I delete or modify Application Permissions in Portal? |
| 97 | +You can set `isEnabled` to `false` in the manifest's JSON configuration.Then delete or modify it. |
| 98 | + |
| 99 | +## Next steps |
| 100 | +## Contributing |
| 101 | +<!-- LINKS --> |
| 102 | +[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 |
0 commit comments