如何使用 azure 活动目录对访问我的 finatra rest api (Scala) 的用户进行身份验证

How to authenticate user accessing my finatra rest api (Scala) with azure active directory

我在 Finatra 上有一个 Scala 休息服务,我想对使用 Azure Active Directory 访问我的休息服务的用户进行身份验证。

目前,我可以通过 curl 获取访问令牌:

curl -s -X POST https://login.microsoftonline.com/tenant id/oauth2/token -d grant_type=password -d username=$username -d password=$pass  -d resource=$resID -d client_id=$id -d client_secret=$key

但它要求用户将其密码作为参数传递,这是一个安全问题。

有没有办法通过输入密码(我很确定这是不可能的)或要求他登录来对使用 Azure AD 的用户进行身份验证?

不建议使用您的用户名和密码登录Azure 帐户。您最好创建服务主体以登录您的 Azure 帐户。请参考这个link:Use portal to create an Azure Active Directory application and service principal that can access resources.

此外,您可以使用 Azure CLI 2.0 来创建它。

az ad sp create-for-rbac --name {appId} --password "{strong password}" 

示例:

az ad sp create-for-rbac --name shuiexample --password "Password012!!"

您可能会得到如下结果:

{
  "appId": "bca24913-026d-4020-b9f1-add600bf9045",
  "displayName": "shuiexample1234",
  "name": "http://shuiexample1234",
  "password": "*******",
  "tenant": "*******"
}

使用服务主体登录。

APPID="bca24913-026d-4020-b9f1-add600bf9045"
PASSWORD="******"
TENANTID="*******"

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d 'client_id=$APPID&scope=https%3A%2F%2Fgraph.microsoft.com%2F.default&client_secret=$PASSWORD&grant_type=client_credentials' 'https://login.microsoftonline.com/$TENANTID/oauth2/token'