环境:
Jboss 7.1.0 操作系统Windows
我正在尝试一个简单的测试,使用Jboss和内置的HornetQ JMS提供程序来试用JMS。经过大量的游戏,我设法得到了这种配置的响应
final Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
env.put(Context.PROVIDER_URL, "remote://localhost:4447");
env.put(Context.SECURITY_PRINCIPAL, "appuser2");
env.put(Context.SECURITY_CREDENTIALS, "s3cr3t");
问题是,当我运行它时,我收到以下错误:
javax.jms.JMSSecurityException: Unable to validate user: null
at org.hornetq.core.protocol.core.impl.ChannelImpl.sendBlocking(ChannelImpl.java:286)
at org.hornetq.core.client.impl.ClientSessionFactoryImpl.createSessionInternal(ClientSessionFactoryImpl.java:695)
at org.hornetq.core.client.impl.ClientSessionFactoryImpl.createSession(ClientSessionFactoryImpl.java:264)
at org.hornetq.jms.client.HornetQConnection.authorize(HornetQConnection.java:589)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnectionInternal(HornetQConnectionFactory.java:694)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:121)
at org.hornetq.jms.client.HornetQConnectionFactory.createConnection(HornetQConnectionFactory.java:116)
at com.jms.client.ConsoleClient.runExample(ConsoleClient.java:51)
at com.jms.client.ConsoleClient.main(ConsoleClient.java:20)
Caused by: HornetQException[errorCode=105 message=Unable to validate user: null]
... 9 more
我一直在寻找谷歌,每个例子似乎都指向如何配置HornetQ作为独立服务器的安全设置。我无法弄清楚如何在Jboss上配置用户以及我是否需要。
有什么想法吗?
答案 0 :(得分:15)
您似乎使用以下用户名和密码创建QueueConnection
:
QueueConnection qcon = qconFactory.createQueueConnection("appuser2","s3cr3t");
如果您不这样做,您将收到此错误
无法验证用户:null。
如果您不想使用用户名和密码,可以使用值false
设置安全性,如下所示:
<subsystem xmlns="urn:jboss:domain:messaging:1.1">
<hornetq-server>
<security-enabled>false</security-enabled>
......
</hornetq-server>
</subsystem>
然后您可以创建一个没有用户名和密码的QueueConnection
,如下所示:
QueueConnection qcon = qconFactory.createQueueConnection();
答案 1 :(得分:2)
<security-enabled>false</security-enabled>
答案 2 :(得分:1)
检查standalone-full.xml
。如果urn:jboss:domain:messaging-activemq:1.0
中的角色配置如下所示:
<security-setting name="#">
<role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
</security-setting>
然后,你必须:
guest
设置为用户); Properties props = new Properties();
props.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
props.put(Context.SECURITY_PRINCIPAL, "username");
props.put(Context.SECURITY_CREDENTIALS, "password");
InitialContext ctx = new InitialContext(props);
ConnectionFactory cf = (ConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
Queue queue = (Queue) ctx.lookup("jms/queue/queueName");
JMSContext context = cf.createContext("username", "password");
答案 3 :(得分:1)
如果有人使用 JBoss EAP 7.2 版本,那么这里是解决方案。
如果您不想通过添加标记 <security enabled="false"/>
来禁用安全性,并希望使用用户名和密码进行身份验证,请按照以下步骤操作。
经过上述步骤后,可以在application-role.properties 和application-users.properties 文件\standalone\configuration 路径中找到用户名和角色详细信息。
重启 JBoss 服务器。
假设您使用的是 JMS 2.0 规范,则创建 JMSContext,如下所示。
JMSContext jmsCtx = connectionFactory.createContext("jmsuser", "pass");
答案 4 :(得分:0)
我同意Sergiu并且可以在不需要用户名和密码的情况下添加队列。
答案 5 :(得分:0)
使用来自cli的队列添加到jboss:
jms-topic add --topic-address=testTopic -–entries=topic/test,java:jboss/exported/jms/topic/test
添加用户如下:
add-user.bat -a -u mquser -p mqpassword -g guest
消费者将代码粘贴到主方法中:
Properties properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY,"org.wildfly.naming.client.WildFlyInitialContextFactory");
properties.put(Context.PROVIDER_URL, "http-remoting://localhost:8080");
properties.put("jboss.naming.client.ejb.context", true);
properties.put("java.naming.security.principal", "mquser");
properties.put("java.naming.security.credentials", "mqpassword");
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
QueueConnection cnn = null;
QueueSender sender = null;
QueueSession session = null;
InitialContext ctx = new InitialContext(properties);
Queue queue = (Queue) ctx.lookup("jms/queue/tests");
QueueConnectionFactory factory = (QueueConnectionFactory) ctx.lookup("jms/RemoteConnectionFactory");
cnn = factory.createQueueConnection("mquser", "mqpassword");
cnn.start();
session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
TextMessage msg = session.createTextMessage("Hello World");
sender = session.createSender(queue);
sender.send(msg);
System.out.println("Message sent successfully to remote queue.");
@MessageDriven(name = "ExampleMDB", activationConfig = {
@ActivationConfigProperty(propertyName = "destination", propertyValue = "testQueues"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge")
public class ExampleMDB implements MessageListener {
/**
* Default constructor.
*/
public ExampleMDB() {
// TODO Auto-generated constructor stub
}
@Override
public void onMessage(Message arg0) {
System.out.println("----------------");
try {
System.out.println("Received message" + ((TextMessage)arg0).getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("----------------");
}
}
答案 6 :(得分:0)
如果您使用的是WildFly 10+(我使用的是WildFly 18),则必须添加以下内容来禁用安全性:
<security enabled="false"/>
在您的standalone-full.xml(wildFly目录/独立/配置)中,位于:
<subsystem xmlns="urn:jboss:domain:messaging-activemq:8.0">
和
<server name="default">
类似的东西:
<subsystem xmlns="urn:jboss:domain:messaging-activemq:8.0">
<server name="default">
<!-- Disable security with the following row, otherwise of course it will give error without authentication in java -->
<security enabled="false"/>
<statistics enabled="${wildfly.messaging-activemq.statistics-enabled:${wildfly.statistics-enabled:false}}"/>
<security-setting name="#">
<role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
</security-setting>
<address-setting name="#" dead-letter-address="jms.queue.DLQ" expiry-address="jms.queue.ExpiryQueue" max-size-bytes="10485760" page-size-bytes="2097152" message-counter-history-day-limit="10"/>
<http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/>
<http-connector name="http-connector-throughput" socket-binding="http" endpoint="http-acceptor-throughput">
<param name="batch-delay" value="50"/>
</http-connector>
<in-vm-connector name="in-vm" server-id="0">
<param name="buffer-pooling" value="false"/>
</in-vm-connector>
<http-acceptor name="http-acceptor" http-listener="default"/>
<http-acceptor name="http-acceptor-throughput" http-listener="default">
<param name="batch-delay" value="50"/>
<param name="direct-deliver" value="false"/>
</http-acceptor>
<in-vm-acceptor name="in-vm" server-id="0">
<param name="buffer-pooling" value="false"/>
</in-vm-acceptor>
<jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
<jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
<!-- Adding a new queue -->
<jms-queue name="ShippingQueue" entries="java:jboss/exported/jms/queue/ShippingQueue"/>
<connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
<connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
<pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>
</server>
</subsystem>