首页 > 解决方案 > 使用 Spring 为 RabbitMQ 配置创建 SSLContext

问题描述

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

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

如何在 Spring 框架上手动/以编程方式(精确地 SSLContext)配置 RabbitMQ?

标签: javaspringspring-rabbit

解决方案


创建一个扩展RabbitConnectionFactoryBean和覆盖的类createSSLContext()

/**
 * 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创建的rabbit连接工厂的bean。


推荐阅读