[jboss-cvs] JBoss Messaging SVN: r6598 - in trunk/examples/jms/security: server0 and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Apr 28 09:47:57 EDT 2009


Author: gaohoward
Date: 2009-04-28 09:47:57 -0400 (Tue, 28 Apr 2009)
New Revision: 6598

Modified:
   trunk/examples/jms/security/readme.html
   trunk/examples/jms/security/server0/jbm-jms.xml
   trunk/examples/jms/security/server0/jbm-queues.xml
   trunk/examples/jms/security/server0/jbm-users.xml
   trunk/examples/jms/security/src/org/jboss/jms/example/SecurityExample.java
Log:
temporary commit of security example


Modified: trunk/examples/jms/security/readme.html
===================================================================
--- trunk/examples/jms/security/readme.html	2009-04-28 13:19:18 UTC (rev 6597)
+++ trunk/examples/jms/security/readme.html	2009-04-28 13:47:57 UTC (rev 6598)
@@ -6,62 +6,107 @@
   <body>
      <h1>JMS Security Example</h1>
      <br>
-     <p>This example shows you how configure security with JBoss Messaging.</p>
+     <p>This example shows you how configure and use security with JBoss Messaging.</p>
      
      <p>With security properly configured, JBoss Messaging can restrict client access to its resouces, including 
      connection creation, message sending/receiving, etc. This is done by configuring users and roles as well as permissions in 
      the configuration files. </p>
+
+     <p>JBoss Messaging supports wild-card in security configuration. This feature makes security configuration very much 
+     flexible and it enables fine-grained control over permissions in an efficient way.</p>
+     
      <p>For a full description of how to configure security with JBoss Messaging, please consult the user
      manual.</p>
-     <p>In this example, two users jbm-sender and jbm-consumer are configured. User jbm-sender belongs to user role and sender role. User
-     jbm-consumer belongs to user role and consumer role. They are configured in server0/jbm-users.xml, as below: </p>
      
+     <p>This example demonstrates how to configure users/roles, how to configure topics with proper permissions using wild-card
+     expressions, and how they take effects in a simple program. </p>
+     
+     <p>First we need to configure users with roles. Users and Roles are configured in <code>jbm-users.xml</code>. This example has four users
+     configured as below </p>
+     
      <pre>
      <code>
-        &lt;user name=&quot;jbm-sender&quot; password=&quot;jbossmessaging1&quot;&gt;
-        &lt;role name=&quot;user&quot;/&gt;
-        &lt;role name=&quot;sender&quot;/&gt;
+        &lt;user name=&quot;bill&quot; password=&quot;jbossmessaging&quot;&gt;
+           &lt;role name=&quot;user&quot;/&gt;
         &lt;/user&gt;
+  
+        &lt;user name=&quot;andrew&quot; password=&quot;jbossmessaging1&quot;&gt;
+           &lt;role name=&quot;europe-user&quot;/&gt;
+        &lt;/user&gt;
    
-        &lt;user name=&quot;jbm-consumer&quot; password=&quot;jbossmessaging2&quot;&gt;
-        &lt;role name=&quot;user&quot;/&gt;
-        &lt;role name=&quot;consumer&quot;/&gt;
+        &lt;user name=&quot;frank&quot; password=&quot;jbossmessaging2&quot;&gt;
+           &lt;role name=&quot;us-user&quot;/&gt;
+           &lt;role name=&quot;news-user&quot;/&gt;
         &lt;/user&gt;
+   
+        &lt;user name=&quot;sam&quot; password=&quot;jbossmessaging3&quot;&gt;
+           &lt;role name=&quot;news-user&quot;/&gt;
+        &lt;/user&gt;
      </code>
      </pre>
+     
      <p>
-     The above configuration makes sure that only 'jbm-sender' and 'jbm-consumer' with correct passwords can create connections to JBoss 
-     Messaging server. In another file server0/jbm-queues.xml, permissions are configured in order to give proper rights to the users to 
-     do the job: 
+     Each user has three properties available: user name, password, and roles it belongs to. It should be noticed that
+     a user can belong to more than one roles. In the above configuration, user 'bill' belongs to role 'user', user 'andrew'
+     belongs to role 'europe-user', user 'frank' belongs to 'us-user' and 'news-user', and user 'sam' belongs to 'news-user'.
      </p>
