【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]



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

相关文章
|
1月前
|
人工智能 运维 安全
SOC 2.0 来了:不是加人加班,而是加“智能”!——智能化安全运营中心的建设之道
SOC 2.0 来了:不是加人加班,而是加“智能”!——智能化安全运营中心的建设之道
200 15
|
1月前
|
机器学习/深度学习 人工智能 运维
别只盯着 CPU 爆了!一篇文章带你看懂:从指标到根因的 AIOps 自动化故障定位流水线
别只盯着 CPU 爆了!一篇文章带你看懂:从指标到根因的 AIOps 自动化故障定位流水线
277 15
|
1月前
|
人工智能 运维 Serverless
ModelScope 模型一键上线?FunModel 让你 5 分钟从零到生产
阿里云FunModel推出模型集成新范式,无缝对接ModelScope,支持0代码一键部署热门AI模型,5分钟完成上线。依托Serverless+GPU,实现弹性扩缩容,大幅降低部署门槛与运维成本,让企业高效拥抱AI时代。
|
1月前
|
消息中间件 Java Kafka
kafka入门+代码初步实现--小白必看
kafka入门+代码初步实现--小白必看
248 5
|
2月前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
1559 89
大厂CIO独家分享:AI如何重塑开发者未来十年
|
1月前
|
人工智能 BI 开发工具
适合个人开发者的5款开发工具,开发者必须知道
2025年,个人开发者迎来工具黄金时代。本文精选5款高效开发利器:GitHub Copilot(AI智能编程)、Trae(中文友好)、Cursor(项目级理解)、VS Code(开源全能)和Zoho Creator(低代码平台),覆盖从代码生成到应用搭建,助你提升效率,快速实现创意。
613 3
|
2月前
|
机器学习/深度学习 人工智能 缓存
让AI评测AI:构建智能客服的自动化运营Agent体系
大模型推动客服智能化演进,从规则引擎到RAG,再到AI原生智能体。通过构建“评估-诊断-优化”闭环的运营Agent,实现对话效果自动化评测与持续优化,显著提升服务质量和效率。
1662 86
让AI评测AI:构建智能客服的自动化运营Agent体系
|
2月前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
1427 59
Meta SAM3开源:让图像分割,听懂你的话
|
1月前
|
存储 运维 监控
从“看曲线”到“懂问题”:MetricSet Explorer 如何重构指标分析体验
告警太多看不过来?MetricSet Explorer 来帮你“挑重点”:自动识别异常、智能分组聚类、一键定位根因,让百万级指标也能秒级洞察!
164 23
|
1月前
|
移动开发 小程序 前端开发
小程序开发平台有哪些?哪个好
小程序项目落地的第一步,也是最关键的一步,就是开发平台的精准选型。它不仅影响项目的开发周期与成本投入,更直接决定了后续业务的适配度和运营上限。企业需结合自身技术能力、预算区间、功能需求等核心要素综合权衡。本文将对主流小程序开发平台进行分类拆解,通过详细对比和场景化推荐,帮助不同类型的企业找到最契合的解决方案。
348 9