Share via


快速入門:使用 Azure Spring Apps 企業版方案設定應用程式的單一登錄

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但在我們努力更新資產,例如螢幕快照、影片和圖表時,您會在某些地方看到舊名稱一段時間。

本文適用於:❌ 基本/標準✔️企業

本快速入門說明如何為在 Azure Spring Apps 企業版方案中執行的應用程式設定單一登錄。

必要條件

  • 具有有效訂用帳戶的 Azure 帳戶。 免費建立帳戶
  • Azure Spring Apps 企業版方案的授權。 如需詳細資訊,請參閱 Azure Marketplace 中的企業方案。
  • Azure CLI 2.45.0 版或更高版本。
  • Git
  • jq
  • Azure Spring Apps 企業版方案延伸模組。 使用下列命令來移除舊版並安裝最新的 Enterprise 方案擴充功能。 如果您先前已安裝擴充 spring-cloud 功能,請將其卸載,以避免設定和版本不符。
    az extension add --upgrade --name spring
    az extension remove --name spring-cloud
    
  • 使用企業方案,完成建置和部署應用程式至 Azure Spring Apps 中的步驟。

準備單一登錄認證

若要設定應用程式的單一登錄,您需要準備認證。 下列各節說明使用現有提供者或以 Microsoft Entra ID 布建應用程式註冊的步驟。

使用現有的提供者

請遵循下列步驟,使用現有的識別提供者來設定單一登錄。 如果您要布建 Microsoft Entra 應用程式註冊,請直接跳到下一節: 使用 Microsoft Entra ID 建立和設定應用程式註冊。

  1. 設定現有的識別提供者,以允許重新導向回適用於 VMware Tanzu 的 Spring Cloud Gateway 和 VMware Tanzu 的 API 入口網站。 Spring Cloud 閘道具有單一 URI,可允許重新進入閘道。 API 入口網站有兩個 URI,可支援使用者介面和基礎 API。 下列命令會擷取您新增至單一登錄提供者設定的這些 URI。

    export GATEWAY_URL=$(az spring gateway show \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-service-instance-name> | jq -r '.properties.url')
    
    export PORTAL_URL=$(az spring api-portal show \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-service-instance-name> | jq -r '.properties.url')
    
    echo "https://${GATEWAY_URL}/login/oauth2/code/sso"
    echo "https://${PORTAL_URL}/oauth2-redirect.html"
    echo "https://${PORTAL_URL}/login/oauth2/code/sso"
    
  2. 取得識別提供者的 Client IDClient Secret

  3. Issuer URI取得識別提供者的 。 您必須使用簽發者 URI 來設定提供者,這是其判斷提示為其簽發者識別碼的 URI。 例如,如果 issuer-uri 提供的 是 https://example.com,則會對 https://example.com/.well-known/openid-configuration提出OpenID提供者組態要求。 結果必須是OpenID提供者組態回應。

    注意

    您只能使用支援 OpenID 連線 Discovery 通訊協定的授權伺服器。

  4. JWK URI取得識別提供者的 ,以供稍後使用。 JWK URI通常會採用 或${ISSUER_URI}/<version>/keys格式${ISSUER_URI}/keys。 身分識別服務應用程式會使用公用 JSON Web 金鑰 (JWK) 來驗證單一登錄識別提供者授權伺服器所發出的 JSON Web 令牌 (JWT)。

使用 Microsoft Entra ID 建立及設定應用程式註冊

若要使用 Microsoft Entra ID 註冊應用程式,請遵循下列步驟。 如果您使用現有的提供者認證,請直接跳到下一節部署 Identity Service 應用程式

  1. 使用下列命令以 Microsoft Entra ID 建立應用程式註冊,並儲存輸出:

    az ad app create --display-name <app-registration-name> > ad.json
    
  2. 使用下列命令來擷取應用程式識別碼並收集客戶端密碼:

    export APPLICATION_ID=$(cat ad.json | jq -r '.appId')
    az ad app credential reset --id ${APPLICATION_ID} --append > sso.json
    
  3. 使用下列命令將服務主體指派給應用程式註冊:

    az ad sp create --id ${APPLICATION_ID}
    
  4. 使用下列命令來擷取 Spring Cloud Gateway 和 API 入口網站的 URL,並將必要的回復 URL 新增至 Active Directory 應用程式註冊。

    export APPLICATION_ID=$(cat ad.json | jq -r '.appId')
    
    export GATEWAY_URL=$(az spring gateway show \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-service-instance-name> | jq -r '.properties.url')
    
    export PORTAL_URL=$(az spring api-portal show \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-service-instance-name> | jq -r '.properties.url')
    
    az ad app update \
        --id ${APPLICATION_ID} \
        --web-redirect-uris "https://${GATEWAY_URL}/login/oauth2/code/sso" "https://${PORTAL_URL}/oauth2-redirect.html" "https://${PORTAL_URL}/login/oauth2/code/sso"
    
  5. 使用下列命令來擷取應用程式的 Client ID。 儲存輸出以供稍後在本快速入門中使用。

    cat sso.json | jq -r '.appId'
    
  6. 使用下列命令來擷取應用程式的 Client Secret。 儲存輸出以供稍後在本快速入門中使用。

    cat sso.json | jq -r '.password'
    
  7. 使用下列命令來擷 Issuer URI取 。 儲存輸出以供稍後在本快速入門中使用。

    export TENANT_ID=$(cat sso.json | jq -r '.tenant')
    echo "https://login.microsoftonline.com/${TENANT_ID}/v2.0"
    
  8. JWK URI從下列指令的輸出擷取 。 Identity Service 應用程式會使用公用 JSON Web 金鑰 (JWK) 來驗證 Active Directory 所發出的 JSON Web 令牌 (JWT)。

    export TENANT_ID=$(cat sso.json | jq -r '.tenant')
    echo "https://login.microsoftonline.com/${TENANT_ID}/discovery/v2.0/keys"
    

