Nota
El acceso a esta página requiere autorización. Puede intentar iniciar sesión o cambiar directorios.
El acceso a esta página requiere autorización. Puede intentar cambiar los directorios.
本文介绍如何使用 PowerShell 在应用程序网关上配置相互身份验证。 相互身份验证是指应用程序网关使用你上传到应用程序网关的客户端证书,对发送请求的客户端进行身份验证。
如果没有 Azure 订阅,可在开始前创建一个试用帐户。
Note
建议使用 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,则还需运行 Connect-AzAccount -Environment AzureChinaCloud
以创建与 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 证书链。
Important
确保将整个客户端 CA 证书链上传到一个文件中,每个文件只上传一个链。 每个上载文件的最大大小必须小于或等于 25 KB.
Note
建议使用带有相互身份验证的 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 证书已过期,可按照以下步骤更新网关上的证书:
- 登录 Azure
Connect-AzAccount -Environment AzureChinaCloud Select-AzSubscription -Subscription "<sub name>"
- 获取应用程序网关配置
$gateway = Get-AzApplicationGateway -Name "<gateway-name>" -ResourceGroupName "<resource-group-name>"
- 删除网关上的受信任的客户端证书
Remove-AzApplicationGatewayTrustedClientCertificate -Name "<name-of-client-certificate>" -ApplicationGateway $gateway
- 将新证书添加到网关
Add-AzApplicationGatewayTrustedClientCertificate -ApplicationGateway $gateway -Name "<name-of-new-cert>" -CertificateFile "<path-to-certificate-file>"
- 用新证书更新网关
Set-AzApplicationGateway -ApplicationGateway $gateway