對 Azure App Service 的部署進行身份驗證
- 10 分鐘
若要完成本課程模組中的練習,您已登入沙盒環境。 由於此環境是互動式的,因此所有部署都已使用您初始化沙盒時所使用的認證進行驗證。 不過,如果您要將建置程式自動化,您的部署將不會使用此互動式環境。 在自動化案例中,您必須將項目設定為使用其中一個支持的驗證方法。
在本單元中,您將瞭解如何將 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 Sandbox 完成練習時,預設會登入 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找出
<configuration>的azure-webapp-maven-plugin區段。在包含
<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:deployMaven 會顯示一系列組建訊息,最後一則訊息應指出已成功部署至 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" }為 Maven 建立
settings.xml檔案的使用者版本。使用程式代碼編輯器為您的 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:deployMaven 會顯示一系列組建訊息,最後一則訊息應指出已成功部署至 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。