【问题标题】:Generate auth token for accessing CR in Java生成用于在 Java 中访问 CR 的身份验证令牌
【发布时间】:2019-08-01 22:26:56
【问题描述】:

我正在使用服务主体对 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 "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/"$//'

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/"$//'

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 这将是我最后的手段,但有没有某种本机 Java 客户端可以完成这些 Curl 命令的功能? IE。获取 AAD 访问令牌、ACR 刷新令牌和 ACR 访问令牌。我仔细研究了 GitHub 上的 MSFT Azure Java 存储库,但只看到了直接进行身份验证的方法,而不是返回令牌。
    猜你喜欢
    • 1970-01-01
    • 2018-02-20
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多