教學:使用管理身份從 Java Tomcat App Service 連接 PostgreSQL 資料庫,無需秘密

Azure App Service 提供在 Azure 上高度可擴展且自我修復的網頁託管服務。 它也為您的應用程式提供 受控身分,這是一個開箱即用的解決方案,用於保護對 Azure Database for PostgreSQL 及其他 Azure 服務的存取。 App Service 中的受控識別可藉由從應用程式刪除祕密 (例如環境變數中的認證),讓應用程式更加安全。 在本教學課程中,您會了解如何:

  • 建立 PostgreSQL 資料庫。
  • 將範例應用程式部署到 Azure 應用服務,使用 WAR 封裝在 Tomcat 上。
  • 設定一個 Tomcat 網頁應用程式,讓它使用 Microsoft Entra 認證搭配 PostgreSQL 資料庫。
  • 使用服務連接器,透過受控識別連線至 PostgreSQL 資料庫。

如果你沒有Azure帳號,請在開始前先建立一個free帳號

必要條件

複製應用程式範例,並準備存放庫

在您的終端機中執行下列命令,即可複製範例存放庫並設定範例應用程式環境。

git clone https://github.com/Azure-Samples/Passwordless-Connections-for-Java-Apps
cd Passwordless-Connections-for-Java-Apps/Tomcat/

建立適用於 PostgreSQL 的 Azure 資料庫

請依照以下步驟在您的訂閱中建立 Azure Postgres 資料庫。 Tomcat 應用程式連接這個資料庫並在執行時儲存資料,無論你在哪裡執行應用程式,都能維持應用程式狀態。

  1. 登入 Azure CLI,如果你的登入憑證連結了多個帳號,可以選擇性地設定訂閱。

    az login
    az account set --subscription <subscription-ID>
    
  2. 建立一個 Azure 資源群組,並標示資源群組名稱。

    export RESOURCE_GROUP=<resource-group-name>
    export LOCATION=eastus
    
    az group create --name $RESOURCE_GROUP --location $LOCATION
    
  3. 創建一個適用於 PostgreSQL 伺服器的 Azure 資料庫。 伺服器是用管理員帳號建立的,但不會被使用,因為我們會用 Microsoft Entra 管理員帳號來執行管理任務。

    export POSTGRESQL_ADMIN_USER=azureuser
    # PostgreSQL admin access rights won't be used because Azure AD authentication is leveraged to administer the database.
    export POSTGRESQL_ADMIN_PASSWORD=<admin-password>
    export POSTGRESQL_HOST=<postgresql-host-name>
    
    # Create a PostgreSQL server.
    az postgres flexible-server create \
        --resource-group $RESOURCE_GROUP \
        --name $POSTGRESQL_HOST \
        --location $LOCATION \
        --admin-user $POSTGRESQL_ADMIN_USER \
        --admin-password $POSTGRESQL_ADMIN_PASSWORD \
        --public-access 0.0.0.0 \
        --sku-name Standard_D2s_v3
    
  4. 為應用程式建立資料庫。

    export DATABASE_NAME=checklist
    
    az postgres flexible-server db create \
        --resource-group $RESOURCE_GROUP \
        --server-name $POSTGRESQL_HOST \
        --database-name $DATABASE_NAME
    

將應用程式部署至 App Service

請依照以下步驟建立 WAR 檔案,並使用 WAR 封裝部署到 Tomcat 上的 Azure App Service。

  1. 範例應用程式包含可產生 WAR 檔案的 pom.xml 檔案。 要建立應用程式,請執行以下指令。

    mvn clean package -f pom.xml
    
  2. 在 Linux 上用 Tomcat 9.0 建立 Azure App Service 資源。

    export APPSERVICE_PLAN=<app-service-plan>
    export APPSERVICE_NAME=<app-service-name>
    # Create an App Service plan
    az appservice plan create \
        --resource-group $RESOURCE_GROUP \
        --name $APPSERVICE_PLAN \
        --location $LOCATION \
        --sku B1 \
        --is-linux
    
    # Create an App Service resource.
    az webapp create \
        --resource-group $RESOURCE_GROUP \
        --name $APPSERVICE_NAME \
        --plan $APPSERVICE_PLAN \
        --runtime "TOMCAT:10.0-java11"
    
  3. 將 WAR 套件部署至 App Service。

    az webapp deploy \
        --resource-group $RESOURCE_GROUP \
        --name $APPSERVICE_NAME \
        --src-path target/app.war \
        --type war
    

使用身分識別連線來連線 Postgres 資料庫

接下來,使用服務連接器連接資料庫。

安裝 Azure CLI 的 Service Connector 無密碼擴充功能:

az extension add --name serviceconnector-passwordless --upgrade

然後,使用服務連接器,將您的應用程式連線到具有系統指派受控識別的 Postgres 資料庫。

若要建立此連線,請執行 az webapp connection create 命令。

az webapp connection create postgres-flexible \
    --resource-group $RESOURCE_GROUP \
    --name $APPSERVICE_NAME \
    --target-resource-group $RESOURCE_GROUP \
    --server $POSTGRESQL_HOST \
    --database $DATABASE_NAME \
    --system-identity \
    --client-type java

此命令會建立 Web 應用程式和 PostgreSQL 伺服器之間的連線,並透過系統指派的受控識別來管理驗證。

接著,更新應用程式設定,並在 connection string 中新增插件

export AZURE_POSTGRESQL_CONNECTIONSTRING=$(\
    az webapp config appsettings list \
        --resource-group $RESOURCE_GROUP \
        --name $APPSERVICE_NAME \
    | jq -c -r '.[] \
    | select ( .name == "AZURE_POSTGRESQL_CONNECTIONSTRING" ) \
    | .value')

az webapp config appsettings set \
    --resource-group $RESOURCE_GROUP \
    --name $APPSERVICE_NAME \
    --settings 'CATALINA_OPTS=-DdbUrl="'"${AZURE_POSTGRESQL_CONNECTIONSTRING}"'&authenticationPluginClassName=com.azure.identity.extensions.jdbc.postgresql.AzurePostgresqlAuthenticationPlugin"'

測試範例 Web 應用程式

請執行以下指令來測試應用程式。

export WEBAPP_URL=$(az webapp show \
    --resource-group $RESOURCE_GROUP \
    --name $APPSERVICE_NAME \
    --query defaultHostName \
    --output tsv)

# Create a list
curl -X POST -H "Content-Type: application/json" -d '{"name": "list1","date": "2022-03-21T00:00:00","description": "Sample checklist"}' https://${WEBAPP_URL}/checklist

# Create few items on the list 1
curl -X POST -H "Content-Type: application/json" -d '{"description": "item 1"}' https://${WEBAPP_URL}/checklist/1/item
curl -X POST -H "Content-Type: application/json" -d '{"description": "item 2"}' https://${WEBAPP_URL}/checklist/1/item
curl -X POST -H "Content-Type: application/json" -d '{"description": "item 3"}' https://${WEBAPP_URL}/checklist/1/item

# Get all lists
curl https://${WEBAPP_URL}/checklist

# Get list 1
curl https://${WEBAPP_URL}/checklist/1

清除資源

在前面的步驟中,你在資源群組中建立了 Azure 資源。 如果你未來不預期會需要這些資源,請在 Cloud Shell 執行以下指令刪除資源群組:

az group delete --name myResourceGroup

執行此命令約需一分鐘。

下一步

想了解更多關於在 App Service on Linux 上執行 Java 應用程式的資訊,請參閱開發者指南。

了解如何使用自訂網域和憑證保障您的應用程式。