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



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

相关文章
|
4月前
|
机器学习/深度学习 人工智能 弹性计算
阿里云服务器租用价格:最新包年包月、按量付费活动价格参考
阿里云服务器租用价格又更新了,租用阿里云轻量应用服务器一年价格是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等不同实例规格的活动价格,以供大家对比和选择参考。
789 13
|
4月前
|
人工智能 运维 安全
SOC 2.0 来了:不是加人加班,而是加“智能”!——智能化安全运营中心的建设之道
SOC 2.0 来了:不是加人加班,而是加“智能”!——智能化安全运营中心的建设之道
352 15
|
4月前
|
机器学习/深度学习 人工智能 运维
别只盯着 CPU 爆了!一篇文章带你看懂:从指标到根因的 AIOps 自动化故障定位流水线
别只盯着 CPU 爆了!一篇文章带你看懂:从指标到根因的 AIOps 自动化故障定位流水线
568 15
|
4月前
|
消息中间件 Java Kafka
kafka入门+代码初步实现--小白必看
kafka入门+代码初步实现--小白必看
380 5
|
4月前
|
人工智能 运维 Serverless
ModelScope 模型一键上线?FunModel 让你 5 分钟从零到生产
阿里云FunModel推出模型集成新范式,无缝对接ModelScope,支持0代码一键部署热门AI模型,5分钟完成上线。依托Serverless+GPU,实现弹性扩缩容,大幅降低部署门槛与运维成本,让企业高效拥抱AI时代。
|
4月前
|
人工智能 BI 开发工具
适合个人开发者的5款开发工具,开发者必须知道
2025年,个人开发者迎来工具黄金时代。本文精选5款高效开发利器:GitHub Copilot(AI智能编程)、Trae(中文友好)、Cursor(项目级理解)、VS Code(开源全能)和Zoho Creator(低代码平台),覆盖从代码生成到应用搭建,助你提升效率,快速实现创意。
1476 2
|
5月前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
1882 59
Meta SAM3开源:让图像分割,听懂你的话
|
5月前
|
人工智能 前端开发 算法
大厂CIO独家分享:AI如何重塑开发者未来十年
在 AI 时代,若你还在紧盯代码量、执着于全栈工程师的招聘,或者仅凭技术贡献率来评判价值,执着于业务提效的比例而忽略产研价值,你很可能已经被所谓的“常识”困住了脚步。
3153 90
大厂CIO独家分享:AI如何重塑开发者未来十年
|
5月前
|
机器学习/深度学习 人工智能 缓存
让AI评测AI:构建智能客服的自动化运营Agent体系
大模型推动客服智能化演进,从规则引擎到RAG,再到AI原生智能体。通过构建“评估-诊断-优化”闭环的运营Agent,实现对话效果自动化评测与持续优化,显著提升服务质量和效率。
2551 86
让AI评测AI:构建智能客服的自动化运营Agent体系
|
5月前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
4324 43