我需要为 Java 应用程序设置一个真正轻量级的 HTTPS 服务器。它是我们的开发实验室中使用的一个模拟器,用于模拟一台设备在野外接受的 HTTPS 连接。因为它纯粹是一个轻量级开发工具,根本不以任何方式用于生产,所以我很高兴能够绕过认证和尽可能多的谈判。
我计划在 Java 6 SE 中使用 HttpsServer 类,但我很难让它正常工作。作为测试客户端,我在 cygwin 命令行 (wget https://[address]:[port]
) 中使用 wget
,但是 wget
code> 报告“无法建立 SSL 连接”。
如果我使用 -d
选项运行 wget
进行调试,它会告诉我“SSL 握手失败”。
我花了 30 分钟在谷歌上搜索这个内容,一切似乎都指向了相当无用的 Java 6 文档,该文档描述了这些方法,但实际上并没有讨论如何让该死的东西说话,也根本没有提供任何示例代码。
有人能把我推向正确的方向吗?
I need to set up a really lightweight HTTPS server for a Java application. It's a simulator that's being used in our development labs to simulate the HTTPS connections accepted by a piece of equipment in the wild. Because it's purely a lightweight development tool and isn't used in production in any way at all, I'm quite happy to bypass certifications and as much negotiation as I can.
I'm planning on using the HttpsServer
class in Java 6 SE but I'm struggling to get it working. As a test client, I'm using wget
from the cygwin command line (wget https://[address]:[port]
) but wget
reports that it was "Unable to establish SSL connection".
If I run wget
with the -d
option for debugging it tells me "SSL handshake failed".
I've spent 30 minutes googling this and everything seems to just point back to the fairly useless Java 6 documentation that describes the methods but doesn't actually talk about how to get the darn thing talking or provide any example code at all.
Can anyone nudge me in the right direction?
发布评论
评论(5)
我最终使用的是:
要生成密钥库:
另请参阅此处。
storepass 和 keypass 可能不同,在这种情况下,
ks.load
和kmf.init
必须分别使用 storepass 和 keypass。What I eventually used was this:
To generate a keystore:
See also here.
Potentially storepass and keypass might be different, in which case the
ks.load
andkmf.init
must use storepass and keypass, respectively.我更新了您对 HTTPS 服务器(不是基于套接字)的答案。它可能有助于 CSRF 和 AJAX 调用。
要创建自签名证书:
I updated your answer for a HTTPS server (not socket-based). It might help with CSRF and AJAX calls.
To create a self-signed certificate:
使用
ServerSocket
您可以使用
HttpsServer
的构建更加轻量级:ServerSocket
。单线程
以下程序是一个非常简单的单线程服务器,侦听端口 8443。消息使用
./keystore.jks
中的密钥通过 TLS 进行加密:With
ServerSocket
You can use the class that
HttpsServer
is built around to be even more light-weight:ServerSocket
.Single-threaded
The following program is a very simple, single-threaded server listening on port 8443. Messages are encrypted with TLS using the keys in
./keystore.jks
:Here's a project using this socket-based approach.
Multi-threaded
To use more than one thread for the server, you can employ a thread pool:
Create a certificate
Use the
keytool
to create a self-signed certificate (you can get a proper certificate from Let's Encrypt for free):Contact the server
After starting the server, connect to it with curl:
This will fetch a message from the server:
Inspect which protocol and cipher suite were established by curl and your server with
Using JDK 13 and curl 7.66.0, this produced
Refer to Java Network Programming by Elliotte Rusty Harold for more on the topic.
虽然这个问题确实很老了,但有人向我提到了这个话题并问是否可以简化它。大多数答案都很好地演示了如何使用 sun 设置一个简单的 https 服务器,但我想提供一种替代方案,希望它更容易一些。
对于此设置,我假设您已经拥有密钥库和信任库。
其余端点:
服务器配置:
我需要在此处添加一些免责声明...我使用 Github - SSLContext-Kickstart 库可轻松构建 SSLContext。它是由我维护的。您不需要使用它,因为其他人提供了一种仅使用普通 java 来构造它的方法。
Although this question is really old, someone mentioned me this topic and asked if it could be simplified. Most of the answers demonstrate very well how to setup a simple https server with sun, but I want to provide an alternative which is hopefully a bit easier.
For this setup I am assuming you already have the keystore and truststore in place.
The rest endpoint:
Server configuration:
I need to add some disclaimer here... I use SSLFactory class from the Github - SSLContext-Kickstart library to easily construct a SSLContext. It is maintained by me. You don't need to use it as others have provided a way to construct it with just plain java.
只是提醒其他人:上述解决方案中的 com.sun.net.httpserver.HttpsServer 不是 Java 标准的一部分。虽然它是 与 Oracle/OpenJDK JVM 捆绑,它并未包含在所有 JVM 中,因此这不会在任何地方开箱即用。
您可以将多种轻量级 HTTP 服务器嵌入到支持 HTTPS 并在任何 JVM 上运行的应用程序中。
其中之一是 JLHTTP - Java 轻量级 HTTP 服务器,它是一个小型单文件服务器(或~50K/35K jar),没有依赖项。设置密钥库、SSLContext 等与上面类似,因为它也依赖于标准 JSSE 实现,或者您可以指定标准系统属性来配置 SSL。您可以查看常见问题解答或代码及其文档了解详细信息。
免责声明:我是 JLHTTP 的作者。您可以亲自检查并确定它是否适合您的需求。我希望你觉得它有用:-)
Just a reminder to others:
com.sun.net.httpserver.HttpsServer
in the solutions above is not part of the Java standard. Although it is bundled with the Oracle/OpenJDK JVM, it is not included in all JVMs so this will not work out of the box everywhere.There are several lightweight HTTP servers out there that you can embed in your application that support HTTPS and run on any JVM.
One of them is JLHTTP - The Java Lightweight HTTP Server which is a tiny one-file server (or ~50K/35K jar) with no dependencies. Setting up the keystore, SSLContext etc. is similar to the above, since it also relies on the standard JSSE implementation, or you can specify the standard system properties to configure SSL. You can see the FAQ or the code and its documentation for details.
Disclaimer: I'm the author of JLHTTP. You can check it out for yourself and determine if it suits your needs. I hope you find it useful :-)