+     <p>
+     User name and password consists of a valid account that can be used to establish connections to a JBoss Messaging server, while 
+     roles are used in controling the access privileges against JBoss Messaging topics and queues. You can achieve this control by
+     configuring proper permissions in <code>jbm-queues.xml</code>, like in the following
+     </p>
+     <pre><code>
+	   &lt;!-- any user can have full control of generic topics --&gt;
+	   &lt;security match=&quot;jms.topic.#&quot;&gt;
+	      &lt;permission type=&quot;createDurableQueue&quot; roles=&quot;#&quot;/&gt;
+	      &lt;permission type=&quot;deleteDurableQueue&quot; roles=&quot;#&quot;/&gt;
+	      &lt;permission type=&quot;createTempQueue&quot; roles=&quot;#&quot;/&gt;
+	      &lt;permission type=&quot;deleteTempQueue&quot; roles=&quot;#&quot;/&gt;
+	      &lt;permission type=&quot;consume&quot; roles=&quot;#&quot;/&gt;
+	      &lt;permission type=&quot;send&quot; roles=&quot;#&quot;/&gt;
+	   &lt;/security&gt;
+	
+	   &lt;!-- only news-user can subscribe to news topic --&gt;
+	   &lt;security match=&quot;jms.topic.news.#&quot;&gt;
+	      &lt;permission type=&quot;consume&quot; roles=&quot;news-user&quot;/&gt;
+	   &lt;/security&gt;
+	
+	   &lt;!-- only europe-user can create/delete any news.europe topics and pulish news to it. --&gt;
+	   &lt;security match=&quot;jms.topic.news.europe.#&quot;&gt;
+	      &lt;permission type=&quot;createDurableQueue&quot; roles=&quot;europe-user&quot;/&gt;
+	      &lt;permission type=&quot;deleteDurableQueue&quot; roles=&quot;europe-user&quot;/&gt;
+	      &lt;permission type=&quot;createTempQueue&quot; roles=&quot;europe-user&quot;/&gt;
+	      &lt;permission type=&quot;deleteTempQueue&quot; roles=&quot;europe-user&quot;/&gt;
+	      &lt;permission type=&quot;send&quot; roles=&quot;europe-user&quot;/&gt;
+	   &lt;/security&gt;
+	
+	   &lt;!-- only us-user can create/delete any news.us topics and pulish news to it. --&gt;
+	   &lt;security match=&quot;jms.topic.news.us.#&quot;&gt;
+	      &lt;permission type=&quot;createDurableQueue&quot; roles=&quot;us-user&quot;/&gt;
+	      &lt;permission type=&quot;deleteDurableQueue&quot; roles=&quot;us-user&quot;/&gt;
+	      &lt;permission type=&quot;createTempQueue&quot; roles=&quot;us-user&quot;/&gt;
+	      &lt;permission type=&quot;deleteTempQueue&quot; roles=&quot;us-user&quot;/&gt;
+	      &lt;permission type=&quot;send&quot; roles=&quot;us-user&quot;/&gt;
+	   &lt;/security&gt;
+     </code></pre>
      
-     <pre>
-     <code>
-      &lt;security match=&quot;jms.#&quot;&gt;
-         &lt;permission type=&quot;createDurableQueue&quot; roles=&quot;user&quot;/&gt;
-         &lt;permission type=&quot;deleteDurableQueue&quot; roles=&quot;user&quot;/&gt;
-         &lt;permission type=&quot;createTempQueue&quot; roles=&quot;user&quot;/&gt;
-         &lt;permission type=&quot;deleteTempQueue&quot; roles=&quot;user&quot;/&gt;
-         &lt;permission type=&quot;send&quot; roles=&quot;sender&quot;/&gt;
-         &lt;permission type=&quot;consume&quot; roles=&quot;consumer&quot;/&gt;
-      &lt;/security&gt;
-     </code>
-     </pre>
+     <p>Permissions can be defined on any group of queues, by using a wildcard. You can easily specify 
+     wildcards to apply certain permissions to a set of matching queues and topics. In the above configuration
+     we have created four sets of permissions, each set matches against a special group of targets, indicated by wild-card match attributes.</p>
      
