java - 生成用于在 Java 中访问 CR 的身份验证 token

标签 java azure azure-active-directory azure-acr

我正在使用服务主体对 Azure 进行身份验证,并且我想将 shell 脚本转换为 Java。我的 shell 脚本代码基本上是这样做的:

export AAD_ACCESS_TOKEN=$(az account get-access-token --query accessToken -o tsv)

export ACR_REFRESH_TOKEN=$(curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" \
    -d "grant_type=access_token&service=$REGISTRY&access_token=$AAD_ACCESS_TOKEN" \
    https://$REGISTRY/oauth2/exchange \
    | jq '.refresh_token' \
    | sed -e 's/^"//' -e 's/"$//')
echo "ACR Refresh Token obtained."
# Create the repo level scope
SCOPE="repository:$REPOSITORY:pull"

# to pull multiple repositories passing in multiple scope arguments.
#&scope="repository:repo:pull,push"

export ACR_ACCESS_TOKEN=$(curl -s -X POST -H "Content-Type: application/x-www-form-urlencoded" \
    -d "grant_type=refresh_token&service=$REGISTRY&scope=$SCOPE&refresh_token=$ACR_REFRESH_TOKEN" \
    https://$REGISTRY/oauth2/token \
    | jq '.access_token' \
    | sed -e 's/^"//' -e 's/"$//')
echo "ACR Access Token obtained."

我正在尝试寻找 Java 的等价物

az account get-access-token --query accessToken -o tsv

curl -s -X POST -H“内容类型:application/x-www-form-urlencoded”-d“grant_type=access_token&service=$REGISTRY&access_token=$AAD_ACCESS_TOKEN” https://$REGISTRY/oauth2/交换| jq '.refresh_token' | jq '.refresh_token' | sed -e 's/^"//' -e 's/"$//'

curl -s -X POST -H“内容类型:application/x-www-form-urlencoded”-d“grant_type=refresh_token&service=$REGISTRY&scope=$SCOPE&refresh_token=$ACR_REFRESH_TOKEN” https://$注册表/oauth2/ token | jq '.access_token' | jq '.access_token' | sed -e 's/^"//' -e 's/"$//'

但还没有真正找到任何有关如何使用 Java 执行此操作的文档。我发现了这个:https://github.com/AzureAD/azure-activedirectory-library-for-java/wiki/Acquire-tokens用于获取 AAD token ,但该页面上没有任何内容告诉我如何使用服务主体来执行此操作。

最佳答案

你需要在java中实现这个curls,你可以使用像https://github.com/square/okhttp/blob/master/README.md这样的restclient

关于java - 生成用于在 Java 中访问 CR 的身份验证 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57317719/

相关文章:

java - 将字符串传递给字符串数组引用

azure - 在 AKS 中启动 Docker 镜像

amazon-web-services - 在 AWS、Azure 或 Google Cloud 等主要参与者托管时,我可以相信我的代码和表结构不会被复制吗?

azure - 获取 .NET Azure Active Directory 组中的所有者

azure - 自动将用户分配到应用程序中的特定应用程序角色

java - 使用 Integer.MAX_VALUE 条目创建 ArrayBlockingQueue 时出现 OutOfmemory 错误

java - 在没有成员变量的情况下通过实现创建抽象类有什么意义?

Java 杀毒软件……有可能吗?如何?

azure - 通知中心的.Net 移动服务后端

c# - 如何将 IGroupMembersCollectionWithReferencesPage 从 Azure AD 转换为 C# 中的列表/集合?