java 为 ActiveMQ JMS 连接使用用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28451329/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use username and password for ActiveMQ JMS Connection
提问by Muhammad Imran Tariq
Apache ActiveMQ creates a secure connection using username and password.
Apache ActiveMQ 使用用户名和密码创建安全连接。
InitialContext initCtx = new InitialContext();
javax.jms.ConnectionFactory qcf = (javax.jms.ConnectionFactory) initCtx.lookup(factoryName);
Connection connection = qcf.createConnection(userName, password);
Where can I found these credentials. Are these username and password configured in any ActiveMQ configuration file?
我在哪里可以找到这些凭据。这些用户名和密码是否在任何 ActiveMQ 配置文件中配置?
回答by hschne
To answer your question: Indeed they are, and the name of the file where the credentials are defined is activemq.xml
. It can be found in the conf
directory of your ActiveMQ Installation, e.g. C:\Program Files (x86)\apache-activemq-5.10.0\conf
.
回答您的问题:确实是这样,并且定义凭据的文件的名称是activemq.xml
. 它可以在conf
您的 ActiveMQ 安装目录中找到,例如C:\Program Files (x86)\apache-activemq-5.10.0\conf
.
Now, on this sitethere are rather detailed instructions on how to configure ActiveMQ to use simple authentication or JAAS, but I'll give you a quick rundown and some tips:
现在,在这个站点上有关于如何配置 ActiveMQ 以使用简单身份验证或 JAAS 的相当详细的说明,但我会给你一个快速的纲要和一些提示:
All the following stuff has to be inserted in the
plugins
section of aforementioned XML file.Use
SimpleAuthentication
in order to just "add" users to groups, e.g.<simpleAuthenticationPlugin anonymousAccessAllowed="true"> <users> <authenticationUser username="system" password="system" groups="users,admins"/> <authenticationUser username="admin" password="admin" groups="users,admins"/> <authenticationUser username="user" password="user" groups="users"/> <authenticationUser username="guest" password="guest" groups="guests"/> </users> </simpleAuthenticationPlugin>
Use
AuthorizationPlugin
to configure which groups have access to which queues and topics.If you plan on using
SimpleAuthentication
make sure you do not have<jaasAuthenticationPlugin configuration="activemq-domain" />
in your active plugins. Just in case you plan on copying that one sample from the page I previously mentioned.You might want to enable anonymous access. To do so, add the corresponding attribute to your SimpleAuthenticatoinPlugin node. Once that is done you may connect to queues without providing username and password when creating a connection.
以下所有内容都必须插入到
plugins
上述 XML 文件的部分中。用于
SimpleAuthentication
将用户“添加”到组中,例如<simpleAuthenticationPlugin anonymousAccessAllowed="true"> <users> <authenticationUser username="system" password="system" groups="users,admins"/> <authenticationUser username="admin" password="admin" groups="users,admins"/> <authenticationUser username="user" password="user" groups="users"/> <authenticationUser username="guest" password="guest" groups="guests"/> </users> </simpleAuthenticationPlugin>
使用
AuthorizationPlugin
配置哪些组可以访问哪些队列和主题。如果您打算使用,请
SimpleAuthentication
确保您<jaasAuthenticationPlugin configuration="activemq-domain" />
的活动插件中没有。以防万一您打算从我之前提到的页面中复制该示例。您可能希望启用匿名访问。为此,请将相应的属性添加到您的 SimpleAuthenticatoinPlugin 节点。完成后,您可以在创建连接时无需提供用户名和密码即可连接到队列。
回答by Alon Eirew
Did you try connecting without supplying userName & password, by default you should be able to do that.
您是否尝试在不提供用户名和密码的情况下进行连接,默认情况下您应该能够做到这一点。
ConnectionFactory connectionFactoryProd = new ActiveMQConnectionFactory("failover://tcp://yourServerWhereActiveMqIs:61616");
Connection connectionProd = connectionFactoryProd.createConnection();
connectionProd.start();