-     <p>Permissions can be defined on any group of queues, by using a wildcard. In the above example
-     we use the wildcard expression <code>match="jms.#"</code> to apply the permissions to all JMS
-     queues and topics. If you wanted different permissions for different queues and topics depending on their
-     name, you can easily create matching wildcards to do this. For more information on how to configure
-     security wild-cards please see the user manual.</p>
+     <p>You can provide a very loose permission control for a very general group of destinations. Then you add more strict control
+     over specific topics. By the above we define the following access rules:</p>
      
-     <p>As you can see, both users can access queue resources (role user). However, user jbm-sender (of role sender) can only send messages 
-     and user jbm-consumer (of role consumer) can only consume messages. In this example the jbm-consumer tries to send message but failed 
-     as it doesn't has the right to do so.</p>
+         <li>Only role 'us-user' can create/delete and pulish messages to topics whose names match wild-card pattern 'news.us.#'.</li>
+         <li>Only role 'europe-user' can create/delete and publish messages to topics whose names match wild-card pattern 'news.europe'.</li>
+         <li>Only role 'news-user' can subscribe messages to topics whose names match wild-card pattern 'news.#'. These enables users of 'news-user' can subscribe both news.us and news.europe topics.</li>
+         <li>For any other topics that don't match any of the above wild-card patterns, all permissions are granted to any users.</li>
+         
+     <p>To illustrate the effect of permissions, three topics are deployed. Topic 'genericTopic' matches 'jms.topic.#' wild-card, topic 'news.europe.europeTopic' matches 'jms.topic.news.#' and
+     jms.topic.news.europe.#' wild-cards, and topic 'news.us.usTopic' matches 'jms.topic.news.#' as well as 'jms.topic.news.us.#'.</p>
      
-     <p>With JBoss Messaging, the security manager is configurable. You can use JAASSecurityManager or JBossASSecurityManager based on you need. Please
-     check out the jbm-standalone-beans.xml for how to do.</p>
-     
-     <p>In this example we just use the basic JBMSecurityManagerImpl which reads users/roles/passwords from the xml
+     <p>With JBoss Messaging, the security manager is also configurable. You can use JAASSecurityManager or JBossASSecurityManager based on you need. Please
+     check out the jbm-standalone-beans.xml for how to do. In this example we just use the basic JBMSecurityManagerImpl which reads users/roles/passwords from the xml
      file <code>jbm-users.xml</code>.
 
      <br>

Modified: trunk/examples/jms/security/server0/jbm-jms.xml
===================================================================
--- trunk/examples/jms/security/server0/jbm-jms.xml	2009-04-28 13:19:18 UTC (rev 6597)
+++ trunk/examples/jms/security/server0/jbm-jms.xml	2009-04-28 13:47:57 UTC (rev 6598)
@@ -12,9 +12,16 @@
       </entries>
    </connection-factory>
 
-   <!--the topic used by the example-->
-   <topic name="exampleTopic">
-      <entry name="/topic/exampleTopic"/>
+   <topic name="genericTopic">
+      <entry name="/topic/genericTopic"/>
    </topic>
 
-</deployment>
\ No newline at end of file
+   <topic name="news.europe.europeTopic">
+      <entry name="/topic/europeTopic"/>
+   </topic>
+
+   <topic name="news.us.usTopic">
+      <entry name="/topic/usTopic"/>
+   </topic>
+
+</deployment>

Modified: trunk/examples/jms/security/server0/jbm-queues.xml
===================================================================
--- trunk/examples/jms/security/server0/jbm-queues.xml	2009-04-28 13:19:18 UTC (rev 6597)
+++ trunk/examples/jms/security/server0/jbm-queues.xml	2009-04-28 13:47:57 UTC (rev 6598)
@@ -2,13 +2,34 @@
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-queues.xsd ">
 
