当前位置: 首页 > backend >正文

java从azure中读取用户信息

以下是用 Java 从 Azure AD 获取用户信息的完整实现方案,使用 Spring Boot 框架和 Microsoft 身份验证库 (MSAL):
 
1. 添加 Maven 依赖

<dependencies>
    <!-- Spring Boot Web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Azure AD MSAL -->
    <dependency>
        <groupId>com.microsoft.azure</groupId>
        <artifactId>msal4j</artifactId>
        <version>1.13.3</version>
    </dependency>
    
    <!-- JWT 处理 -->
    <dependency>
        <groupId>com.nimbusds</groupId>
        <artifactId>nimbus-jose-jwt</artifactId>
        <version>9.25</version>
    </dependency>
    
    <!-- HTTP 客户端 -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.13</version>
    </dependency>
</dependencies>
 

2. 配置 Azure AD 参数
在  application.properties  中:

# Azure AD 配置
azure.client-id=your_client_id
azure.client-secret=your_client_secret
azure.tenant-id=your_tenant_id
azure.redirect-uri=https://your-app.com/auth/redirect
azure.scope=openid profile User.Read
 

3. 控制器实现

import com.microsoft.aad.msal4j.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.net.MalformedURLException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@RestController
public class AuthController {
    
    @Value("${azure.client-id}")
    private String clientId;
    
    @Value("${azure.client-secret}")
    private String clientSecret;
    
    @Value("${azure.tenant-id}")
    private String tenantId;
    
    @Value("${azure.redirect-uri}")
    private String redirectUri;
    
    @Value("${azure.scope}")
    private String scope;
    
    // 第一步:生成登录URL
    @GetMapping("/login")
    public Map<String, String> login() throws MalformedURLException {
        String authUrl = getAuthUrl();
        return Collections.singletonMap("loginUrl", authUrl);
    }
    
    // 第二步:处理回调
    @GetMapping("/auth/redirect")
    public Map<String, Object> handleRedirect(
            @RequestParam("code") String authCode,
            @RequestParam("state") String state
    ) throws Exception {
        
        // 使用授权码获取令牌
        IAuthenticationResult result = acquireToken(authCode);
        
        // 获取用户信息
        Map<String, Object> userInfo = getUserInfo(result.accessToken());
        
        // 返回用户信息
        Map<String, Object> response = new HashMap<>();
        response.put("id_token_claims", result.idToken());
        response.put("user_info", userInfo);
        
        return response;
    }
    
    // 生成认证URL
    private String getAuthUrl() throws MalformedURLException {
        ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                clientId, ClientCredentialFactory.createFromSecret(clientSecret))
                .authority("https://login.microsoftonline.com/" + tenantId)
                .build();
        
        AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder(
                authCode, 
                new URI(redirectUri)
            ).scopes(Collections.singleton(scope))
             .build();
        
        String authorizationUrl = app.getAuthorizationRequestUrl(
                AuthorizationRequestUrlParameters
                    .builder(redirectUri, Collections.singleton(scope))
                    .build()
            ).toString();
        
        return authorizationUrl;
    }
    
    // 使用授权码获取令牌
    private IAuthenticationResult acquireToken(String authCode) 
        throws MalformedURLException, ExecutionException, InterruptedException {
        
        ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                clientId, ClientCredentialFactory.createFromSecret(clientSecret))
                .authority("https://login.microsoftonline.com/" + tenantId)
                .build();
        
        AuthorizationCodeParameters parameters = AuthorizationCodeParameters.builder(
                authCode, 
                new URI(redirectUri)
            ).scopes(Collections.singleton(scope))
             .build();
        
        return app.acquireToken(parameters).get();
    }
    
    // 使用访问令牌获取用户信息
    private Map<String, Object> getUserInfo(String accessToken) {
        String graphEndpoint = "https://graph.microsoft.com/v1.0/me";
        
        // 使用HttpClient调用Graph API
        // 实际实现需要添加错误处理和JSON解析
        return fetchUserDataFromGraph(graphEndpoint, accessToken);
    }
    
    // 调用Microsoft Graph API的实现
    private Map<String, Object> fetchUserDataFromGraph(String endpoint, String accessToken) {
        // 这里使用HttpClient简化实现
        // 实际项目使用RestTemplate或WebClient
        
        HttpGet request = new HttpGet(endpoint);
        request.setHeader("Authorization", "Bearer " + accessToken);
        
        try (CloseableHttpClient httpClient = HttpClients.createDefault();
             CloseableHttpResponse response = httpClient.execute(request)) {
            
            String json = EntityUtils.toString(response.getEntity());
            return new Gson().fromJson(json, new TypeToken<Map<String, Object>>(){}.getType());
            
        } catch (Exception e) {
            throw new RuntimeException("Failed to fetch user info", e);
        }
    }
}
 

