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

本文涉及的产品
云原生网关 MSE Higress,422元/月
应用实时监控服务-可观测链路OpenTelemetry版,每月50GB免费额度
MSE Nacos/ZooKeeper 企业版试用,1600元额度,限量50份
简介: 本文介绍如何在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天前
|
云安全 人工智能 自然语言处理
|
9天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
670 57
Meta SAM3开源:让图像分割,听懂你的话
|
7天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
321 116
|
6天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
|
22天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
9天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
450 33
|
5天前
|
弹性计算 人工智能 Cloud Native
阿里云无门槛和有门槛优惠券解析:学生券,满减券,补贴券等优惠券领取与使用介绍
为了回馈用户与助力更多用户节省上云成本,阿里云会经常推出各种优惠券相关的活动,包括无门槛优惠券和有门槛优惠券。本文将详细介绍阿里云无门槛优惠券的领取与使用方式,同时也会概述几种常见的有门槛优惠券,帮助用户更好地利用这些优惠,降低云服务的成本。
278 133