-   <security match="jms.#">
+   <!-- any user can have full control of generic topics -->
+   <security match="jms.topic.#">
       <permission type="createDurableQueue" roles="user"/>
       <permission type="deleteDurableQueue" roles="user"/>
       <permission type="createTempQueue" roles="user"/>
       <permission type="deleteTempQueue" roles="user"/>
-      <permission type="send" roles="sender"/>
-      <permission type="consume" roles="consumer"/>
+      <permission type="send" roles="user"/>
+      <permission type="consume" roles="user"/>
    </security>
 
+   <!-- only europe-user can create/delete any news.europe topics and pulish news to it. -->
+   <security match="jms.topic.news.europe.#">
+      <permission type="createDurableQueue" roles="user"/>
+      <permission type="deleteDurableQueue" roles="user"/>
+      <permission type="createTempQueue" roles="user"/>
+      <permission type="deleteTempQueue" roles="user"/>
+      <permission type="send" roles="europe-user"/>
+      <permission type="consume" roles="news-user"/>
+   </security>
+
+   <!-- only us-user can create/delete any news.europe topics and pulish news to it. -->
+   <security match="jms.topic.news.us.#">
+      <permission type="createDurableQueue" roles="user"/>
+      <permission type="deleteDurableQueue" roles="user"/>
+      <permission type="createTempQueue" roles="user"/>
+      <permission type="deleteTempQueue" roles="user"/>
+      <permission type="send" roles="us-user"/>
+      <permission type="consume" roles="news-user"/>
+   </security>
+
 </settings>

Modified: trunk/examples/jms/security/server0/jbm-users.xml
===================================================================
--- trunk/examples/jms/security/server0/jbm-users.xml	2009-04-28 13:19:18 UTC (rev 6597)
+++ trunk/examples/jms/security/server0/jbm-users.xml	2009-04-28 13:47:57 UTC (rev 6598)
@@ -1,13 +1,25 @@
 <deployment xmlns="urn:jboss:messaging" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="urn:jboss:messaging ../schemas/jbm-users.xsd ">
    
-   <user name="jbm-sender" password="jbossmessaging1">
+   <user name="bill" password="jbossmessaging">
       <role name="user"/>
-      <role name="sender"/>
    </user>
+  
+   <user name="andrew" password="jbossmessaging1">
+      <role name="europe-user"/>
+      <role name="user"/>
+   </user>
    
-   <user name="jbm-consumer" password="jbossmessaging2">
+   <user name="frank" password="jbossmessaging2">
+      <role name="us-user"/>
+      <role name="news-user"/>
       <role name="user"/>
-      <role name="consumer"/>
    </user>
-</deployment>
\ No newline at end of file
+   
+   <user name="sam" password="jbossmessaging3">
+      <role name="news-user"/>
+      <role name="user"/>
+   </user>
+
+</deployment>
+

Modified: trunk/examples/jms/security/src/org/jboss/jms/example/SecurityExample.java
===================================================================
--- trunk/examples/jms/security/src/org/jboss/jms/example/SecurityExample.java	2009-04-28 13:19:18 UTC (rev 6597)
+++ trunk/examples/jms/security/src/org/jboss/jms/example/SecurityExample.java	2009-04-28 13:47:57 UTC (rev 6598)
@@ -23,6 +23,7 @@
 
 import javax.jms.Connection;
 import javax.jms.ConnectionFactory;
+import javax.jms.JMSException;
 import javax.jms.JMSSecurityException;
 import javax.jms.MessageConsumer;
 import javax.jms.MessageProducer;
