你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

通过 PowerShell 配置应用程序网关的相互身份验证

本文介绍如何使用 PowerShell 在应用程序网关上配置相互身份验证。 相互身份验证意味着应用程序网关使用上传到应用程序网关的客户端证书对发送请求的客户端进行身份验证。

如果没有 Azure 订阅,请在开始之前创建一个免费帐户

注释

建议使用 Azure Az PowerShell 模块与 Azure 交互。 若要开始,请参阅安装 Azure PowerShell。 若要了解如何迁移到 Az PowerShell 模块,请参阅 将 Azure PowerShell 从 AzureRM 迁移到 Az

本文需要 Azure PowerShell 模块 1.0.0 或更高版本。 运行 Get-Module -ListAvailable Az 即可查找版本。 如果需要升级,请参阅安装 Azure PowerShell 模块。 如果在本地运行 PowerShell,则还需运行 Connect-AzAccount 以创建与 Azure 的连接。

在您开始之前

若要配置应用程序网关的相互身份验证,需要客户端证书才能上传到网关。 客户端证书用于验证客户端向应用程序网关显示的证书。 对于测试,可以使用自签名的证书。 不过,不建议对生产工作负荷使用自签名证书,因为这些证书难以管理,且不完全安全。

若要了解有关可以上传的客户端证书类型的详细信息,请参阅 应用程序网关的相互身份验证概述

创建资源组

首先在订阅中创建新的资源组。

$resourceGroup = New-AzResourceGroup -Name $rgname -Location $location -Tags @{ testtag = "APPGw tag"}

创建虚拟网络

为应用程序网关部署虚拟网络。

$gwSubnet = New-AzVirtualNetworkSubnetConfig -Name $gwSubnetName -AddressPrefix 10.0.0.0/24
$vnet = New-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname -Location $location -AddressPrefix 10.0.0.0/16 -Subnet $gwSubnet
$vnet = Get-AzVirtualNetwork -Name $vnetName -ResourceGroupName $rgname
$gwSubnet = Get-AzVirtualNetworkSubnetConfig -Name $gwSubnetName -VirtualNetwork $vnet

创建公共 IP

创建用于应用程序网关的公共 IP。

$publicip = New-AzPublicIpAddress -ResourceGroupName $rgname -name $publicIpName -location $location -AllocationMethod Static -sku Standard

创建应用程序网关 IP 配置

创建 IP 配置和前端端口。

$gipconfig = New-AzApplicationGatewayIPConfiguration -Name $gipconfigname -Subnet $gwSubnet
$fipconfig = New-AzApplicationGatewayFrontendIPConfig -Name $fipconfigName -PublicIPAddress $publicip
$port = New-AzApplicationGatewayFrontendPort -Name $frontendPortName  -Port 443

配置前端 TLS/SSL

为应用程序网关配置 TLS/SSL 证书。

$password = ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force
$sslCertPath = $basedir + "/ScenarioTests/Data/ApplicationGatewaySslCert1.pfx"
$sslCert = New-AzApplicationGatewaySslCertificate -Name $sslCertName -CertificateFile $sslCertPath -Password $password

配置客户端身份验证

在应用程序网关上配置客户端身份验证。 有关如何提取要使用的受信任客户端 CA 证书链的详细信息,请参阅 如何提取受信任的客户端 CA 证书链

重要

确保将整个客户端 CA 证书链上传到一个文件中,每个文件只上传一个链。 每个上载文件的最大大小必须小于或等于 25 KB.

注释

建议使用带有相互身份验证的 TLS 1.2,因为自 2025 年 8 月 31 日起,TLS 1.2 将成为强制要求。

