我正在开发一个应用程序来连接到 WPA2 Enterprise EAP PEAP 网络,以便用户无需输入其凭据。
对于 Android 11 设备,我使用 WifiNetworkSuggestion,因为我认为这是最佳可用选项。 问题是,当我尝试连接时,抛出此异常:
java.lang.IllegalArgumentException:企业配置不安全 在 kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 在 android.net.wifi.WifiNetworkSuggestion$Builder.setWpa2EnterpriseConfig(WifiNetworkSuggestion.java:275)
这就是我初始化配置的方式:
private fun initWpa2EnterpriseWifiSuggestion(wifiConnection: WifiConnectionEntity) =
if (wifiConnection.user != null && wifiConnection.password != null) {
val networkSuggestionBuilder = WifiNetworkSuggestion.Builder()
networkSuggestionBuilder.setSsid(wifiConnection.ssid)
if (wifiConnection.sharedKey != null) {
networkSuggestionBuilder.setWpa2Passphrase(wifiConnection.sharedKey)
}
val enterpriseConfig = WifiEnterpriseConfig()
enterpriseConfig.identity = wifiConnection.user
enterpriseConfig.password = wifiConnection.password
enterpriseConfig.eapMethod = WifiEnterpriseConfig.Eap.PEAP
enterpriseConfig.phase2Method = WifiEnterpriseConfig.Phase2.MSCHAPV2
networkSuggestionBuilder.setWpa2EnterpriseConfig(enterpriseConfig)
Pair(networkSuggestionBuilder.build(), null)
} else {
Pair(
null,
if (wifiConnection.user != null) ERROR_USER_NOT_PROVIDED else ERROR_PASSWORD_NOT_PROVIDED
)
}
我提出建议:
val wifiManager = appContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
val status = wifiManager.addNetworkSuggestions(listOf(wifiSuggestion.first))
if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
trySend(WifiManagementDataResult.Error(null, ERROR_NETWORK_SUGGESTION))
} else {
trySend(WifiManagementDataResult.Success(null, null))
我一直在https://developer.android.com/guide/topics/connectivity/wifi-suggest中读到我需要指定ca证书(setCaCertificate方法)和服务器域名(setAltSubjectMatch方法)。
我还没有找到太多相关信息: CA 证书必须作为应用程序的资源包含并将其转换为 X509Certificate 模型吗? 有人用其他方式联系过吗
如有任何帮助,我们将不胜感激。
更新:我已经能够在设备设置中手动连接到网络。出现“无证书”选项,我了解到该选项已在 Android 11 中删除。 有没有办法在 WifiNetworkSuggestion 中使用“无证书”选项?
更新2:使用
public WifiNetworkSuggestion.Builder setUntrusted (boolean isUntrusted)
方法,不会抛出“企业配置不安全”异常,并出现网络建议对话框。但是,设备未连接到网络,并且配置未保存在操作系统中。
更新3:我一直在另一个网络中进行测试,尽管使用了
public WifiNetworkSuggestion.Builder setUntrusted (boolean isUntrusted)
,但还是抛出了异常。不知道为什么上次没有抛出。
解决办法是设置认证服务器的域名:
val enterpriseConfig = WifiEnterpriseConfig()
enterpriseConfig.identity = wifiConnection.user
enterpriseConfig.password = wifiConnection.password
enterpriseConfig.eapMethod = WifiEnterpriseConfig.Eap.PEAP
enterpriseConfig.phase2Method = WifiEnterpriseConfig.Phase2.MSCHAPV2
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
enterpriseConfig.caCertificates = arrayOf()
enterpriseConfig.altSubjectMatch = "DNS:your.domain.server.com"
}