@@ -47,16 +48,21 @@
 
    public boolean runExample() throws Exception
    {
-      Connection connection1 = null;
-      Connection connection2 = null;
+      Connection billConnection = null;
+      Connection andrewConnection = null;
+      Connection frankConnection = null;
+      Connection samConnection = null;
+      
       InitialContext initialContext = null;
       try
       {
          ///Step 1. Create an initial context to perform the JNDI lookup.
          initialContext = getContext(0);
 
-         //Step 2. perform a lookup on the topic
-         Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");
+         //Step 2. perform lookup on the topics
+         Topic genericTopic = (Topic) initialContext.lookup("/topic/genericTopic");
+         Topic europeTopic = (Topic) initialContext.lookup("/topic/europeTopic");
+         Topic usTopic = (Topic) initialContext.lookup("/topic/usTopic");
 
          //Step 3. perform a lookup on the Connection Factory
          ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
@@ -64,90 +70,87 @@
          //Step 4. Try to create a JMS Connection without user/password. It will fail.
          try
          {
-            connection1 = cf.createConnection();
+            Connection connection = cf.createConnection();
             result = false;
          }
          catch (JMSSecurityException e)
          {
-            System.out.println("Error creating connection, detail: " + e.getMessage());
+            System.out.println("Default user cannot get a connection. Details: " + e.getMessage());
          }
 
-         //Step 5. Create a Connection using wrong password, it will fail.
+         //Step 5. bill tries to make a connection using wrong password
+         billConnection = null;
          try
          {
-            connection1 = cf.createConnection("jbm-sender", "wrong-password");
+            billConnection = createConnection("bill", "jbossmessaging1", cf);
             result = false;
          }
-         catch (JMSSecurityException e)
+         catch (JMSException e)
          {
-            System.out.println("Error creating connection, detail: " + e.getMessage());
+            System.out.println("Bill failed to connect. Details: " + e.getMessage());
          }
          
-         //Step 6. Now create two connections with correct credentials. connection1 is used for sending, connection2 receiving
-         connection1 = cf.createConnection("jbm-sender", "jbossmessaging1");
-         connection2 = cf.createConnection("jbm-consumer", "jbossmessaging2");
-
-         //Step 7. Create 2 JMS Sessions
-         Session session1 = connection1.createSession(false, Session.AUTO_ACKNOWLEDGE);
-         Session session2 = connection2.createSession(false, Session.AUTO_ACKNOWLEDGE);
-
-         //Step 8. Create 2 Message Producers, where producer2 has no right to send
-         MessageProducer producer1 = session1.createProducer(topic);
-         MessageProducer producer2 = session2.createProducer(topic);
-
-         //Step 9. Create 2 JMS Message Consumers
-         MessageConsumer messageConsumer1 = session2.createConsumer(topic);
-         MessageConsumer messageConsumer2 = session2.createConsumer(topic);
-                  
-         //Step 10. Start the Connections
-         connection1.start();
-         connection2.start();
+         //Step 6. bill makes a good connection.
+         billConnection = createConnection("bill", "jbossmessaging", cf);
+         billConnection.start();
          
-         //Step 11. Create a Text Message
-         TextMessage message = session1.createTextMessage("This is a text message");
-
-         //Step 12. Send the Message by producer2
-         producer2.send(message);
-         System.out.println("Producer2 sent message: " + message.getText());
+         //Step 7. andrew makes a good connection.
+         andrewConnection = createConnection("andrew", "jbossmessaging1", cf);
+         andrewConnection.start();
          
-         //Step 13. Check no messages are received by either consumer.
-         TextMessage messageReceived1 = (TextMessage) messageConsumer1.receive(2000);
-         TextMessage messageReceived2 = (TextMessage) messageConsumer2.receive(2000);
-         if (messageReceived1 != null) 
-         {
-            System.out.println("Message received! " + messageReceived1.getText());
-            result = false;
-         }
-         if (messageReceived2 != null) 
-         {
-            System.out.println("Message received! " + messageReceived2.getText());
-            result = false;
-         }
+         //Step 8. frank makes a good connection.
+         frankConnection = createConnection("frank", "jbossmessaging2", cf);
+         frankConnection.start();
          
-         //Step 14. Send the message by producer1
-         producer1.send(message);
+         //Step 9. sam makes a good connection.
+         samConnection = createConnection("sam", "jbossmessaging3", cf);
+         samConnection.start();
+         
+         //Step 10. Check every user can publish/subscribe genericTopics.
+         System.out.println("------------------------Checking permissions on " + genericTopic + "----------------");
+         checkUserSendAndReceive(genericTopic, billConnection, "bill");
+         checkUserSendAndReceive(genericTopic, andrewConnection, "andrew");
+         checkUserSendAndReceive(genericTopic, frankConnection, "frank");
+         checkUserSendAndReceive(genericTopic, samConnection, "sam");
+         System.out.println("-------------------------------------------------------------------------------------");
+         
+         //Step 11. Check permissions on europeTopic
+         System.out.println("------------------------Checking permissions on " + europeTopic + "----------------");
+         checkUserNoSendNoReceive(europeTopic, billConnection, "bill", andrewConnection, frankConnection);
+         checkUserSendNoReceive(europeTopic, andrewConnection, "andrew", frankConnection);
+         checkUserReceiveNoSend(europeTopic, frankConnection, "frank", andrewConnection);
+         checkUserReceiveNoSend(europeTopic, samConnection, "sam", andrewConnection);
+         System.out.println("-------------------------------------------------------------------------------------");
+         
+         //Step 12. Check permissions on usTopic
+         System.out.println("------------------------Checking permissions on " + usTopic + "----------------");
+         checkUserNoSendNoReceive(usTopic, billConnection, "bill", frankConnection, frankConnection);
+         checkUserNoSendNoReceive(usTopic, andrewConnection, "andrew", frankConnection, frankConnection);
+         checkUserSendAndReceive(usTopic, frankConnection, "frank");
+         checkUserReceiveNoSend(usTopic, samConnection, "sam", frankConnection);
+         System.out.println("-------------------------------------------------------------------------------------");
 
-         System.out.println("Producer1 sent message: " + message.getText());
-
-         //Step 15. Receive the message
-         messageReceived1 = (TextMessage) messageConsumer1.receive(1000);
-         messageReceived2 = (TextMessage) messageConsumer2.receive(1000);
-         System.out.println("Consumer 1 Received message: " + messageReceived1.getText());
-         System.out.println("Consumer 2 Received message: " + messageReceived2.getText());
-         
          return result;
       }
       finally
       {
          //Step 16. Be sure to close our JMS resources!
-         if (connection1 != null)
+         if (billConnection != null)
          {
-            connection1.close();
+            billConnection.close();
          }
-         if (connection2 != null)
+         if (andrewConnection != null)
          {
-            connection2.close();
+            andrewConnection.close();
          }
+         if (frankConnection != null)
+         {
+            frankConnection.close();
+         }
+         if (samConnection != null)
+         {
+            samConnection.close();
+         }
          
          // Also the initialContext
          if (initialContext != null)
@@ -156,4 +159,177 @@
          }
       }
    }
