驗證 Azure App Service 的部署
為了完成本課程模組中的練習,您已登入沙箱環境。 因為此環境是互動式環境,所以所有部署都已使用初始化沙箱時所使用的認證來進行驗證。 不過,如果要將建置程序自動化,則部署不會使用此互動式環境。 在自動化案例中,您必須將專案設定為使用其中一種支援的驗證方法。
在本單元中,您將了解貴公司如何將 Maven 設定為使用 Azure 驗證。
驗證 Web 應用程式
Azure 可供彈性地決定要如何驗證應用程式。 您所選選項取決於貴公司的組建環境。 以下是使用 Maven 驗證應用程式程式碼的三個選項,以複雜度的順序列出 (從最低到最高):
使用 Azure CLI 進行驗證,或使用 Azure 入口網站上的 Cloud Shell。
建立 Azure 服務主體、使用服務主體認證建立 JSON 檔案,然後修改專案的
pom.xml
檔案。建立 Azure 服務主體,將服務主體認證新增至 Maven
settings.xml
檔案,然後修改專案的pom.xml
檔案以使用 Maven 設定。
Microsoft 建議使用第三個選項,因為該選項提供最可靠、彈性且一致的驗證方法。 在實際設定中,貴公司現有 Java Web 應用程式可能會在未安裝 Azure CLI 工具的本機伺服器上執行。 考慮到這一點,您可能會實作建議,以便使用服務主體和 Maven settings.xml
檔案來新增驗證。 不過,在本練習中,沙箱沒有足夠的權限來建立服務主體。
使用 Azure CLI 進行驗證
驗證 Maven 最簡單的方式,就是使用 Azure CLI 登入。 適用於 Azure App Service 的 Maven 外掛程式可以接著使用認證來部署應用程式,而不需要進行額外的設定。
如果您使用的是 Azure Cloud Shell,就如同在本課程模組中使用 Microsoft Learn 沙箱完成練習時一樣,依預設您會登入 Azure,而不需要執行任何其他命令。 不過,如果您是從另一部電腦使用 Azure CLI,則必須使用 az login
命令登入。
使用服務主體進行驗證
驗證 Web 應用程式的第二種方法牽涉到建立 Azure 服務主體,並將服務主體認證儲存到將從專案設定參考的檔案。
若要使用 Azure CLI 建立 Azure 服務主體,請使用下列步驟。
從 Azure CLI 執行下列命令,以建立 Azure 服務主體:
az ad sp create-for-rbac --name https://mywebapp-1234567890.azurewebsites.net/ --role Contributor --scopes /subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss
其中,
https://mywebapp-1234567890.azurewebsites.net/
是 Web 應用程式的 URL。此命令會傳回具有 JSON 物件的回應,類似於下列範例:
Creating 'Contributor' role assignment under scope '/subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss' The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control. For more information, see https://aka.ms/azadsp-cli { "appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", "displayName": "mywebapp-1234567890.azurewebsites.net/", "name": "https://mywebapp-1234567890.azurewebsites.net/", "password": "...", "tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt" }
修改 Web 應用程式的
pom.xml
檔案,以使用 JSON 輸出中的資訊。使用程式碼編輯器開啟
pom.xml
檔案:cd ~/MyWebApp code pom.xml
尋找
azure-webapp-maven-plugin
的<configuration>
區段。在包含
<region>
元素的行後面新增下列 XML,並使用 JSON 輸出中的資訊:<auth> <type>service_principal</type> <client>value-of-appId</client> <tenant>value-of-tenant</tenant> <key>value-of-password</key> <environment>azure</environment> </auth>
azure-webapp-maven-plugin
區段現在應該類似於下列範例:<plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-webapp-maven-plugin</artifactId> <version>2.13.0</version> <configuration> <schemaVersion>v2</schemaVersion> <resourceGroup>MyWebApp-1714654093047-rg</resourceGroup> <appName>MyWebApp-1714654093047</appName> <pricingTier>S1</pricingTier> <region>centralus</region> <auth> <type>service_principal</type> <client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client> <tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant> <key>abcdefghijklmnopqrstuvwxyz1234567890</key> <environment>azure</environment> </auth> <runtime> <os>Linux</os> <javaVersion>Java 17</javaVersion> <webContainer>Tomcat 10.0</webContainer> </runtime> <deployment> <resources> <resource> <directory>${project.basedir}/target</directory> <includes> <include>*.war</include> </includes> </resource> </resources> </deployment> </configuration> </plugin>
鍵入 Ctrl+S 來儲存變更。
輸入 Ctrl+Q 來結束程式碼編輯器。
使用 Maven 建置 Web 應用程式,並將其部署至 Azure App Service:
mvn azure-webapp:deploy
Maven 會顯示一系列的建置訊息,且最終訊息應會指出成功部署至 Azure:
[INFO] Scanning for projects... [INFO] [INFO] -------------------< com.microsoft.example:MyWebApp >------------------- [INFO] Building MyWebApp Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- azure-webapp-maven-plugin:2.13.0:deploy (default-cli) @ MyWebApp --- [INFO] Auth type: SERVICE_PRINCIPAL [INFO] Username: 74d82376-184f-400e-a08e-27cd522d7559 [INFO] There is only one subscription '...' in your account, will use it automatically. [INFO] Subscription: ... [INFO] Failed to get version of your artifact, skip artifact compatibility test [INFO] Trying to deploy external resources to MyWebApp-1714654093047... [INFO] Successfully deployed the resources to MyWebApp-1714654093047 [INFO] Trying to deploy artifact to MyWebApp-1714654093047... [INFO] Deploying (/home/cephas/MyWebApp/target/MyWebApp.war)[war] ... [INFO] Application url: https://mywebapp-1714654093047.azurewebsites.net [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 47.052 s [INFO] Finished at: 2024-05-02T13:10:54Z [INFO] ------------------------------------------------------------------------
回應中的
Auth type: SERVICE_PRINCIPAL
行表示服務主體是用來將 Web 應用程式發佈至 Azure。
使用 Maven settings.xml
檔案進行驗證
驗證 Web 應用程式的第三種方法包括建立 Azure 服務主體、建立包含服務主體認證的 Maven settings.xml
檔案,以及修改專案的 pom.xml
檔案以使用 Maven 設定。
使用 Azure CLI 建立 Azure 服務主體的步驟,與本單元上一節中的步驟相同。
從 Azure CLI 執行下列命令,以建立 Azure 服務主體:
az ad sp create-for-rbac --name https://mywebapp-1234567890.azurewebsites.net/ --role Contributor --scopes /subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss
其中,
https://mywebapp-1234567890.azurewebsites.net/
是 Web 應用程式的 URL。此命令會傳回具有 JSON 物件的回應,類似於下列範例:
Creating 'Contributor' role assignment under scope '/subscriptions/ssssssss-ssss-ssss-ssss-ssssssssssss' The output includes credentials that you must protect. Be sure that you do not include these credentials in your code or check the credentials into your source control. For more information, see https://aka.ms/azadsp-cli { "appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", "displayName": "mywebapp-1234567890.azurewebsites.net/", "name": "https://mywebapp-1234567890.azurewebsites.net/", "password": "...", "tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt" }
建立
settings.xml
檔案的使用者版本,以供 Maven 使用。使用程式碼編輯器,為 Maven 設定建立新的 XML 檔案:
code ~/.m2/settings.xml
將下列 XML 貼入該檔案:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"> <servers> <server> <id>azure-auth</id> <configuration> <client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client> <tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant> <key>pppppppp-pppp-pppp-pppp-pppppppppppp</key> </configuration> </server> </servers> </settings>
其中:
參數 描述 client
指定服務主體的 appId
值key
指定服務主體的 password
值tenant
指定服務主體的 tenant
值鍵入 Ctrl+S 來儲存變更。
輸入 Ctrl+Q 來結束程式碼編輯器。
修改 Web 應用程式的
pom.xml
檔案,以參考驗證檔案。使用程式碼編輯器開啟
pom.xml
檔案:cd ~/MyWebApp code pom.xml
尋找
azure-webapp-maven-plugin
的<configuration>
區段。在包含
<region>
項目的那一行後面新增下列 XML:<auth> <type>service_principal</type> <serverId>azure-auth</serverId> </auth>
azure-webapp-maven-plugin
區段現在應該類似於下列範例:<plugin> <groupId>com.microsoft.azure</groupId> <artifactId>azure-webapp-maven-plugin</artifactId> <version>2.13.0</version> <configuration> <schemaVersion>V2</schemaVersion> <resourceGroup>maven-publish</resourceGroup> <appName>MyWebApp-1234567890</appName> <pricingTier>S1</pricingTier> <region>centralus</region> <auth> <type>service_principal</type> <serverId>azure-auth</serverId> </auth> <runtime> <os>Linux</os> <javaVersion>Java 17</javaVersion> <webContainer>Tomcat 10.0</webContainer> </runtime> <deployment> <resources> <resource> <directory>${project.basedir}/target</directory> <includes> <include>*.war</include> </includes> </resource> </resources> </deployment> </configuration> </plugin>
鍵入 Ctrl+S 來儲存變更。
輸入 Ctrl+Q 來結束程式碼編輯器。
使用 Maven 建置 Web 應用程式,並將其部署至 Azure App Service:
mvn azure-webapp:deploy
Maven 會顯示一系列的建置訊息,且最終訊息應會指出成功部署至 Azure:
[INFO] -------------------< com.microsoft.example:MyWebApp >------------------- [INFO] Building MyWebApp Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- azure-webapp-maven-plugin:2.13.0:deploy (default-cli) @ MyWebApp --- [INFO] Auth type: SERVICE_PRINCIPAL [INFO] Username: aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa [INFO] There is only one subscription '...' in your account, will use it automatically. [INFO] Subscription: ... [INFO] Failed to get version of your artifact, skip artifact compatibility test [INFO] Trying to deploy external resources to MyWebApp-1714654093047... [INFO] Successfully deployed the resources to MyWebApp-1714654093047 [INFO] Trying to deploy artifact to MyWebApp-1714654093047... [INFO] Deploying (/home/cephas/MyWebApp/target/MyWebApp.war)[war] ... [INFO] Application url: https://mywebapp-1714654093047.azurewebsites.net [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 53.611 s [INFO] Finished at: 2024-05-02T13:53:31Z [INFO] ------------------------------------------------------------------------
回應中的
Auth type: SERVICE_PRINCIPAL
行指出服務主體認證是用來將 Web 應用程式發佈到 Azure。