如何通过以编程方式设置用户名和密码来连接到ActiveMQ服务器?

时间:2017-03-24 17:00:52

标签: java apache authentication activemq broker

我创建一个ActiveMQ服务器以发送一些消息。我使用Apache Activemq版本5.14.4 当我尝试连接到ActiveMQ服务器时,我想要询问用户名和密码,这就是为什么我在activemq.xml中添加了一个简单的身份验证插件,如下所示。

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();
        }
...

在broker元素中,我使用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

正如您所看到的,我在工厂级别设置了用户名和密码,但我可以在创建连接时设置它们。

哪个选项最好,将用户名和密码设置为工厂或创建连接时?

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

VS

connection = factory.createConnection(username, password);

无论如何,有了安全性,我无法连接到ActiveMQ服务器:

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

\ apache-activemq-5.14.4 \ conf目录中的文件具有以下配置: users.properties:admin=admin,groups.properties:admins=admin和credentials.properties:

activemq.username=admin
activemq.password=anything

如果我评论simpleAuthenticationPlugin和authorizationPlugin,那么我可以连接到服务器,发送我想要发送的消息并使用它们。此外,如果我删除anonymousAccessAllowed="false"或将其设置为true,我可以连接到服务器,发送和使用消息,但这不是我想要的。

您能告诉我如何通过以编程方式设置用户名和密码来连接到配置了该安全级别的ActiveMQ服务器?

1 个答案:

答案 0 :(得分:2)

解决!

问题在于我没有将我的插件插入<plugin></plugin>元素。 现在,配置是:

<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>

连接我使用:

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