diff --git a/activemq-client/src/main/java/org/apache/activemq/filter/PropertyExpression.java b/activemq-client/src/main/java/org/apache/activemq/filter/PropertyExpression.java index 562d5013b5..5c5c01ca2e 100755 --- a/activemq-client/src/main/java/org/apache/activemq/filter/PropertyExpression.java +++ b/activemq-client/src/main/java/org/apache/activemq/filter/PropertyExpression.java @@ -138,6 +138,13 @@ public class PropertyExpression implements Expression { return message.getGroupID(); } }); + JMS_PROPERTY_EXPRESSIONS.put("JMSXUserID", new SubExpression() { + + @Override + public Object evaluate(Message message) { + return message.getUserID(); + } + }); JMS_PROPERTY_EXPRESSIONS.put("JMSXGroupSeq", new SubExpression() { @Override diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/test/JmsTopicSendReceiveWithEmbeddedBrokerAndUserIDTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/test/JmsTopicSendReceiveWithEmbeddedBrokerAndUserIDTest.java index 3a1e9bf9f4..aac150f023 100644 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/test/JmsTopicSendReceiveWithEmbeddedBrokerAndUserIDTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/test/JmsTopicSendReceiveWithEmbeddedBrokerAndUserIDTest.java @@ -16,7 +16,6 @@ */ package org.apache.activemq.test; -import java.util.Iterator; import java.util.List; import javax.jms.JMSException; @@ -24,39 +23,54 @@ import javax.jms.Message; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.broker.BrokerService; -import org.apache.activemq.security.SimpleSecurityBrokerSystemTest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * + * */ public class JmsTopicSendReceiveWithEmbeddedBrokerAndUserIDTest extends JmsTopicSendReceiveWithTwoConnectionsAndEmbeddedBrokerTest { private static final Logger LOG = LoggerFactory.getLogger(JmsTopicSendReceiveWithEmbeddedBrokerAndUserIDTest.class); protected String userName = "James"; + @Override protected ActiveMQConnectionFactory createConnectionFactory() throws Exception { ActiveMQConnectionFactory answer = super.createConnectionFactory(); answer.setUserName(userName); return answer; } + @Override protected void configureBroker(BrokerService answer) throws Exception { answer.setPopulateJMSXUserID(true); super.configureBroker(answer); } - protected void assertMessagesReceivedAreValid(List receivedMessages) throws JMSException { + @Override + protected void assertMessagesReceivedAreValid(List receivedMessages) throws JMSException { super.assertMessagesReceivedAreValid(receivedMessages); // lets assert that the user ID is set - for (Iterator iter = receivedMessages.iterator(); iter.hasNext();) { - Message message = (Message)iter.next(); + for (Message message : receivedMessages) { String userID = message.getStringProperty("JMSXUserID"); - LOG.info("Received message with userID: " + userID); + assertEquals("JMSXUserID header", userName, userID); + } + } + protected void assertMessagesAreReceived2() throws JMSException { + waitForMessagesToBeDelivered(); + assertMessagesReceivedAreValid2(messages); + } + + protected void assertMessagesReceivedAreValid2(List receivedMessages) throws JMSException { + super.assertMessagesReceivedAreValid(receivedMessages); + + // lets assert that the user ID is set + for (Message message : receivedMessages) { + String userID = (String) message.getObjectProperty("JMSXUserID"); + LOG.info("Received message with userID: " + userID); assertEquals("JMSXUserID header", userName, userID); } } @@ -77,4 +91,21 @@ public class JmsTopicSendReceiveWithEmbeddedBrokerAndUserIDTest extends JmsTopic assertMessagesAreReceived(); LOG.info("" + data.length + " messages(s) received, closing down connections"); } + + public void testSpoofedJMSXUserIdIsIgnoredAsObjectProperty() throws Exception { + Thread.sleep(1000); + messages.clear(); + + for (int i = 0; i < data.length; i++) { + Message message = createMessage(i); + configureMessage(message); + message.setStringProperty("JMSXUserID", "spoofedId"); + if (verbose) { + LOG.info("About to send a message: " + message + " with text: " + data[i]); + } + sendMessage(i, message); + } + assertMessagesAreReceived2(); + LOG.info("" + data.length + " messages(s) received, closing down connections"); + } }