使用 Spring 为 RabbitMQ 配置创建 SSLContext

Create SSLContext for RabbitMQ configuration with Spring

我需要创建并设置一个 SSLContext 以使用 Spring 框架连接到 RabbitMQ 队列。我使用@RabbitListener 注释并且没有使用 SSLContext,通过将主机名、端口、用户名和密码设置到 application.properties 文件中,所有的东西都很完美。

但是如何手动设置 SSLContext? application.properties 想要 JKS,但我不能使用它。我看到了一些配置bean,但是我在网上找到的一点都不清楚。

如何在 Spring 框架上配置 manually/programmatically(精确的 SSLContext)RabbitMQ?

创建一个扩展 RabbitConnectionFactoryBean 并覆盖 createSSLContext() 的 class。

/**
 * Override this method to create and/or configure the {@link SSLContext} used
 * by the {@link ConnectionFactory}.
 * @return The {@link SSLContext}.
 * @throws NoSuchAlgorithmException if the algorithm is not available.
 * @since 1.4.4
 */
protected SSLContext createSSLContext() throws NoSuchAlgorithmException {
    return SSLContext.getInstance(this.sslAlgorithm);
}

然后声明一个 CachingConnectionFactory bean,它使用由工厂 bean 创建的 rabbit 连接工厂。