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

标签 java android sockets ssl

我正在使用服务器-客户端模型制作一个非常小规模的应用程序,其中服务器是一个 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/

相关文章:

android - 如何在不在 Android 上保存视频的情况下从相机逐帧捕获?

java - RabbitMQ HelloWorld 消息在本地主机上被阻止?

java - Google 端点 - Android GoogleAuthIOException Tic Tac Toe - 删除了 clientIds

java - 为 java 1.6 切换类型字符串

java - 如何获取当前星期几(如星期一等)并在 android 中进行比较

android - 在 <div> 中显示 AR 浏览器。是否可以?

java - Intershop 代码部署工作流程

django - 使用 gevent socket.py 时无法使用 Django 连接到 smtp 服务器

c - 与许多实时设备保持连接

Java 更新程序问题