4. 安全配置类 (可选)

import com.nimbusds.jose.jwk.source.JWKSource;
import com.nimbusds.jose.jwk.source.RemoteJWKSet;
import com.nimbusds.jose.proc.JWSVerificationKeySelector;
import com.nimbusds.jose.proc.SecurityContext;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.proc.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.MalformedURLException;
import java.net.URL;

@Configuration
public class SecurityConfig {

    @Value("${azure.tenant-id}")
    private String tenantId;
    
    // 配置JWT验证器
    @Bean
    public ConfigurableJWTProcessor<SecurityContext> jwtProcessor() 
        throws MalformedURLException {
        
        // Azure AD JWKS端点
        String jwksUrl = String.format(
            "https://login.microsoftonline.com/%s/discovery/v2.0/keys", 
            tenantId
        );
        
        // 设置JWT处理器
        DefaultJWTProcessor<SecurityContext> jwtProcessor = 
            new DefaultJWTProcessor<>();
        
        // 配置JWK来源
        JWKSource<SecurityContext> keySource = 
            new RemoteJWKSet<>(new URL(jwksUrl));
        
        // 配置签名验证
        JWSAlgorithm expectedJWSAlg = JWSAlgorithm.RS256;
        JWSVerificationKeySelector<SecurityContext> keySelector = 
            new JWSVerificationKeySelector<>(expectedJWSAlg, keySource);
        
        jwtProcessor.setJWSKeySelector(keySelector);
        
        // 设置JWT声明验证器
        jwtProcessor.setJWTClaimsSetVerifier(new DefaultJWTClaimsVerifier<>(
            new JWTClaimsSet.Builder().build(),
            new HashSet<>(Arrays.asList("sub", "aud", "exp", "iat"))));
        
        return jwtProcessor;
    }
}
 

 

http://www.xdnf.cn/news/11869.html

相关文章:

  • Kafka入门- 基础命令操作指南
  • NBA名人堂之-查尔斯·巴克利|里克·巴里|罗伯特·帕里什|斯科蒂·皮蓬|戴夫·德布斯切尔
  • VC++制作连连看辅助经验分享
  • Preference 系统自带的偏好设置页面解析
  • android中的TextWatcher的应用
  • Android应用获取root权限,修改系统文件
  • CentOS搭建并配置Nexus3记录
  • 干货整理:电脑监控软件都有哪些,哪款好用( 六大好用监控电脑软件推荐)
  • gps wifi信号测试软件,搜星+WiFi+EMI 导航平板信号性能横评
  • vb与EXCEL的连接
  • 由于找不到d3dx9_39.dll,无法继续执行代码的5种解决方法
  • 串口数据转以太网
  • Java入门基础,Java知识点总结合集
  • 迅雷老版本下载地址
  • 【学术相关】最新整理!绝对不能错过的130个学术网站和26个科研工具
  • OA系统:一文读懂OA系统,内附优秀OA界面。
  • nginx配置文件特殊字符说明
  • python实现抖音上比较火的罗盘时钟
  • 逆向工程入门学习(FreeBuf)
  • 黑马程序员 JavaScript
  • Iocomp Crack和ProEssentials Crack
  • 百度竞价点击器_百度竞价如何屏蔽恶意点击
  • Windows 7 RC十大功能的详细介绍以及改进方法
  • 【推荐】10个网站分类目录提交地址
  • linux下清理信号量,Linux下进程间通信方式——信号量(Semaphore)
  • JAVAscript的发展及八大数据类型
  • win10系统魔兽世界无法连接服务器地址,win10系统无法更新魔兽世界提示正在等待另一项安...
  • 罗马音平假字复制_罗马音平假名和片假名大全ID,罗马音平假名和片假名大全复制中文[多图]...
  • index of 的高级搜索命令
  • 对ROS局部运动规划器Teb的理解