【Azure AI Search】如何通过Entra ID RBAC认证连接中国区 Azure AI Search

简介: 本文介绍如何在Java SDK中配置中国区AI Search资源访问。由于默认认证地址为全球环境(https://search.azure.com),在中国区需修改为https://search.azure.cn,并通过设置SearchAudience.AZURE_CHINA解决认证失败问题,确保资源正常获取。

问题描述

使用Java SDK来获取中国区 AI Search资源,使用如下代码:

package org.yourcompany.yourproject;
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenRequestContext;
import com.azure.identity.AzureAuthorityHosts;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.search.documents.SearchClient;
import com.azure.search.documents.SearchClientBuilder;
import com.azure.search.documents.models.SearchAudience;
public class Javademo1 {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        String indexName = "xxxxx";
        // Get the service endpoint from the environment
        String endpoint = "https://<your ai service name>.search.azure.cn";
        String authorityHost = AzureAuthorityHosts.AZURE_CHINA;
        DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().authorityHost(authorityHost).build();
        // Create a client
        SearchClient client = new SearchClientBuilder()
                .endpoint(endpoint)
                .indexName(indexName)
                .credential(credential)
                .buildClient();
        long documentCount = client.getDocumentCount();
        System.out.println("Document count: " + documentCount);
    }
}

当运行时,会获取到如下错误:

com.microsoft.aad.msal4j.MsalServiceException: AADSTS500011: The resource principal named https://search.azure.com was not found in the tenant named XXXXXXXX. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You might have sent your authentication request to the wrong tenant.

那么,应该如何修改代码,让它正常运行呢?

 

问题解答

因为JDK中,SearchClient 默认的认证是在Global 环境中,所以使用的默认地址为https://search.azure.com,而在中国区,则需要修改为https://search.azure.cn 。参考官方文档介绍,可以在构建SearchClientBuilder对象时设置 audience值为 SearchAudience.AZURE_CHINA。

修改后的代码为

        String indexName = "xxxxx";
        // Get the service endpoint from the environment
        String endpoint = "https://<your ai service name>.search.azure.cn";
        String authorityHost = AzureAuthorityHosts.AZURE_CHINA;
        DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().authorityHost(authorityHost).build();
        // Create a client
        SearchClient client = new SearchClientBuilder()
                .endpoint(endpoint)
                .indexName(indexName)
                .credential(credential)
                .audience(SearchAudience.AZURE_CHINA) //set the audience of your cloud
                .buildClient();
        long documentCount = client.getDocumentCount();


如果想要单独获取AI Search资源的Token,可以使用如下代码:


        String authorityHost = AzureAuthorityHosts.AZURE_CHINA;
        DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().authorityHost(authorityHost).build();
        TokenRequestContext ctx = new TokenRequestContext()
                .addScopes("https://search.azure.cn/.default");
        AccessToken accessToken = credential.getTokenSync(ctx);
        String token = accessToken.getToken();
        System.out.println("Token: " + token);


获取到的Token,通过 https://jwt.io  解析后查看 aud的值就是 https://search.azure.cn

 

附件 : Dome的全部代码

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.yourcompany.yourproject</groupId>
    <artifactId>javademo1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.release>17</maven.compiler.release>
        <exec.mainClass>org.yourcompany.yourproject.Javademo1</exec.mainClass>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.3</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-search-documents</artifactId>
            <version>11.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-identity</artifactId>
            <version>1.16.3</version>
        </dependency>
    </dependencies>
</project>


Javademo1.java

/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 */
package org.yourcompany.yourproject;
import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenRequestContext;
import com.azure.identity.AzureAuthorityHosts;
import com.azure.identity.DefaultAzureCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.search.documents.SearchClient;
import com.azure.search.documents.SearchClientBuilder;
import com.azure.search.documents.models.SearchAudience;
public class Javademo1 {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        String indexName = "xxxxx";
        // Get the service endpoint from the environment
        String endpoint = "https://<your ai service name>.search.azure.cn";
        String authorityHost = AzureAuthorityHosts.AZURE_CHINA;
        DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().authorityHost(authorityHost).build();
        TokenRequestContext ctx = new TokenRequestContext()
                .addScopes("https://search.azure.cn/.default");
        AccessToken accessToken = credential.getTokenSync(ctx);
        String token = accessToken.getToken();
        System.out.println("Token: " + token);
        // Create a client
        SearchClient client = new SearchClientBuilder()
                .endpoint(endpoint)
                .indexName(indexName)
                .credential(credential)
                .audience(SearchAudience.AZURE_CHINA) //set the audience of your cloud
                .buildClient();
        long documentCount = client.getDocumentCount();
        System.out.println("Document count: " + documentCount);
        System.out.println("Search client created: " + client.getEndpoint());
    }
}

 

 

