Tomcat可以在不重新启动的情况下重新加载其SSL证书吗?

sde*_*eer 16 ssl tomcat

我有一个后台进程,可以更新 Tomcat 用于其 SSL 凭据的密钥库。我希望能够让 Tomcat 自动重新加载它,而无需手动重启。

是否可以在不重新启动的情况下让 Tomcat 重新加载它,或者是否可以通过编程方式来代替它?

小智 8

您可以重新启动单个 Tomcat 连接器,即在更改 jssecacert 文件后可以像 8443 这样的端口重新启动。

这是我在添加/删除证书后用于重新启动 tomcat 连接器的完整代码/方法。

// Stop and restart the SSL connection so that the tomcat server will
// re-read the certificates from the truststore file.
public void refreshTrustStore() throws Exception 
{
    try 
    {
        //following line should be replaced based on where you get your port number. You may pass in as argument to this method
        String httpsPort = configurationManager.getHttpsPort();
        String objectString = "*:type=Connector,port=" + httpsPort + ",*";

        final ObjectName objectNameQuery = new ObjectName(objectString); 

        for (final MBeanServer server: MBeanServerFactory.findMBeanServer(null))
        {
            if (!server.queryNames(objectNameQuery, null).isEmpty())
            {
                MBeanServer mbeanServer = server;
                ObjectName objectName = (ObjectName) server.queryNames(objectNameQuery, null).toArray()[0];

                mbeanServer.invoke(objectName, "stop", null, null);

                // Polling sleep to reduce delay to safe minimum.
                // Use currentTimeMillis() over nanoTime() to avoid issues
                // with migrating threads across sleep() calls.
                long start = System.currentTimeMillis();
                // Maximum of 6 seconds, 3x time required on an idle system.
                long max_duration = 6000L;
                long duration = 0L;
                do
                {
                    try
                    {
                        Thread.sleep(100);
                    }
                    catch (InterruptedException e)
                    {
                        Thread.currentThread().interrupt();
                    }

                    duration = (System.currentTimeMillis() - start);
                } while (duration < max_duration &&
                        server.queryNames(objectNameQuery, null).size() > 0);

                // Use below to get more accurate metrics.
                String message = "TrustStoreManager TrustStore Stop: took " + duration + "milliseconds";
                logger.information(message);

                mbeanServer.invoke(objectName, "start", null, null);

                break;
            }
        }
    } 
    catch (Exception exception) 
    {
        // Log and throw exception
            throw exception
    }
}
Run Code Online (Sandbox Code Playgroud)


mah*_*nsc 7

我不相信有一种方法可以自动执行此操作,尽管您的后台进程可以自动重新启动 tomcat。密钥库仅在 jvm 初始化时读取一次。如果您要编写自己的处理程序来定期重新检查密钥库,那么可能有一个解决方案,但我个人在 Internet 上没有找到任何示例。


小智 5

从 tomcat v8.5.24 开始,现在有一种方法可以做到这一点。

他们引入了 2 个方法,命名为:

  1. reloadSslHostConfig(String hostName) - 重新加载特定主机
  2. reloadSslHostConfigs() - 重新加载所有

可以通过多种方式调用它们:

  1. 使用 jmx
  2. 使用管理器服务(在 tomcat v9.xx 中)
  3. 通过制作自定义协议 - 我在研究过程中发现了这种方式

在 tomcat 文档中可以轻松获得方式 1 和方式 2 的详细信息。

如何使用方式3的详细信息:

  1. 制作一个扩展您选择的协议的类,例如。Http11Nio协议
  2. 覆盖所需的方法并在其中调用 super 以保持默认行为
  3. 在这个类中创建一个线程来不时调用 reloadSslHostConfigs 方法
  4. 将这个类打包成一个jar包,放到tomcat的lib文件夹中
  5. 在 server.xml 中的连接器中编辑协议以使用此自定义协议

在下面找到示例代码:

主要协议类:

package com.myown.connector;

import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentMap;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.net.ssl.SSLSessionContext;

import org.apache.coyote.http11.Http11NioProtocol;
import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;
import org.apache.tomcat.util.modeler.Registry;
import org.apache.tomcat.util.net.AbstractEndpoint;
import org.apache.tomcat.util.net.AbstractJsseEndpoint;
import org.apache.tomcat.util.net.GetSslConfig;
import org.apache.tomcat.util.net.SSLContext;
import org.apache.tomcat.util.net.SSLHostConfig;
import org.apache.tomcat.util.net.SSLHostConfigCertificate;
import org.apache.tomcat.util.net.SSLImplementation;
import org.apache.tomcat.util.net.SSLUtil;

public class ReloadProtocol extends Http11NioProtocol {

    private static final Log log = LogFactory.getLog(Http12ProtocolSSL.class);

    public ReloadProtocol() {
        super();
        RefreshSslConfigThread refresher = new 
              RefreshSslConfigThread(this.getEndpoint(), this);
        refresher.start();
    }

    @Override
    public void setKeystorePass(String s) {
        super.setKeystorePass(s);
    }

    @Override
    public void setKeyPass(String s) {
        super.setKeyPass(s);
    }

    @Override
    public void setTruststorePass(String p) {
        super.setTruststorePass(p);
    }

    class RefreshSslConfigThread extends Thread {

        AbstractJsseEndpoint<?> abstractJsseEndpoint = null;
        Http11NioProtocol protocol = null;

        public RefreshSslConfigThread(AbstractJsseEndpoint<?> abstractJsseEndpoint, Http11NioProtocol protocol) {
            this.abstractJsseEndpoint = abstractJsseEndpoint;
            this.protocol = protocol;
        }

        public void run() {
            int timeBetweenRefreshesInt = 1000000; // time in milli-seconds
            while (true) {
                try {
                        abstractJsseEndpoint.reloadSslHostConfigs();
                        System.out.println("Config Updated");
                } catch (Exception e) {
                    System.out.println("Problem while reloading.");
                }
                try {
                    Thread.sleep(timeBetweenRefreshesInt);
                } catch (InterruptedException e) {
                    System.out.println("Error while sleeping");
                }
            }
        }
   }
}
Run Code Online (Sandbox Code Playgroud)

server.xml 中的连接器应该将此作为协议提及:

<Connector protocol="com.myown.connector.ReloadProtocol"
 ..........
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。