$clientCertFilePath = $basedir + "/ScenarioTests/Data/TrustedClientCertificate.cer"
$trustedClient01 = New-AzApplicationGatewayTrustedClientCertificate -Name $trustedClientCert01Name -CertificateFile $clientCertFilePath
$sslPolicy = New-AzApplicationGatewaySslPolicy -PolicyType Predefined -PolicyName "AppGwSslPolicy20170401S"
$clientAuthConfig = New-AzApplicationGatewayClientAuthConfiguration -VerifyClientCertIssuerDN
$sslProfile01 = New-AzApplicationGatewaySslProfile -Name $sslProfile01Name -SslPolicy $sslPolicy -ClientAuthConfiguration $clientAuthConfig -TrustedClientCertificates $trustedClient01
$listener = New-AzApplicationGatewayHttpListener -Name $listenerName -Protocol Https -SslCertificate $sslCert -FrontendIPConfiguration $fipconfig -FrontendPort $port -SslProfile $sslProfile01

配置后端池和设置

为您的应用程序网关设置后端池及其配置。 (可选)为端到端 TLS/SSL 加密设置后端受信任的根证书。

$certFilePath = $basedir + "/ScenarioTests/Data/ApplicationGatewayAuthCert.cer"
$trustedRoot = New-AzApplicationGatewayTrustedRootCertificate -Name $trustedRootCertName -CertificateFile $certFilePath
$pool = New-AzApplicationGatewayBackendAddressPool -Name $poolName -BackendIPAddresses www.microsoft.com, www.bing.com
$poolSetting = New-AzApplicationGatewayBackendHttpSettings -Name $poolSettingName -Port 443 -Protocol Https -CookieBasedAffinity Enabled -PickHostNameFromBackendAddress -TrustedRootCertificate $trustedRoot

配置规则

在应用程序网关上设置规则。

$rule = New-AzApplicationGatewayRequestRoutingRule -Name $ruleName -RuleType basic -BackendHttpSettings $poolSetting -HttpListener $listener -BackendAddressPool $pool

为将来的侦听器设置默认 TLS/SSL 策略

设置相互身份验证时,已设置特定于侦听器的 TLS/SSL 策略。 在此步骤中,可以选择为创建的未来侦听器设置默认 TLS/SSL 策略。

$sslPolicyGlobal = New-AzApplicationGatewaySslPolicy -PolicyType Predefined -PolicyName "AppGwSslPolicy20170401"

创建应用程序网关

使用我们创建的所有内容,部署应用程序网关。

$sku = New-AzApplicationGatewaySku -Name Standard_v2 -Tier Standard_v2
$appgw = New-AzApplicationGateway -Name $appgwName -ResourceGroupName $rgname -Zone 1,2 -Location $location -BackendAddressPools $pool -BackendHttpSettingsCollection $poolSetting -FrontendIpConfigurations $fipconfig -GatewayIpConfigurations $gipconfig -FrontendPorts $port -HttpListeners $listener -RequestRoutingRules $rule -Sku $sku -SslPolicy $sslPolicyGlobal -TrustedRootCertificate $trustedRoot -AutoscaleConfiguration $autoscaleConfig -TrustedClientCertificates $trustedClient01 -SslProfiles $sslProfile01 -SslCertificates $sslCert

清理资源

如果不再需要资源组、应用程序网关和所有相关资源,可以使用 Remove-AzResourceGroup 将其删除。

Remove-AzResourceGroup -Name $rgname

续订过期的客户端 CA 证书

如果客户端 CA 证书已过期,可以通过以下步骤更新网关上的证书:

  1. 登录到 Azure
    Connect-AzAccount
    Select-AzSubscription -Subscription "<sub name>"
    
  2. 获取应用程序网关配置
    $gateway = Get-AzApplicationGateway -Name "<gateway-name>" -ResourceGroupName "<resource-group-name>"
    
  3. 从网关中删除受信任的客户端证书
    Remove-AzApplicationGatewayTrustedClientCertificate -Name "<name-of-client-certificate>" -ApplicationGateway $gateway
    
  4. 将新证书添加到网关
    Add-AzApplicationGatewayTrustedClientCertificate -ApplicationGateway $gateway -Name "<name-of-new-cert>" -CertificateFile "<path-to-certificate-file>"
    
  5. 使用新证书更新网关
    Set-AzApplicationGateway -ApplicationGateway $gateway
    

后续步骤