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

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

我创建了一个 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();
        }
...

我使用 SimpleAuthentificationPlugin 在 broker 元素中添加了一个安全级别:

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

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 服务器吗?

已解决!

问题是我没有将我的插件放入 <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");