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

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

在代理元素中,我使用 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);

对比

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

关于java - 如何通过以编程方式设置用户名和密码来连接到 ActiveMQ 服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43005226/

相关文章:

java - 如何远程连接到本地主机上运行的 WebSphere 服务器?

java - Lombok:非子类不应该访问 protected setter 吗?

php - 谁能确认 phpMyAdmin AllowNoPassword 适用于 MySQL 数据库?

php - 如何使用 .htaccess 单独留下 index.html 实现站点范围的 SSL?

javascript - Apache : CORS header 'Access-Control-Allow-Origin' missing

api - Laravel 5.3 RESTFul API 无需身份验证

java - 从字符串中检索整数

Java向JOptionPane添加组件

windows - 如何保护 Windows Azure 上运行的 odata 服务的安全

wcf - 将 WCF 自定义身份验证 session 传输到服务