GithubHelp home page GithubHelp logo

nepitwin / rabbitssl Goto Github PK

View Code? Open in Web Editor NEW
24.0 24.0 18.0 179 KB

Example Java, Spring-Boot and Python RabbitMQ SSL configuration

License: Apache License 2.0

Java 82.31% Python 17.69%
java python rabbitmq spring-boot ssl tls

rabbitssl's People

Contributors

nepitwin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

rabbitssl's Issues

Caused by: java.net.SocketException: Connection reset by peer: socket write error

I'm trying to connect to rabbitMQ over SSL using Spring Boot 2.7.4 and java 11.0.14

I have added the following configurations:

properties file:
rabbit.username=admin
rabbit.password=admin
rabbit.host=localhost
rabbit.port=5671
rabbit.ssl=TLSv1.2
rabbit.keystore.name=client_key.p12
rabbit.keystore.password=rabbitstore
rabbit.truststore=server_store.jks
rabbit.truststore.password=rabbitstore

Configuration class:

@Configuration
@PropertySource("classpath:rabbit.properties")
public class RabbitConfiguration {

    /**
     * Default sample channel name to respond for requests from clients.
     */
    public static final String DEFAULT_QUEUE = "sample_queue";

    /**
     * Environment properties file from rabbitmq configuration.
     */
    @Autowired
    private Environment env;

    /**
     * Establish a connection to a rabbit mq server.
     * @return Rabbit connection factory for rabbitmq access.
     * @throws IOException If wrong parameters are used for connection.
     */
    @Bean
    public RabbitConnectionFactoryBean connectionFactoryBean() throws IOException {
        RabbitConnectionFactoryBean connectionFactoryBean = new RabbitConnectionFactoryBean();
        connectionFactoryBean.setHost(Objects.requireNonNull(env.getProperty("rabbit.host")));
        connectionFactoryBean.setPort(Integer.parseInt(Objects.requireNonNull(env.getProperty("rabbit.port"))));
        connectionFactoryBean.setUsername(Objects.requireNonNull(env.getProperty("rabbit.username")));
        connectionFactoryBean.setPassword(Objects.requireNonNull(env.getProperty("rabbit.password")));

        // SSL-Configuration if set
        if(env.getProperty("rabbit.ssl") != null) {
            connectionFactoryBean.setUseSSL(true);
            connectionFactoryBean.setSslAlgorithm(Objects.requireNonNull(env.getProperty("rabbit.ssl")));

            // This information should be stored safely !!!
            connectionFactoryBean.setKeyStore(Objects.requireNonNull(env.getProperty("rabbit.keystore.name")));
            connectionFactoryBean.setKeyStorePassphrase(Objects.requireNonNull(env.getProperty("rabbit.keystore.password")));
            connectionFactoryBean.setTrustStore(Objects.requireNonNull(env.getProperty("rabbit.truststore")));
            connectionFactoryBean.setTrustStorePassphrase(Objects.requireNonNull(env.getProperty("rabbit.truststore.password")));
        }

        return connectionFactoryBean;
    }

    /**
     * Connection factory which established a rabbitmq connection used from a connection factory
     * @param connectionFactoryBean Connection factory bean to create connection.
     * @return A connection factory to create connections.
     * @throws Exception If wrong parameters are used for connection.
     */
    @Bean(name = "GEO_RABBIT_CONNECTION")
    public ConnectionFactory connectionFactory(RabbitConnectionFactoryBean connectionFactoryBean) throws Exception {
        return new CachingConnectionFactory(Objects.requireNonNull(connectionFactoryBean.getObject()));
    }

    /**
     * Queue initialization from rabbitmq to listen a queue.
     * @return An queue to listen for listen receiver.
     */
    @Bean
    public Queue queue() {
        // Create an new queue to handle incoming responds
        return new Queue(DEFAULT_QUEUE, false, false, false, null);
    }

    /**
     * Generates a simple message listener container.
     * @param connectionFactory Established connection to rabbitmq server.
     * @param listenerAdapter   Listener event adapter to listen for messages.
     * @return A simple message container for listening for requests.
     */
    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
                                                    MessageListenerAdapter listenerAdapter) {

        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(DEFAULT_QUEUE);
        container.setMessageListener(listenerAdapter);
        container.setAcknowledgeMode(AcknowledgeMode.AUTO);
        return container;

    }

    /**
     * Message listener adapter to generate a message listener.
     * @param deviceMonitoringReceiver Device receive to for listening.
     * @return A message listener adapter to receive messages.
     */
    @Bean
    public MessageListenerAdapter listenerAdapter(DeviceMonitoringReceiver deviceMonitoringReceiver) {
        return new MessageListenerAdapter(deviceMonitoringReceiver, "receiveMessage");
    }
}

Also I have updated rabbitMQ configurations:

[
  {rabbit, [
     {ssl_listeners, [5671]},
     {ssl_options, [{cacertfile, "D:\\tls-gen\\basic\\result\\ca_certificate.pem"},
                    {certfile,   "D:\\tls-gen\\basic\\result\\server_seliiwvdec53152_certificate.pem"},
                    {keyfile,    "D:\\tls-gen\basic\\result\\server_seliiwvdec53152_key.pem"},
                    {verify,     verify_peer},
                    {fail_if_no_peer_cert, true}]}

   ]}
].

But the application is not starting and throwing

Caused by: java.net.SocketException: Connection reset by peer: socket write error

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.