gpt4 book ai didi

java - 在 android-j2se 上使用 SSL 与服务器客户端套接字通信

转载 作者:太空宇宙 更新时间:2023-11-03 14:43:13 27 4
gpt4 key购买 nike

我正在使用服务器-客户端模型制作一个非常小规模的应用程序,其中服务器是一个 j2se 应用程序,客户端是一个 android 应用程序。

服务器将使用动态 DNS 托管,并且在更改域名时需要可移植。

现在使用 SSL 这可能会有问题,因为证书需要有一个可识别的服务器地址,域名或 IP,两者都可能改变。我认为这是为了防止有人伪装成服务器从不同位置发送相同的公钥。

那么我该如何应对呢?

最佳答案

下面是继续的提示。为验证服务器证书的连接添加一个自定义信任管理器 - 并添加一个自定义主机名验证逻辑,该逻辑将主机名评估为来自可接受的列表 - 或者只是硬编码为 true。

        SSLContext ctx = SSLContext.getInstance("TLS");
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(getTrustedCert());//todo
ctx.init(null, tmf.getTrustManagers(), null);

HttpsURLConnection.setDefaultSSLSocketFactory(ctx.getSocketFactory());

HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return isInAcceptableHostNameList(arg0);//todo
}
});

来自 docs -

This class uses HostnameVerifier and SSLSocketFactory. There are default implementations defined for both classes. However, the implementations can be replaced on a per-class (static) or per-instance basis. All new HttpsURLConnections instances will be assigned the "default" static values at instance creation, but they can be overriden by calling the appropriate per-instance set method(s) before connecting.

关于java - 在 android-j2se 上使用 SSL 与服务器客户端套接字通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41461025/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com