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