ActiveMQ(3) ActiveMQ创建(simpleAuthenticationPlugin)安全认证

控制端安全认证:

ActiveMQ目录conf下jetty.xml:

 1 <bean id="securityLoginService" class="org.eclipse.jetty.security.HashLoginService">
 2     <property name="name" value="ActiveMQRealm" />
 3     <property name="config" value="${activemq.conf}/jetty-realm.properties" />
 4 </bean>
 5 
 6 <bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
 7     <property name="name" value="BASIC" />
 8     <property name="roles" value="user,admin" />
 9     <!-- set authenticate=false to disable login -->
10     <property name="authenticate" value="true" />
11 </bean>
jetty.xml

<property name="authenticate" value="true" />  true: 需要认证   false: 不需要认证

ActiveMQ目录conf下jetty-realm.properties:

1 # Defines users that can access the web (console, demo, etc.)
2 # username: password [,rolename ...]
3 admin: admin, admin
4 user: user, user
jetty-realm.properties

认证用户名: 认证密码 [,角色名称 ...]

注意: 配置需重启ActiveMQ才会生效。

客户端安全认证:

simpleAuthenticationPlugin 认证: 直接把相关的认证插件配置到xml文件中

ActiveMQ目录conf下activemq.xml的broker元素中添加插件:

 1 <plugins>
 2     <simpleAuthenticationPlugin>
 3         <users>
 4             <authenticationUser username="admin" password="admin" groups="admins,publishers,consumers"/>
 5             <authenticationUser username="publisher" password="publisher"  groups="publishers,consumers"/>
 6             <authenticationUser username="consumer" password="consumer" groups="consumers"/>
 7             <authenticationUser username="guest" password="guest"  groups="guests"/>
 8         </users>
 9     </simpleAuthenticationPlugin>
10 </plugins>
activemq.xml

在代码中认证:

1 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Constants.AUTHENTICATION_USERNAME_ADMIN,
2                 Constants.AUTHENTICATION_PASSWORD_ADMIN, "tcp://localhost:61616");

 重启ActiveMQ, 使配置生效。

用户名和密码认证失败会抛出异常:

 1 Exception in thread "main" javax.jms.JMSSecurityException: User name [ddd] or password is invalid.
 2     at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:52)
 3     at org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1399)
 4     at org.apache.activemq.ActiveMQConnection.ensureConnectionInfoSent(ActiveMQConnection.java:1478)
 5     at org.apache.activemq.ActiveMQConnection.start(ActiveMQConnection.java:527)
 6     at com.itdoc.learn.activemq.helloworld.Sender.main(Sender.java:35)
 7 Caused by: java.lang.SecurityException: User name [ddd] or password is invalid.
 8     at org.apache.activemq.security.SimpleAuthenticationBroker.authenticate(SimpleAuthenticationBroker.java:103)
 9     at org.apache.activemq.security.SimpleAuthenticationBroker.addConnection(SimpleAuthenticationBroker.java:71)
10     at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:99)
11     at org.apache.activemq.broker.TransportConnection.processAddConnection(TransportConnection.java:843)
12     at org.apache.activemq.broker.jmx.ManagedTransportConnection.processAddConnection(ManagedTransportConnection.java:77)
13     at org.apache.activemq.command.ConnectionInfo.visit(ConnectionInfo.java:139)
14     at org.apache.activemq.broker.TransportConnection.service(TransportConnection.java:330)
15     at org.apache.activemq.broker.TransportConnection$1.onCommand(TransportConnection.java:194)
16     at org.apache.activemq.transport.MutexTransport.onCommand(MutexTransport.java:50)
17     at org.apache.activemq.transport.WireFormatNegotiator.onCommand(WireFormatNegotiator.java:125)
18     at org.apache.activemq.transport.AbstractInactivityMonitor.onCommand(AbstractInactivityMonitor.java:301)
19     at org.apache.activemq.transport.TransportSupport.doConsume(TransportSupport.java:83)
20     at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:233)
21     at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:215)
22     at java.lang.Thread.run(Unknown Source)
Exception

