Search code examples
javaspringspring-rabbit

Create SSLContext for RabbitMQ configuration with Spring


I need to create and set a SSLContext to connect to a RabbitMQ queue with Spring framework. I use the @RabbitListener annotation and without SSLContext, by setting hostname, port, username and password into the application.properties file, all the things works just perfect.

But how can I manually set a SSLContext? The application.properties want a JKS and I can't use it. I see some configurations beans but what I found online is not clear at all.

How can I configure manually/programmatically (SSLContext precisly) RabbitMQ on Spring framework?


Solution

  • Create a class that extends RabbitConnectionFactoryBean and override 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);
    }
    

    Then declare a CachingConnectionFactory bean that uses the rabbit connection factory created by the factory bean.