部署 Identity Service 應用程式

若要完成單一登錄體驗,請使用下列步驟來部署 Identity Service 應用程式。 Identity Service 應用程式提供單一路由來協助識別使用者。

  1. 瀏覽至專案資料夾。

  2. 使用下列命令來建立 identity-service 應用程式:

    az spring app create \
        --resource-group <resource-group-name> \
        --name identity-service \
        --service <Azure-Spring-Apps-service-instance-name>
    
  3. 使用下列命令,藉由系結至應用程式組態服務來啟用身分識別服務的外部化組態:

    az spring application-configuration-service bind \
        --resource-group <resource-group-name> \
        --app identity-service \
        --service <Azure-Spring-Apps-service-instance-name>
    
  4. 使用下列命令,藉由系結至服務登錄來啟用身分識別服務的服務探索和註冊:

    az spring service-registry bind \
        --resource-group <resource-group-name> \
        --app identity-service \
        --service <Azure-Spring-Apps-service-instance-name>
    
  5. 使用下列命令來部署身分識別服務:

    az spring app deploy \
        --resource-group <resource-group-name> \
        --name identity-service \
        --service <Azure-Spring-Apps-service-instance-name> \
        --config-file-pattern identity/default \
        --source-path apps/acme-identity \
        --build-env BP_JVM_VERSION=17 \
        --env "JWK_URI=<jwk-uri>"
    
  6. 使用下列命令將要求路由傳送至身分識別服務:

    az spring gateway route-config create \
        --resource-group <resource-group-name> \
        --name identity-routes \
        --service <Azure-Spring-Apps-service-instance-name> \
        --app-name identity-service \
        --routes-file azure-spring-apps-enterprise/resources/json/routes/identity-service.json
    

設定 Spring Cloud Gateway 的單一登錄

您可以設定 Spring Cloud Gateway 以使用單一登錄來驗證要求。 若要將 Spring Cloud Gateway 設定為使用單一登錄,請遵循下列步驟:

  1. 使用下列命令來設定 Spring Cloud Gateway 以使用單一登入:

    export GATEWAY_URL=$(az spring gateway show \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-service-instance-name> | jq -r '.properties.url')
    
    az spring gateway update \
        --resource-group <resource-group-name> \
        --service <Azure-Spring-Apps-service-instance-name> \
        --api-description "Fitness Store API" \
        --api-title "Fitness Store" \
        --api-version "v1.0" \
        --server-url "https://${GATEWAY_URL}" \
        --allowed-origins "*" \
        --client-id <client-id> \
        --client-secret <client-secret> \
        --scope "openid,profile" \
        --issuer-uri <issuer-uri>
    
  2. 指示購物車服務應用程式使用 Spring Cloud Gateway 進行驗證。 使用下列命令來提供必要的環境變數:

    az spring app update \
        --resource-group <resource-group-name> \
        --name cart-service \
        --service <Azure-Spring-Apps-service-instance-name> \
        --env "AUTH_URL=https://${GATEWAY_URL}" "CART_PORT=8080"
    
  3. 指示訂單服務應用程式使用 Spring Cloud Gateway 進行驗證。 使用下列命令來提供必要的環境變數:

    az spring app update \
        --resource-group <resource-group-name> \
        --name order-service \
        --service <Azure-Spring-Apps-service-instance-name> \
        --env "AcmeServiceSettings__AuthUrl=https://${GATEWAY_URL}"
    
  4. 使用下列命令來擷取 Spring Cloud Gateway 的 URL:

    echo "https://${GATEWAY_URL}"
    

    您可以在瀏覽器中開啟輸出 URL,以探索更新的應用程式。 [登入] 函式現在可運作,可讓您將專案新增至購物車並下單。 登入之後,客戶資訊按鈕會顯示登入的用戶名稱。

設定 API 入口網站的單一登錄

您可以設定 VMware Tanzu 的 API 入口網站,以在探索 API 之前使用單一登錄來要求驗證。 使用下列命令來設定 API 入口網站的單一登入:

export PORTAL_URL=$(az spring api-portal show \
    --resource-group <resource-group-name> \
    --service <Azure-Spring-Apps-service-instance-name> | jq -r '.properties.url')

az spring api-portal update \
    --resource-group <resource-group-name> \
    --service <Azure-Spring-Apps-service-instance-name> \
    --client-id <client-id> \
    --client-secret <client-secret> \
    --scope "openid,profile,email" \
    --issuer-uri <issuer-uri>

使用下列命令來擷取 API 入口網站的網址:

export PORTAL_URL=$(az spring api-portal show \
    --resource-group <resource-group-name> \
    --service <Azure-Spring-Apps-service-instance-name> | jq -r '.properties.url')

echo "https://${PORTAL_URL}"

您可以在瀏覽器開啟輸出 URL,以探索應用程式 API。 系統會指示您在探索 API 之前登入。


清除資源

如果您打算繼續使用後續的快速入門和教學課程,您可能會想要保留這些資源。 若不再需要,請刪除資源群組,以刪除資源群組中的資源。 若要使用 Azure CLI 刪除資源群組,請使用下列命令:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
az group delete --name $resourceGroupName &&
echo "Press [ENTER] to continue ..."

下一步

繼續進行下列任一選擇性快速入門: