如何通过本地Java程序或其他语言获取Azure用户Object ID?
在本地Java程序中获取Azure用户Object ID的实现方案
场景1:通过用户登录认证获取当前用户的Object ID(Web/桌面应用通用)
前置配置
- 在Azure门户创建应用注册,设置适配本地程序的重定向URI(如
http://localhost:8080/login/oauth2/code/azure) - 为应用添加User.Read委托权限,并完成管理员授权
- 记录应用的客户端ID、租户ID,若用密钥认证则生成客户端密钥
Spring Boot项目实现示例
添加依赖(Maven):
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency>
配置application.yml:
spring: security: oauth2: client: registration: azure: client-id: <你的客户端ID> client-secret: <你的客户端密钥> scope: openid, profile, User.Read authorization-grant-type: authorization_code provider: azure: issuer-uri: https://login.microsoftonline.com/<你的租户ID>/v2.0
获取用户Object ID的代码:
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.oauth2.core.oidc.user.OidcUser; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class UserController { @GetMapping("/get-object-id") public String getCurrentUserObjectId(@AuthenticationPrincipal OidcUser oidcUser) { // 从ID Token的oid字段直接获取用户全局Object ID return oidcUser.getClaims().get("oid").toString(); } }
场景2:无用户登录,通过客户端凭证流获取指定用户的Object ID
前置配置
- 应用注册添加User.Read.All应用权限,并完成管理员授权
- 生成并记录客户端密钥
使用Microsoft Graph SDK实现
添加依赖(Maven):
<dependency> <groupId>com.microsoft.graph</groupId> <artifactId>microsoft-graph</artifactId> <version>6.3.0</version> </dependency> <dependency> <groupId>com.azure</groupId> <artifactId>azure-identity</artifactId> <version>1.12.0</version> </dependency>
查询指定用户Object ID的代码:
import com.azure.identity.ClientSecretCredential; import com.azure.identity.ClientSecretCredentialBuilder; import com.microsoft.graph.models.User; import com.microsoft.graph.requests.GraphServiceClient; public class UserObjectIdFetcher { public static void main(String[] args) { String tenantId = "<你的租户ID>"; String clientId = "<你的客户端ID>"; String clientSecret = "<你的客户端密钥>"; String targetUserEmail = "user@example.com"; // 目标用户邮箱 // 构建认证凭证 ClientSecretCredential credential = new ClientSecretCredentialBuilder() .tenantId(tenantId) .clientId(clientId) .clientSecret(clientSecret) .build(); // 初始化Graph客户端 GraphServiceClient graphClient = GraphServiceClient.builder() .authenticationProvider(request -> { String accessToken = credential.getToken("https://graph.microsoft.com/.default").block().getToken(); request.addHeader("Authorization", "Bearer " + accessToken); }) .buildClient(); // 通过邮箱查询用户并获取Object ID User targetUser = graphClient.users() .buildRequest() .filter("mail eq '" + targetUserEmail + "'") .get() .getCurrentPage() .get(0); System.out.println("目标用户Object ID: " + targetUser.id); } }
关键注意事项
- 确认应用权限已完成授权,否则会触发403权限不足错误
- 本地程序需确保网络可访问Azure AD和Graph API端点
- 桌面应用可替换为
DeviceCodeCredential实现交互式认证,适配无浏览器的环境
内容的提问来源于stack exchange,提问作者duducheng
相关产品推荐
相关产品推荐