认证成功代码:

消息生产者:

 1 /**
 2  * @filename Sender.Java
 3  * @desc 消息生产者
 4  * @blog http://www.cnblogs.com/goodcheap
 5  * @author Chinda Wang
 6  * @create 2017-12-02 16:06
 7  * @version v1.0
 8  * @copyright Copyright © 2017 达华信息科技有限公司 版权所有
 9  * @modifyhistory 2017-12-02 16:06
10  * @modifyauthor Chinda Wang
11  * @modifydesc
12  */
13 package com.itdoc.learn.activemq.helloworld;
14 
15 import com.itdoc.learn.activemq.common.Constants;
16 import org.apache.activemq.ActiveMQConnectionFactory;
17 
18 import javax.jms.*;
19 
20 /**
21  * @author Chinda Wang
22  * @desc 消息生产者
23  * @create 2017-12-02 16:06
24  */
25 public class Sender {
26 
27     public static void main(String[] args) throws Exception {
28 
29 
30         // 第一步: 建立 ConnectionFactory 工厂对象, 需要填入用户名、密码、以及要连接的地址, 均使用默认即可, 默认端口为"tcp//loclhost:61616"
31         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Constants.AUTHENTICATION_USERNAME_ADMIN,
32                 Constants.AUTHENTICATION_PASSWORD_ADMIN, "tcp://localhost:61616");
33         // 第二步: 通过ConnectionFactory工厂对象创建一个Connection连接, 并且调用Connection的start方法开启连接, Connection默认是关闭的。
34         Connection connection = connectionFactory.createConnection();
35         connection.start();
36         /*
37          * 第三步: 通过Connection对象创建Session会话(上下文环境对象), 用于接收消息, 参数位置1为是否启用事务, 参数位置2为签收模式,
38          * 一般设置为自动签收。
39          */
40         Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
41         /*
42          * 第四步: 通过Session创建Destination对象, 指的是一个客户端用来指定生产消息目标和消费消息来源的对象, 在PTP模式中, Destination
43          * 被称作为Queue, 即队列; 在Pub/Sub模式, Destination被称作Topic, 即主题。在程序中可以使用多个Queue和Topic。
44          */
45         Destination destination = session.createQueue("queue1");
46         // 第五步: 需要通过Session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer
47         MessageProducer producer = session.createProducer(destination);
48         // 第六步: 可以使用MessageProducer的setDeliveryMode()方法为其设置持久化特性和非持久化特性(DeliveryMode)。
49         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
50         /*
51          * 第七步: 使用JMS规范的TextMessage形式创建数据(通过Session对象), 并用MessageProducer的send()方法发送数据。同理, 客户端使用
52          * receive()方法进行接收数据。
53          */
54         TextMessage textMessage = session.createTextMessage();
55         for (int i = 1; i <= 10; i++) {
56             textMessage.setText("I am Message! id: " + i);
57             producer.send(textMessage);
58             System.out.println("生产者: " + textMessage.getText());
59         }
60         // 第八步: 关闭Connection连接
61         if (connection != null) {
62             connection.close();
63         }
64     }
65 }
Sender.Java

 消息消费者:

 1 /**
 2  * @filename Receiver.Java
 3  * @desc 消息消费者
 4  * @blog http://www.cnblogs.com/goodcheap
 5  * @author Chinda Wang
 6  * @create 2017-12-02 16:07
 7  * @version v1.0
 8  * @copyright Copyright © 2017 达华信息科技有限公司 版权所有
 9  * @modifyhistory 2017-12-02 16:07
10  * @modifyauthor Chinda Wang
11  * @modifydesc
12  */
13 package com.itdoc.learn.activemq.helloworld;
14 
15 import com.itdoc.learn.activemq.common.Constants;
16 import org.apache.activemq.ActiveMQConnectionFactory;
17 
18 import javax.jms.*;
19 
20 /**
21  * @desc 消息消费者
22  * @author Chinda Wang
23  * @create 2017-12-02 16:07
24  */
25 public class Receiver {
26 
27     public static void main(String[] args) throws Exception {
28         // 第一步: 建立 ConnectionFactory 工厂对象, 需要填入用户名、密码、以及要连接的地址, 均使用默认即可, 默认端口为"tcp//loclhost:61616"
29         ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
30                 Constants.AUTHENTICATION_USERNAME_GUEST,
31                 Constants.AUTHENTICATION_PASSWORD_GUEST,
32                 "tcp://localhost:61616");
33         // 第二步: 通过ConnectionFactory工厂对象创建一个Connection连接, 并且调用Connection的start方法开启连接, Connection默认是关闭的。
34         Connection connection = connectionFactory.createConnection();
35         connection.start();
36         /*
37          * 第三步: 通过Connection对象创建Session会话(上下文环境对象), 用于接收消息, 参数位置1为是否启用事务, 参数位置2为签收模式,
38          * 一般设置为自动签收。
39          */
40         Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
41         /*
42          * 第四步: 通过Session创建Destination对象, 指的是一个客户端用来指定生产消息目标和消费消息来源的对象, 在PTP模式中, Destination
43          * 被称作为Queue, 即队列; 在Pub/Sub模式, Destination被称作Topic, 即主题。在程序中可以使用多个Queue和Topic。
44          */
45         Destination destination = session.createQueue("queue1");
46         // 第五步: 需要通过Session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer
47         MessageConsumer consumer = session.createConsumer(destination);
48         /*
49          * 第六步: 使用JMS规范的TextMessage形式创建数据(通过Session对象), 并用MessageProducer的send()方法发送数据。同理, 客户端使用
50          * receive()方法进行接收数据。
51          */
52         while (true) {
53             TextMessage msg = (TextMessage) consumer.receive();
54             if (msg == null) {
55                 break;
56             }
57             System.out.println("收到内容: " + msg.getText());
58         }
59         // 第七步: 关闭Connection连接
60         if (connection != null) {
61             connection.close();
62         }
63     }
64 }
Receiver.Java

 认证常量类:

 1 /**
 2  * @filename Constants.Java
 3  * @desc 常量类
 4  * @blog http://www.cnblogs.com/goodcheap
 5  * @author Chinda Wang
 6  * @create 2017-12-09 10:14
 7  * @version v1.0
 8  * @copyright Copyright © 2017 达华信息科技有限公司 版权所有
 9  * @modifyhistory 2017-12-09 10:14
10  * @modifyauthor Chinda Wang
11  * @modifydesc
12  */
13 package com.itdoc.learn.activemq.common;
14 
15 /**
16  * @desc 常量类
17  * @author Chinda Wang
18  * @create 2017-12-09 10:14
19  */
20 public class Constants {
21 
22     /** 认证用户名密码 */
23     public static final String AUTHENTICATION_USERNAME_ADMIN = "admin";
24     public static final String AUTHENTICATION_PASSWORD_ADMIN = "admin";
25 
26     public static final String AUTHENTICATION_USERNAME_PUBLISHER = "publisher";
27     public static final String AUTHENTICATION_PASSWORD_PUBLISHER = "publisher";
28 
29     public static final String AUTHENTICATION_USERNAME_CONSUMER = "consumer";
30     public static final String AUTHENTICATION_PASSWORD_CONSUMER = "consumer";
31 
32     public static final String AUTHENTICATION_USERNAME_GUEST = "guest";
33     public static final String AUTHENTICATION_PASSWORD_GUEST = "guest";
34 
35 }
Constants.Java

  

原文地址:https://www.cnblogs.com/chinda/p/8010917.html