教學課程:從 JAVA JBoss EAP App Service 使用無密碼連線連線至 My SQL 資料庫
Azure App 服務 在 Azure 中提供可高度調整、自我修補的 Web 主機服務。 它也提供 應用程式的受控識別 ,這是保護適用於 MySQL 的 Azure 資料庫 和其他 Azure 服務存取 權的周全解決方案。 App Service 中的受控識別可藉由排除來自您應用程式的秘密,例如環境變數中的認證,讓您的應用程式更安全。 在本教學課程中,您會了解如何:
- 建立 MySQL 資料庫。
- 部署範例 JBoss EAP 應用程式,以使用 WAR 套件Azure App 服務。
- 設定 Spring Boot Web 應用程式,以搭配 My SQL 資料庫 使用 Microsoft Entra 驗證。
- 使用服務連線or 使用受控識別連線至 My SQL 資料庫。
如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶。
必要條件
- Git
- Java JDK
- Maven
- Azure CLI 2.46.0 版或更高版本。
- Azure CLI serviceconnector-passwordless extension version 0.2.2 或更高版本。
- jq
複製範例應用程式並準備存放庫
在您的終端機中執行下列命令,以複製範例存放庫並設定範例應用程式環境。
git clone https://github.com/Azure-Samples/Passwordless-Connections-for-Java-Apps
cd Passwordless-Connections-for-Java-Apps/JakartaEE/jboss-eap/
建立適用於 MySQL 的 Azure 資料庫
請遵循下列步驟,在您的訂用帳戶中建立適用於 MySQL 的 Azure 資料庫。 Spring Boot 應用程式會連線到此資料庫,並在執行時儲存其資料,無論您在何處執行應用程式,都保存應用程式狀態。
登入 Azure CLI,並選擇性地設定您的訂用帳戶,如果您有多個連線到您的登入認證。
az login az account set --subscription <subscription-ID>
建立 Azure 資源群組,並記下資源組名。
export RESOURCE_GROUP=<resource-group-name> export LOCATION=eastus az group create --name $RESOURCE_GROUP --location $LOCATION
建立適用於 MySQL 伺服器的 Azure 資料庫。 伺服器是以系統管理員帳戶建立,但不會使用它,因為我們將使用 Microsoft Entra 系統管理員帳戶來執行系統管理工作。
export MYSQL_ADMIN_USER=azureuser # MySQL admin access rights won't be used because Azure AD authentication is leveraged to administer the database. export MYSQL_ADMIN_PASSWORD=<admin-password> export MYSQL_HOST=<mysql-host-name> # Create a MySQL server. az mysql flexible-server create \ --name $MYSQL_HOST \ --resource-group $RESOURCE_GROUP \ --location $LOCATION \ --admin-user $MYSQL_ADMIN_USER \ --admin-password $MYSQL_ADMIN_PASSWORD \ --public-access 0.0.0.0 \ --tier Burstable \ --sku-name Standard_B1ms \ --storage-size 32
建立應用程式的資料庫。
export DATABASE_NAME=checklist az mysql flexible-server db create \ --resource-group $RESOURCE_GROUP \ --server-name $MYSQL_HOST \ --database-name $DATABASE_NAME
建立 App Service
在 Linux 上建立Azure App 服務資源。 JBoss EAP 需要進階版 SKU。
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 P1V3 \
--is-linux
# Create an App Service resource.
az webapp create \
--resource-group $RESOURCE_GROUP \
--name $APPSERVICE_NAME \
--plan $APPSERVICE_PLAN \
--runtime "JBOSSEAP:7-java8"
連線具有身分識別連線功能的 MySQL 資料庫
接下來,使用 Service 連線or 連接資料庫。
安裝 Azure CLI 的服務連線或無密碼擴充功能:
az extension add --name serviceconnector-passwordless --upgrade
然後,使用下列命令建立使用者指派的受控識別以進行 Microsoft Entra 驗證。 如需詳細資訊,請參閱 設定 適用於 MySQL 的 Azure 資料庫 - 彈性伺服器的 Microsoft Entra 驗證。
export USER_IDENTITY_NAME=<your-user-assigned-managed-identity-name>
export IDENTITY_RESOURCE_ID=$(az identity create \
--name $USER_IDENTITY_NAME \
--resource-group $RESOURCE_GROUP \
--query id \
--output tsv)
重要
建立使用者指派的身分識別之後,請要求全域 管理員istrator 或 Privileged Role 管理員istrator 授與此身分識別的下列許可權: User.Read.All
、 GroupMember.Read.All
和 Application.Read.ALL
。 如需詳細資訊,請參閱 Active Directory 驗證 的許可權 一節。
然後,使用服務連線or,將您的應用程式連線到具有系統指派受控識別的 MySQL 資料庫。 若要建立此連線,請 執行 az webapp connection create 命令。
az webapp connection create mysql-flexible \
--resource-group $RESOURCE_GROUP \
--name $APPSERVICE_NAME \
--target-resource-group $RESOURCE_GROUP \
--server $MYSQL_HOST \
--database $DATABASE_NAME \
--system-identity mysql-identity-id=$IDENTITY_RESOURCE_ID \
--client-type java
此服務連線or 命令會在背景中執行下列工作:
為裝載Azure App 服務的應用程式
$APPSERVICE_NAME
啟用系統指派的受控識別。將 Microsoft Entra 系統管理員設定為目前的登入使用者。
在步驟 1 中為系統指派的受控識別新增資料庫使用者,並將資料庫
$DATABASE_NAME
的擁有權限授與此使用者。 您可以在上一個命令的輸出中,從連接字串取得使用者名稱。在名為
AZURE_MYSQL_CONNECTIONSTRING
的應用程式中,將連接字串新增至應用程式設定。注意
如果您看到錯誤訊息
The subscription is not registered to use Microsoft.ServiceLinker
,請執行 命令az provider register --namespace Microsoft.ServiceLinker
來註冊服務連線或資源提供者,然後再次執行連線命令。
部署應用程式
請遵循下列步驟來準備資料庫中的資料,並部署應用程式。
建立資料庫架構
開啟防火牆以允許來自您目前 IP 位址的連線。
# Create a temporary firewall rule to allow connections from your current machine to the MySQL server export MY_IP=$(curl http://whatismyip.akamai.com) az mysql flexible-server firewall-rule create \ --resource-group $RESOURCE_GROUP \ --name $MYSQL_HOST \ --rule-name AllowCurrentMachineToConnect \ --start-ip-address ${MY_IP} \ --end-ip-address ${MY_IP}
連線至資料庫並建立資料表。
export DATABASE_FQDN=${MYSQL_HOST}.mysql.database.azure.com export CURRENT_USER=$(az account show --query user.name --output tsv) export RDBMS_ACCESS_TOKEN=$(az account get-access-token \ --resource-type oss-rdbms \ --output tsv \ --query accessToken) mysql -h "${DATABASE_FQDN}" --user "${CURRENT_USER}" --enable-cleartext-plugin --password="$RDBMS_ACCESS_TOKEN" < azure/init-db.sql
移除暫時防火牆規則。
az mysql flexible-server firewall-rule delete \ --resource-group $RESOURCE_GROUP \ --name $MYSQL_HOST \ --rule-name AllowCurrentMachineToConnect
部署應用程式
更新 App 設定中的連接字串。
取得 Service 連線or 所產生的連接字串,並新增無密碼驗證外掛程式。 啟動腳本中會參考此連接字串。
export PASSWORDLESS_URL=$(\ az webapp config appsettings list \ --resource-group $RESOURCE_GROUP \ --name $APPSERVICE_NAME \ | jq -c '.[] \ | select ( .name == "AZURE_MYSQL_CONNECTIONSTRING" ) \ | .value' \ | sed 's/"//g') # Create a new environment variable with the connection string including the passwordless authentication plugin export PASSWORDLESS_URL=${PASSWORDLESS_URL}'&defaultAuthenticationPlugin=com.azure.identity.extensions.jdbc.mysql.AzureMysqlAuthenticationPlugin&authenticationPlugins=com.azure.identity.extensions.jdbc.mysql.AzureMysqlAuthenticationPlugin' az webapp config appsettings set \ --resource-group $RESOURCE_GROUP \ --name $APPSERVICE_NAME \ --settings "AZURE_MYSQL_CONNECTIONSTRING_PASSWORDLESS=${PASSWORDLESS_URL}"
範例應用程式包含 可產生 WAR 檔案的 pom.xml 檔案。 執行下列命令以建置應用程式。
mvn clean package -DskipTests
將 WAR 和啟動腳本部署至 App Service。
az webapp deploy \ --resource-group $RESOURCE_GROUP \ --name $APPSERVICE_NAME \ --src-path target/ROOT.war \ --type war az webapp deploy \ --resource-group $RESOURCE_GROUP \ --name $APPSERVICE_NAME \ --src-path src/main/webapp/WEB-INF/createMySQLDataSource.sh \ --type startup
測試範例 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
此命令可能需要一分鐘的時間才能執行。
下一步
在開發人員指南中深入瞭解如何在 Linux 上的 App Service上執行 JAVA 應用程式。