对 Azure 应用服务的部署进行身份验证
- 10 分钟
若要完成本模块中的练习,你已经被登录到沙盒环境中。 由于此环境是交互式的,因此所有部署都已使用初始化沙盒时使用的凭据进行身份验证。 如果您将生成过程自动化,部署将不会使用此交互式环境。 在自动化方案中,需要将项目配置为使用受支持的身份验证方法之一。
在本单元中,你将了解如何将 Maven 配置为使用 Azure 身份验证。
对 Web 应用进行身份验证
借助 Azure,可以灵活地决定如何对应用进行身份验证。 你选择的选项取决于你公司构建环境。 此处列出了使用 Maven 对应用程序代码进行身份验证的三个选项,按复杂性(从最低到最高)排序:
在 Azure 门户中使用 Azure CLI 进行身份验证或使用 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 应用服务的 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
找到
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 应用服务:
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
您的 Web 应用的 URL 是
https://mywebapp-1234567890.azurewebsites.net/
。此命令返回一个响应,其中包含类似于以下示例的 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>
地点:
参数 DESCRIPTION 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 应用服务:
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。