How to connect to an ActiveMQ server by setting the username and password programmatically?

Nelly Junior picture Nelly Junior · Mar 24, 2017 · Viewed 11.5k times · Source

I create an ActiveMQ server in order to send some messages. I use Apache Activemq version 5.14.4 I want to be asked for username and password when I try to connect to the ActiveMQ server and that's why I added a simple authentification plugin into the activemq.xml as you can see bellow.

String username = "admin";
String password = "anything";
ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        factory.setUserName(username);
        factory.setPassword(password);
        factory.setTrustedPackages(Arrays.asList("com.h4p.server"));
        Connection connection = null;
        Session session = null;
        Destination destination = null;
        try {
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("myQueue");
        } catch (JMSException e) {
            e.printStackTrace();
        }
...

Into the broker element I added a security level using SimpleAuthentificationPlugin:

    <simpleAuthenticationPlugin anonymousAccessAllowed="false">
        <users>
            <authenticationUser username="admin" password="anything" groups="admins"/>
        </users>
    </simpleAuthenticationPlugin>

    <authorizationPlugin>
        <map>
            <authorizationMap>
                <authorizationEntries>
                    <authorizationEntry queue="myQueue" write="admins" read="admins" admin="admins" />  
                    <authorizationEntry topic="ActiveMQ.Advisory.>" write="admins" read="admins" admin="admins" />                      
                </authorizationEntries>
            </authorizationMap>
        </map>
    </authorizationPlugin>I

As you can see, I set the username and password at factory level, although I can set them when I create the connection.

Which option is best, to set the username&password to the factory or when I create the connection?

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("tcp://localhost:61616");
        factory.setUserName(username);
        factory.setPassword(password);

vs

connection = factory.createConnection(username, password);

Anyway, with security I can't to connect to the ActiveMQ server:

javax.jms.JMSException: Could not connect to broker URL: tcp://localhost:61616. Reason: java.net.ConnectException: Connection refused: connect

The files in \apache-activemq-5.14.4\conf directory have this configurations: users.properties: admin=admin, groups.properties: admins=admin and credentials.properties:

activemq.username=admin
activemq.password=anything

If I comment the simpleAuthenticationPlugin and authorizationPlugin, then I can connect to the server, send the messages I want to sent and consume them. Also, I can connect to the server,send and consume the messages if I remove anonymousAccessAllowed="false" or put it on true, but this is not what I want.

Could you please tell me how can I connect to the ActiveMQ server configured with that security level by setting the username and password programmatically?

Answer

Nelly Junior picture Nelly Junior · Mar 27, 2017

SOLVED!

The problem was that I didn't put my plugins into the <plugin></plugin> element. Now, the configuration is:

<plugins>
    <simpleAuthenticationPlugin anonymousAccessAllowed="false">
        <users>
            <authenticationUser username="admin" password="anything" groups="admins, producers, consumers"/>
        </users>
    </simpleAuthenticationPlugin>


    <authorizationPlugin>
        <map>
            <authorizationMap>
                <authorizationEntries>
                    <authorizationEntry queue="myQueue" read="consumers" write="producers" admin="producers,consumers,admins" />    
                    <authorizationEntry topic="ActiveMQ.Advisory.>" read="consumers" write="producers" admin="producers,consumers,admins"/>
                </authorizationEntries>
            </authorizationMap>
        </map>
    </authorizationPlugin>
</plugins>

And to connect I use:

ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(username,password,"tcp://localhost:61616");