小程序要求的 TLS 版本必须大于等于 1.2

本文介绍了解决微信小程序中遇到的TLS版本过低错误的方法。通过使用PowerShell脚本更新注册表来启用TLS 1.2并禁用SSL 2.0,确保了与微信小程序的兼容性和安全性。

微信小程序发现wx.request调试报错: 小程序要求的 TLS 版本必须大于等于 1.2



解决方法

执行powershell脚本
Powershell拥有自己的脚本,扩展名为“.ps1”.把下列内容复制保存成.ps1的后缀名,然后执行。

执行:开始->运行->cmd,在命令行下输入 PowerShell 进入 windows PowerShell

PS C:\PS> test.ps1

在 PowerShell中运行以下内容, 然后重启服务器


# Enables TLS 1.2 on windows Server 2008 R2 and Windows 7

# These keys do not exist so they need to be created prior to setting values.
md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2"
md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server"
md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client"

# Enable TLS 1.2 for client and server SCHANNEL communications
new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -name "Enabled" -value 1 -PropertyType "DWord"
new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -name "DisabledByDefault" -value 0 -PropertyType "DWord"
new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -name "Enabled" -value 1 -PropertyType "DWord"
new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -name "DisabledByDefault" -value 0 -PropertyType "DWord"

# Disable SSL 2.0 (PCI Compliance)
md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server"
new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server" -name Enabled -value 0 -PropertyType "DWord"
# Enables TLS 1.2 on Windows Server 2008 R2 and Windows 7 # These keys do not exist so they need to be created prior to setting values. md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2" md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" # Enable TLS 1.2 for client and server SCHANNEL communications new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -name "Enabled" -value 1 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server" -name "DisabledByDefault" -value 0 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -name "Enabled" -value 1 -PropertyType "DWord" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client" -name "DisabledByDefault" -value 0 -PropertyType "DWord" # Disable SSL 2.0 (PCI Compliance) md "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server" new-itemproperty -path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server" -name Enabled -value 0 -PropertyType "DWord"



初次执行脚本时,可能会碰到一个异常:
File ” C:\PS\ test .ps1″ cannot be loaded because the
execution of scripts is disabled on this system. Please see
“get-help about_signing” for more details.
At line:1 char:10

这是powershell的默认安全设置禁用了执行脚本,要启用这个功能需要拥有管理员的权限。

开启:set-executionpolicy remotesigned

关闭:Set-ExecutionPolicy Restricted



为了解决微信小程序要求 TLS 版本不低于 1.2 的问题,需从服务器配置和系统支持两个方面入手。以下是具体操作方法: ### 服务器端配置 1. **检查服务器 TLS 支持版本**: - 如果服务器运行在 Windows 上,可以通过注册表编辑器查看当前启用的 TLS 版本。进入以下路径: ``` HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\ ``` 检查是否存在并启用了 TLS 1.2 相关的项 [^1]。 2. **启用 TLS 1.2**: - 在上述注册表路径中,如果没有 TLS 1.2 的项,可以右键点击 `Protocols`,选择“新建” -> “项”,然后创建名为 `TLS 1.2` 的项。 - 确保在 `TLS 1.2` 下包含 `Client` 和 `Server` 子项,以启用客户端和服务器端的 TLS 1.2 支持 [^2]。 3. **禁用不安全的旧版本 TLS**: - 如果服务器启用了 TLS 1.0 或 TLS 1.1,建议将其禁用以提高安全性。进入对应版本的注册表项(如 `TLS 1.1`),在 `Client` 和 `Server` 子项下创建一个名为 `Enabled` 的 DWORD 值,并将其设置为 `0` [^3]。 4. **重启服务器**: - 修改注册表后,需要重启服务器以确保配置生效。 ### 客户端支持 - 微信小程序本身依赖于客户端设备的 TLS 支持情况。例如,iOS 9 及以上版本默认支持 TLS 1.2,但部分 Android 设备可能需要更新系统或使用 HTTPS 库来支持更高版本TLS。 - 对于微信小程序开发者,需要确保服务器地址在小程序后台配置为 HTTPS,并且服务器证书有效且由可信的 CA 签发。 ### 验证配置 - 使用在线工具(如 [SSL Labs](https://www.ssllabs.com/))测试服务器的 TLS 配置,确保 TLS 1.2 已启用且评分良好。 - 在微信小程序中,可以使用 `wx.request` 发起 HTTPS 请求,观察是否仍然报错。如果成功请求,说明 TLS 配置已正确生效。 ### 示例代码:检查服务器 TLS 支持情况 以下是一个简单的 Python 脚本,用于检查远程服务器的 TLS 支持情况: ```python import ssl import socket def check_tls_support(host, port=443): context = ssl.create_default_context() try: with socket.create_connection((host, port)) as sock: with context.wrap_socket(sock, server_hostname=host) as ssock: print(f"Connected with TLS version: {ssock.version()}") except Exception as e: print(f"Error: {e}") # 示例:检查 example.com 的 TLS 支持 check_tls_support("example.com") ```
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值