简体   繁体   English

如何在Java Sun HttpsServer中使用LetsEncrypt证书?

[英]How do I use LetsEncrypt certificates in Java Sun HttpsServer?

Using various results from a lot of googling I've managed to make a Java class that handles downloading of certificates from LetsEncrypt, pretty neat. 使用大量Google搜索的各种结果,我设法制作了一个Java类,可以处理从LetsEncrypt下载证书的过程,非常简洁。 (Available here if anyone is interested) (如果有兴趣,请点击此处

With that I get this content in a folder as output: 这样我就可以在文件夹中获取此内容作为输出:

chain.crt
domain.crt
domain.csr
domain.key
user.key

I've been using a com.sun.net.httpserver.HttpsServer with a self-signed certificate that I followed some guide to generate that gave me a .jks file, which has worked great up until now using test-clients. 我一直在使用带有自签名证书的com.sun.net.httpserver.HttpsServer,我遵循一些指南进行生成,该指南为我提供了一个.jks文件,该文件在使用测试客户端之前一直有效。 But now I need browsers to accept the certificate without giving verification errors, so I would like to use the certificates downloaded from LetsEncrypt in this HttpsServer but I have no idea how to go about doing that. 但是现在我需要浏览器在不给出验证错误的情况下接受证书,因此我想在此HttpsServer中使用从LetsEncrypt下载的证书,但是我不知道该怎么做。 I have googled a bit on it but I haven't found anything that has worked for me, though I have to admit I don't really understand most of the things I'm doing here, and I don't even know which of the 5 files above I should try to use. 我已经用谷歌搜索了一下,但是没有找到任何对我有用的东西,尽管我不得不承认我并不真正了解我在这里所做的大部分事情,而且我什至都不知道我应该尝试使用上面的5个文件。

My current code setting up my HttpsServer with an existing generated .jks file: (Also available here ) 我当前的代码使用一个现有的生成的.jks文件设置我的HttpsServer:(也可以在此处获得

  public void initHttpsServer(int port, String certificateFilePath, String sslPassword)
  {
    try
    {
      this.server = HttpsServer.create(new InetSocketAddress(port), 0);
      SSLContext sslContext = SSLContext.getInstance("TLS");
      char[] password = sslPassword.toCharArray();
      KeyStore ks = KeyStore.getInstance("JKS");
      FileInputStream fis = new FileInputStream(certificateFilePath);
      ks.load(fis, password);
      KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
      kmf.init(ks, password);
      TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
      tmf.init(ks);
      sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
      this.server.setHttpsConfigurator(new HttpsConfigurator(sslContext)
      {
        @Override
        public void configure(HttpsParameters params)
        {
          try
          {
            SSLContext c = SSLContext.getDefault();
            SSLEngine engine = c.createSSLEngine();
            params.setNeedClientAuth(false);
            params.setCipherSuites(engine.getEnabledCipherSuites());
            params.setProtocols(engine.getEnabledProtocols());
            SSLParameters defaultSSLParameters = c.getDefaultSSLParameters();
            params.setSSLParameters(defaultSSLParameters);
          }

While looking for solutions I've come across some answers that includes running commands from a cmd to generate different sort of files. 在寻找解决方案时,我遇到了一些答案,其中包括从cmd运行命令以生成不同种类的文件。 I need all of this to work during runtime so ways to do it programmatically is preferred. 我需要所有这些在运行时中正常工作,因此以编程方式进行操作是首选。

Also I have a sort of related question, can I update a running instance of HttpsServer to use newly downloaded certificates when the old LetsEncrypt certificates expire and I have downloaded new ones, or will I have to restart the server? 我还有一个相关的问题,当旧的LetsEncrypt证书到期并且我已经下载了新的证书时,是否可以将正在运行的HttpsServer实例更新为使用新下载的证书,还是必须重新启动服务器?

Any help in figuring out how to make this work is much appreciated. 非常感谢在弄清楚如何进行此工作方面的任何帮助。

Aside: calling this JKS file 'certificateFile' is misleading; 另外:将这个JKS文件称为'certificateFile'具有误导性; a JKS (or other keystore like PKCS12) contains usually several certificates, but usually including this case also and critically the privatekey. JKS(或诸如PKCS12之类的其他密钥库)通常包含多个证书,但通常也包括这种情况,并且至关重要地包括私钥。

Usually the harder part is reading the key file, and since you are using acme4j it handles that for you. 通常最困难的部分是读取密钥文件,并且由于您使用的是acme4j,因此它会为您处理。 Not tested due to lack of infrastructure, but do something like: 由于缺乏基础架构,因此未经过测试,但是请执行以下操作:

KeyPair kp = KeyPairUtils.readKeyPair (new FileReader ("domain.key"));
// add error handling and close much like your getOrCreateKeyPair 
CertificateFactory cf = CertificateFactory.getInstance ("X.509");
Certificate cert0 = cf.generateCertificate (new FileInputStream ("domain.crt"));
Certificate cert1 = cf.generateCertificate (new FileInputStream ("chain.crt"));
// add similar error handling and close

KeyStore ks = KeyStore.getInstance ("jks"); // type doesn't really matter since it's in memory only
ks.load (null); 
ks.setKeyEntry ("anyalias", kp.getPrivate(), password, new Certificate[]{cert0,cert1});

// now create a KMF and KeyManager from this KeyStore,
// optionally a TMF and TrustManager, then SSLContext etc as in your existing code

I don't know if the Sun HttpsServer can change the KeyManager data while running; 我不知道Sun HttpsServer在运行时是否可以更改KeyManager数据。 if I have time later I'll try to look at that. 如果以后有时间,我会尝试看看。 But clearly you need to get it started before changing it is an issue :-) 但是很明显,您需要先开始才能进行更改,这是一个问题:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中使用“多个” SSL证书? - How do I Use “Multiple” SSL certificates in Java? 如何在客户端 java 应用程序中使用客户端证书? - How do I use client certificates in a client java application? 使用com.sun.net.httpserver.HttpsServer-如何指定协议? - Using com.sun.net.httpserver.HttpsServer - how to specify protocol? 如何使用 com.sun.net.httpserver.HttpsServer 要求客户端证书 - How to require client certificate with com.sun.net.httpserver.HttpsServer 如何在使用sun HttpsServer时获取ssl会话 - How to get the ssl session when using the sun HttpsServer 如何创建 Java HttpsServer 多线程? - how create Java HttpsServer multi threaded? 如何在 Java HTTPSServer 中添加中间证书 - How to add the intermediate certificate within Java HTTPSServer 如何强制Eclipse使用Sun Java? - How can I force Eclipse to use Sun Java? 如何将 LetsEncrypt SSL 安装到运行 Tomcat 8 和 Java 8 平台的 AWS Elastic Beanstalk 应用程序上 - How do I install LetsEncrypt SSL onto AWS Elastic Beanstalk application running Tomcat 8 with Java 8 Platform 如何在分布式Jenkins构建环境中管理Java证书? - How do I manage java certificates on a distributed Jenkins build environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM