Java服务器是否可以在一个端口上同时接受SSL和纯文本连接? [英] Can a Java server accept both SSL and plaintext connections on one port?

查看:160
本文介绍了Java服务器是否可以在一个端口上同时接受SSL和纯文本连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题说明了。

如果我尝试将ServerSocket和SSLServerSocket绑定到同一个端口,我会收到错误消息。如果客户端尝试连接到不带SSL的SSLServerSocket,则accept()方法会引发错误。如果客户端尝试通过SSL连接到ServerSocket,我不知道如何建立安全连接。

If I try to bind a ServerSocket and a SSLServerSocket to the same port I get an error. If a client tries to connect to an SSLServerSocket without SSL, the accept() method throws an error. If a client tries to connect to a ServerSocket via SSL I have no idea how I would go about establishing a secure connection.

甚至可能吗?

推荐答案

您可以接受正常的套接字连接,并在稍后阶段使用 SSLSocketFactory.createSocket(Socket s,String host,int port,boolean autoClose) (和 SSLSocket.setUseClientMode(false)在服务器端)。

You can accept a normal socket connection and upgrade it to SSL/TLS at a later stage, using SSLSocketFactory.createSocket(Socket s, String host, int port, boolean autoClose) (and SSLSocket.setUseClientMode(false) on the server side).

您需要在明文协议中定义一个命令,以便双方同意进行升级(例如,与SMTP或LDAP中的 STARTTLS 命令类似)。

You'll need to defined a command in your plaintext protocol so that both sides can agree about an upgrade taking place (similarly to STARTTLS commands in SMTP or LDAP for example).

或者,您可以使用端口统一(如它可以使用Grizzly完成),您可以尝试检测客户端是否使用SSL / TLS客户端Hello消息启动连接。这可能比较棘手,因为您必须提前读取以检测数据包类型(因此您可能需要保留该缓冲区并将其内容传递到 SSLEngine ,而不是直接使用 SSLSocket

Alternatively, you could use port unification (as it can be done with Grizzly), whereby you try to detect whether the client initiates the connection with an SSL/TLS Client Hello message. It can be trickier to do, since you'd have to read ahead to detect the packet type (so you'd probably need to keep that buffer and pass its content into an SSLEngine, instead of being able to use the SSLSocket directly).

这篇关于Java服务器是否可以在一个端口上同时接受SSL和纯文本连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