你当前正在访问 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 即可查找版本。 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。 如果在本地运行 PowerShell,则还需运行 Login-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

配置前端 SSL

配置应用程序网关的 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 证书链,并保证每个文件中只有一个链。

备注

建议将 TLS 1.2 与相互身份验证配合使用,因为将来会强制要求使用 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

配置后端池和设置

设置应用程序网关的后端池和设置。 (可选)为端到端 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

为日后接入的侦听器设置默认 SSL 策略

设置相互身份验证时,已设置特定于侦听器的 SSL 策略。 在此步骤中,你可以选择性地为日后创建的侦听器设置默认 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
    

后续步骤