参考资料

Create a client using Microsoft Entra ID authentication : https://learn.microsoft.com/zh-cn/java/api/overview/azure/search-documents-readme?view=azure-java-stable#create-a-client-using-microsoft-entra-id-authentication

Authenticate in a National Cloud : https://learn.microsoft.com/zh-cn/java/api/com.azure.search.documents?view=azure-java-stable

 

[END]



当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

相关文章
|
2月前
|
机器学习/深度学习 人工智能 弹性计算
阿里云服务器租用价格:最新包年包月、按量付费活动价格参考
阿里云服务器租用价格又更新了,租用阿里云轻量应用服务器一年价格是38元,经济型e实例2核2G3M带宽 40G ESSD Entry云盘特惠价99元1年,通用算力型u1实例2核4G5M带宽80G ESSD Entry云盘特惠价199元1年。通用算力型u2i实例4核8G1170.26元1年起。本文为大家展示本次价格更新之后,阿里云服务器的最新租用价格,包含经济型e、通用算力型u2i/u2a、计算型c9i/c9a、通用型g9i/g9a、内存型r9i/r9a等不同实例规格的活动价格,以供大家对比和选择参考。
386 13
|
2月前
|
人工智能 运维 安全
SOC 2.0 来了:不是加人加班,而是加“智能”!——智能化安全运营中心的建设之道
SOC 2.0 来了:不是加人加班,而是加“智能”!——智能化安全运营中心的建设之道
241 15
|
2月前
|
人工智能 运维 Serverless
ModelScope 模型一键上线?FunModel 让你 5 分钟从零到生产
阿里云FunModel推出模型集成新范式,无缝对接ModelScope,支持0代码一键部署热门AI模型,5分钟完成上线。依托Serverless+GPU,实现弹性扩缩容,大幅降低部署门槛与运维成本,让企业高效拥抱AI时代。
|
3月前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
1844 89
大厂CIO独家分享:AI如何重塑开发者未来十年
|
3月前
|
机器学习/深度学习 人工智能 缓存
让AI评测AI:构建智能客服的自动化运营Agent体系
大模型推动客服智能化演进,从规则引擎到RAG,再到AI原生智能体。通过构建“评估-诊断-优化”闭环的运营Agent,实现对话效果自动化评测与持续优化,显著提升服务质量和效率。
1890 86
让AI评测AI:构建智能客服的自动化运营Agent体系
|
2月前
|
机器学习/深度学习 人工智能 运维
别只盯着 CPU 爆了!一篇文章带你看懂:从指标到根因的 AIOps 自动化故障定位流水线
别只盯着 CPU 爆了!一篇文章带你看懂:从指标到根因的 AIOps 自动化故障定位流水线
359 15
|
2月前
|
存储 运维 监控
从“看曲线”到“懂问题”:MetricSet Explorer 如何重构指标分析体验
告警太多看不过来?MetricSet Explorer 来帮你“挑重点”:自动识别异常、智能分组聚类、一键定位根因,让百万级指标也能秒级洞察!
202 31
|
2月前
|
Java Nacos Sentinel
SpringCloud 微服务解决方案:企业级架构实战
全面介绍 SpringCloud 微服务解决方案,涵盖服务注册发现、网关路由、熔断限流、分布式事务等企业级实践
|
2月前
|
存储 运维 vr&ar
实时云渲染与云桌面解析(二):从云桌面到实时云渲染:图形计算云化的下一站
实时云渲染技术通过云端渲染、终端显示的模式,解决了延迟和性能问题,支持多端接入和快速部署。相比云桌面,实时云渲染更适用于3D设计、VR等图形密集型场景,具有低延迟、弹性扩展等优势。随着5G和边缘计算发展,实时云渲染正推动图形计算向"云-边-端"协同演进,成为数字化转型的重要技术支撑。
|
2月前
|
人工智能 缓存 运维
【本不该故障系列】从 runC 到 runD:SAE 如何化解安全泄露风险
阿里云SAE默认采用runD安全容器,通过轻量虚拟化实现硬件级隔离,彻底解决runC共享内核导致的逃逸、噪声邻居、侧信道攻击等多租户安全风险。