This sample illustrates how to use spring-cloud-azure-starter-active-directory
package to work with OAuth 2.0 and OpenID Connect protocols on Azure. This sample will use Microsoft Graph API to retrieve user information.
- Search for and select your tenant in Microsoft Entra ID.
- Under Manage In the same tenant, select App registrations -> New registration.
- The registered application name is filled into
webapp
, select Accounts in this organizational directory only, click the register button. - Under webapp application, select Certificates & secrets -> new client secret, click the add button.(Remember to save the secrets here and use them later.)
- Under webapp application, select Authentication -> Add a platform, select web platform, redirect urls set to
http://localhost:8080/login/oauth2/code/
, click configure button. - Under webapp application, select API permissions -> Add a permission, select Microsoft Graph. Next, search
Directory.Read.All
via select Permissions, check the check box, click add permissions button.(User.Read
is created automatically, we need to keep it.) - Similarly, add permission user_impersonation in Azure Service Management,
See Register app, Grant scoped permission for more information about web app.
In order to try the authorization action with this sample with minimum effort, configure the user and groups in Microsoft Entra ID, configure the user with group1
.
If you want to use id_token
for authorization, the appRoles
feature of Microsoft Entra ID is supported which is presented in id_token's roles
claim. By following below configurations, GrantedAuthority
can be generated from roles
claim.
Note:
- The
roles
claim generated fromappRoles
is decorated with prefixAPPROLE_
. - When using
appRoles
asroles
claim, please avoid configuring group attribute asroles
at the same time. The latter will override the claim to contain group information instead ofappRoles
. Below configuration in manifest should be avoided:"optionalClaims": { "idtoken": [{ "name": "groups", "additionalProperties": ["emit_as_roles"] }] }
Follow the guide to add app roles in your application.
- In this example you need to create following
appRoles
in your application's manifest:"appRoles": [ { "allowedMemberTypes": [ "User" ], "displayName": "Admin", "id": "2fa848d0-8054-4e11-8c73-7af5f1171001", "isEnabled": true, "description": "Full admin access", "value": "Admin" } ]
- After you've created the roles go to your Enterprise Application in Azure Portal, select "Users and groups" and assign the new roles to your Users (assignment of roles to groups is not available in the free tier of Microsoft Entra ID).
This is an optional configuration. This guide is for accessing Resource Server Obo. If you want to use webapp to access other resource server (for example, access Resource Server Obo or Resource Server or custom resource server), you can refer to this guide.
- First you need to complete config for resource server obo and make sure to expose the scope of
Obo.WebApiA.ExampleScope
. - Select API permissions > Add a permission > My APIs, select Web API A application name.
- Delegated permissions is selected by default, Select Obo.WebApiA.ExampleScope permission, select Add permission to complete the process.
- Grant admin consent for Web API A permissions.
- Enable webapiA client in
application.yml
.
# WebapiA is an optional client, we can access obo resource servers or the other custom server.
spring:
cloud:
azure:
active-directory:
enabled: true
credential:
client-id: ${AZURE_CLIENT_ID}
client-secret: ${AZURE_CLIENT_SECRET}
profile:
tenant-id: ${AZURE_TENANT_ID}
user-group:
allowed-group-names: <group1>,<group2>
allowed-group-ids: <group1-id>,<group2-id> # When 'all' is used, all group id can be obtained.
post-logout-redirect-uri: http://localhost:8080
authorization-clients:
arm:
on-demand: true
scopes: https://management.core.windows.net/user_impersonation
graph:
scopes:
- https://graph.microsoft.com/User.Read
- https://graph.microsoft.com/Directory.Read.All
# webapiA:
# scopes:
# - ${WEB_API_A_APP_ID_URL}/Obo.WebApiA.ExampleScope
# enable-full-list is used to control whether to list all group ids, default is false
# It's suggested the logged in user should at least belong to one of the above groups
# If not, the logged in user will not be able to access any authorization controller rest APIs
cd azure-spring-boot-samples/aad/spring-cloud-azure-starter-active-directory/web-client-access-resource-server/aad-web-application
mvn spring-boot:run
# Or use the below command to the Microsoft Entra conditional access filter.
mvn spring-boot:run -Dspring-boot.run.profiles=default,conditional-access
- Access http://localhost:8080
- Login
- Access
Group1 Message
link: success - Access
Group2 Message
link: fail with forbidden error message - Access
Admin Message
link: fail with forbidden error message - Access
Graph Client
link: access token forMicrosoft Graph
will be acquired, and the content of customized OAuth2AuthorizedClient instance forMicrosoft Graph
resource will be displayed. - Access
Arm Client
link: page will be redirected to Consent page for on-demand authorization ofuser_impersonation
permission inAzure Service Management
resource. Clicking onConsent
, access token forAzure Service Management
will be acquired, the content of customized OAuth2AuthorizedClient instance forAzure Service Management
resource will be displayed. - Access
Obo Client
link: access token forwebapiA
will be acquired, the success or failure of accessingwebapiA
will be displayed. - Access
Client Credential Client
link: success.
In your application.yml file:
spring:
cloud:
azure:
active-directory:
enabled: true
profile:
tenant-id: ${AZURE_TENANT_ID}
Meet with AADSTS240002: Input id_token cannot be used as 'urn:ietf:params:oauth:grant-type:jwt-bearer' grant
error.
In Azure portal, app registration manifest page, configure oauth2AllowImplicitFlow
in your application manifest to true
. See this issue for details on this workaround.