我创建一个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
and 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");