+
+
+   //Check the user can receive message but cannot send message.
+   private void checkUserReceiveNoSend(Topic topic, Connection connection, String user, Connection sendingConn) throws JMSException
+   {
+      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      MessageProducer producer = session.createProducer(topic);
+      MessageConsumer consumer = session.createConsumer(topic);
+      TextMessage msg = session.createTextMessage("hello-world-1");
+      producer.send(msg);
+      TextMessage receivedMsg = (TextMessage)consumer.receive(2000);
+      if (receivedMsg == null)
+      {
+         System.out.println("User " + user + " cannot send message [" + msg.getText() + "] to topic " + topic);
+      }
+      else
+      {
+         System.out.println("Security setting is broken! User " + user + " can send message [" + receivedMsg.getText() + "] to topic " + topic);
+         result = false;
+      }
+
+      //Now send a good message
+      Session session1 = sendingConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      producer = session1.createProducer(topic);
+      producer.send(msg);
+      
+      receivedMsg = (TextMessage)consumer.receive(2000);
+      
+      if (receivedMsg != null)
+      {
+         System.out.println("User " + user + " can receive message [" + receivedMsg.getText() + "] from topic " + topic);
+      }
+      else
+      {
+         System.out.println("Security setting is broken! User " + user + " cannot receive message from topic " + topic);
+         result = false;         
+      }
+      session.close();
+   }
+
+   //Check the user can send message but cannot receive message
+   private void checkUserSendNoReceive(Topic topic, Connection connection, String user, Connection receivingConn) throws JMSException
+   {
+      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      MessageProducer producer = session.createProducer(topic);
+      MessageConsumer consumer = null;
+      try
+      {
+         consumer = session.createConsumer(topic);
+      }
+      catch (JMSException e)
+      {
+         System.out.println("User " + user + " cannot receive any message from topic " + topic);
+      }
+
+      Session session1 = receivingConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      MessageConsumer goodConsumer = session1.createConsumer(topic);
+      
+      TextMessage msg = session.createTextMessage("hello-world-2");
+      producer.send(msg);
+      
+      TextMessage receivedMsg = (TextMessage)goodConsumer.receive(2000);
+      if (receivedMsg != null)
+      {
+         System.out.println("User " + user + " can send message [" + receivedMsg.getText() + "] to topic " + topic);
+      }
+      else
+      {
+         System.out.println("Security setting is broken! User " + user + " cannot send message [" + msg.getText() + "] to topic " + topic);
+         result = false;
+      }
+      
+      if (consumer != null)
+      {
+         receivedMsg = (TextMessage)consumer.receive(2000);
+         if (receivedMsg == null)
+         {
+            System.out.println("User " + user + " cannot receive any message from topic " + topic);
+         }
+         else
+         {
+            System.out.println("Security setting is broken! User " + user + " can receive message [" + receivedMsg.getText() + "]");
+            result = false;
+         }
+      }
+      
+      session.close();
+      session1.close();
+   }
+
+   //Check the user has neither send nor receive permission on topic
+   private void checkUserNoSendNoReceive(Topic topic, Connection connection, String user, Connection sendingConn, Connection receivingConn) throws JMSException
+   {
+      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      MessageProducer producer = session.createProducer(topic);
+      MessageConsumer consumer = null;
+      
+      try
+      {
+         consumer = session.createConsumer(topic);
+      }
+      catch (JMSException e)
+      {
+         System.out.println("User " + user + " cannot create consumer on topic " + topic);
+      }
+      
+      Session session1 = receivingConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      MessageConsumer goodConsumer = session1.createConsumer(topic);
+      
+      TextMessage msg = session.createTextMessage("hello-world-3");
+      producer.send(msg);
+
+      TextMessage receivedMsg = (TextMessage)goodConsumer.receive(2000);
+      
+      if (receivedMsg == null)
+      {
+         System.out.println("User " + user + " cannot send message [" + msg.getText() + "] to topic: " + topic);
+      }
+      else
+      {
+         System.out.println("Security setting is broken! User " + user + " can send message [" + msg.getText() + "] to topic " + topic);
+         result = false;
+      }
+      
+      if (consumer != null)
+      {
+         Session session2 = sendingConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
+         MessageProducer goodProducer = session2.createProducer(topic);
+         goodProducer.send(msg);
+      
+         receivedMsg = (TextMessage)consumer.receive(2000);
+      
+         if (receivedMsg == null)
+         {
+            System.out.println("User " + user + " cannot receive message [" + msg.getText() + "] from topic " + topic);
+         }
+         else
+         {
+            System.out.println("Security setting is broken! User " + user + " can receive message [" + receivedMsg.getText() + "] from topic " + topic);
+         }
+         session2.close();
+      }
+      
+      session.close();
+      session1.close();
+   }
+
+   //Check the user connection has both send and receive permissions on the topic
+   private void checkUserSendAndReceive(Topic topic, Connection connection, String user) throws JMSException
+   {
+      Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+      TextMessage msg = session.createTextMessage("hello-world-4");
+      MessageProducer producer = session.createProducer(topic);
+      MessageConsumer consumer = session.createConsumer(topic);
+      producer.send(msg);
+      TextMessage receivedMsg = (TextMessage)consumer.receive(5000);
+      if (receivedMsg != null)
+      {
+         System.out.println("User " + user + " can send message: [" + msg.getText() + "] to topic: " + topic);
+         System.out.println("User " + user + " can receive message: [" + msg.getText() + "] from topic: " + topic);
+      }
+      else
+      {
+         System.out.println("Error! User " + user + " cannot receive the message! ");
+         result = false;
+      }
+      session.close();
+   }
+
+   private Connection createConnection(String username, String password, ConnectionFactory cf) throws JMSException
+   {
+      return cf.createConnection(username, password);
+   }
 }




More information about the jboss-cvs-commits mailing list