ActiveMQ(3) ActiveMQ创建(simpleAuthenticationPlugin)安全认证
控制端安全认证:
ActiveMQ目录conf下jetty.xml:
<bean id="securityLoginService" class="org.eclipse.jetty.security.HashLoginService">
<property name="name" value="ActiveMQRealm" />
<property name="config" value="${activemq.conf}/jetty-realm.properties" />
</bean> <bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
<property name="name" value="BASIC" />
<property name="roles" value="user,admin" />
<!-- set authenticate=false to disable login -->
<property name="authenticate" value="true" />
</bean>
jetty.xml
<property name="authenticate" value="true" /> true: 需要认证 false: 不需要认证
ActiveMQ目录conf下jetty-realm.properties:
# Defines users that can access the web (console, demo, etc.)
# username: password [,rolename ...]
admin: admin, admin
user: user, user
jetty-realm.properties
认证用户名: 认证密码 [,角色名称 ...]
注意: 配置需重启ActiveMQ才会生效。
客户端安全认证:
simpleAuthenticationPlugin 认证: 直接把相关的认证插件配置到xml文件中
ActiveMQ目录conf下activemq.xml的broker元素中添加插件:
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="admin" password="admin" groups="admins,publishers,consumers"/>
<authenticationUser username="publisher" password="publisher" groups="publishers,consumers"/>
<authenticationUser username="consumer" password="consumer" groups="consumers"/>
<authenticationUser username="guest" password="guest" groups="guests"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
activemq.xml
在代码中认证:
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Constants.AUTHENTICATION_USERNAME_ADMIN,
Constants.AUTHENTICATION_PASSWORD_ADMIN, "tcp://localhost:61616");
重启ActiveMQ, 使配置生效。
用户名和密码认证失败会抛出异常:
Exception in thread "main" javax.jms.JMSSecurityException: User name [ddd] or password is invalid.
at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:52)
at org.apache.activemq.ActiveMQConnection.syncSendPacket(ActiveMQConnection.java:1399)
at org.apache.activemq.ActiveMQConnection.ensureConnectionInfoSent(ActiveMQConnection.java:1478)
at org.apache.activemq.ActiveMQConnection.start(ActiveMQConnection.java:527)
at com.itdoc.learn.activemq.helloworld.Sender.main(Sender.java:35)
Caused by: java.lang.SecurityException: User name [ddd] or password is invalid.
at org.apache.activemq.security.SimpleAuthenticationBroker.authenticate(SimpleAuthenticationBroker.java:103)
at org.apache.activemq.security.SimpleAuthenticationBroker.addConnection(SimpleAuthenticationBroker.java:71)
at org.apache.activemq.broker.BrokerFilter.addConnection(BrokerFilter.java:99)
at org.apache.activemq.broker.TransportConnection.processAddConnection(TransportConnection.java:843)
at org.apache.activemq.broker.jmx.ManagedTransportConnection.processAddConnection(ManagedTransportConnection.java:77)
at org.apache.activemq.command.ConnectionInfo.visit(ConnectionInfo.java:139)
at org.apache.activemq.broker.TransportConnection.service(TransportConnection.java:330)
at org.apache.activemq.broker.TransportConnection$1.onCommand(TransportConnection.java:194)
at org.apache.activemq.transport.MutexTransport.onCommand(MutexTransport.java:50)
at org.apache.activemq.transport.WireFormatNegotiator.onCommand(WireFormatNegotiator.java:125)
at org.apache.activemq.transport.AbstractInactivityMonitor.onCommand(AbstractInactivityMonitor.java:301)
at org.apache.activemq.transport.TransportSupport.doConsume(TransportSupport.java:83)
at org.apache.activemq.transport.tcp.TcpTransport.doRun(TcpTransport.java:233)
at org.apache.activemq.transport.tcp.TcpTransport.run(TcpTransport.java:215)
at java.lang.Thread.run(Unknown Source)
Exception
认证成功代码:
消息生产者:
/**
* @filename Sender.Java
* @desc 消息生产者
* @blog http://www.cnblogs.com/goodcheap
* @author Chinda Wang
* @create 2017-12-02 16:06
* @version v1.0
* @copyright Copyright © 2017 达华信息科技有限公司 版权所有
* @modifyhistory 2017-12-02 16:06
* @modifyauthor Chinda Wang
* @modifydesc
*/
package com.itdoc.learn.activemq.helloworld; import com.itdoc.learn.activemq.common.Constants;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /**
* @author Chinda Wang
* @desc 消息生产者
* @create 2017-12-02 16:06
*/
public class Sender { public static void main(String[] args) throws Exception { // 第一步: 建立 ConnectionFactory 工厂对象, 需要填入用户名、密码、以及要连接的地址, 均使用默认即可, 默认端口为"tcp//loclhost:61616"
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(Constants.AUTHENTICATION_USERNAME_ADMIN,
Constants.AUTHENTICATION_PASSWORD_ADMIN, "tcp://localhost:61616");
// 第二步: 通过ConnectionFactory工厂对象创建一个Connection连接, 并且调用Connection的start方法开启连接, Connection默认是关闭的。
Connection connection = connectionFactory.createConnection();
connection.start();
/*
* 第三步: 通过Connection对象创建Session会话(上下文环境对象), 用于接收消息, 参数位置1为是否启用事务, 参数位置2为签收模式,
* 一般设置为自动签收。
*/
Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
/*
* 第四步: 通过Session创建Destination对象, 指的是一个客户端用来指定生产消息目标和消费消息来源的对象, 在PTP模式中, Destination
* 被称作为Queue, 即队列; 在Pub/Sub模式, Destination被称作Topic, 即主题。在程序中可以使用多个Queue和Topic。
*/
Destination destination = session.createQueue("queue1");
// 第五步: 需要通过Session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer
MessageProducer producer = session.createProducer(destination);
// 第六步: 可以使用MessageProducer的setDeliveryMode()方法为其设置持久化特性和非持久化特性(DeliveryMode)。
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
/*
* 第七步: 使用JMS规范的TextMessage形式创建数据(通过Session对象), 并用MessageProducer的send()方法发送数据。同理, 客户端使用
* receive()方法进行接收数据。
*/
TextMessage textMessage = session.createTextMessage();
for (int i = 1; i <= 10; i++) {
textMessage.setText("I am Message! id: " + i);
producer.send(textMessage);
System.out.println("生产者: " + textMessage.getText());
}
// 第八步: 关闭Connection连接
if (connection != null) {
connection.close();
}
}
}
Sender.Java
消息消费者:
/**
* @filename Receiver.Java
* @desc 消息消费者
* @blog http://www.cnblogs.com/goodcheap
* @author Chinda Wang
* @create 2017-12-02 16:07
* @version v1.0
* @copyright Copyright © 2017 达华信息科技有限公司 版权所有
* @modifyhistory 2017-12-02 16:07
* @modifyauthor Chinda Wang
* @modifydesc
*/
package com.itdoc.learn.activemq.helloworld; import com.itdoc.learn.activemq.common.Constants;
import org.apache.activemq.ActiveMQConnectionFactory; import javax.jms.*; /**
* @desc 消息消费者
* @author Chinda Wang
* @create 2017-12-02 16:07
*/
public class Receiver { public static void main(String[] args) throws Exception {
// 第一步: 建立 ConnectionFactory 工厂对象, 需要填入用户名、密码、以及要连接的地址, 均使用默认即可, 默认端口为"tcp//loclhost:61616"
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
Constants.AUTHENTICATION_USERNAME_GUEST,
Constants.AUTHENTICATION_PASSWORD_GUEST,
"tcp://localhost:61616");
// 第二步: 通过ConnectionFactory工厂对象创建一个Connection连接, 并且调用Connection的start方法开启连接, Connection默认是关闭的。
Connection connection = connectionFactory.createConnection();
connection.start();
/*
* 第三步: 通过Connection对象创建Session会话(上下文环境对象), 用于接收消息, 参数位置1为是否启用事务, 参数位置2为签收模式,
* 一般设置为自动签收。
*/
Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
/*
* 第四步: 通过Session创建Destination对象, 指的是一个客户端用来指定生产消息目标和消费消息来源的对象, 在PTP模式中, Destination
* 被称作为Queue, 即队列; 在Pub/Sub模式, Destination被称作Topic, 即主题。在程序中可以使用多个Queue和Topic。
*/
Destination destination = session.createQueue("queue1");
// 第五步: 需要通过Session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer
MessageConsumer consumer = session.createConsumer(destination);
/*
* 第六步: 使用JMS规范的TextMessage形式创建数据(通过Session对象), 并用MessageProducer的send()方法发送数据。同理, 客户端使用
* receive()方法进行接收数据。
*/
while (true) {
TextMessage msg = (TextMessage) consumer.receive();
if (msg == null) {
break;
}
System.out.println("收到内容: " + msg.getText());
}
// 第七步: 关闭Connection连接
if (connection != null) {
connection.close();
}
}
}
Receiver.Java
认证常量类:
/**
* @filename Constants.Java
* @desc 常量类
* @blog http://www.cnblogs.com/goodcheap
* @author Chinda Wang
* @create 2017-12-09 10:14
* @version v1.0
* @copyright Copyright © 2017 达华信息科技有限公司 版权所有
* @modifyhistory 2017-12-09 10:14
* @modifyauthor Chinda Wang
* @modifydesc
*/
package com.itdoc.learn.activemq.common; /**
* @desc 常量类
* @author Chinda Wang
* @create 2017-12-09 10:14
*/
public class Constants { /** 认证用户名密码 */
public static final String AUTHENTICATION_USERNAME_ADMIN = "admin";
public static final String AUTHENTICATION_PASSWORD_ADMIN = "admin"; public static final String AUTHENTICATION_USERNAME_PUBLISHER = "publisher";
public static final String AUTHENTICATION_PASSWORD_PUBLISHER = "publisher"; public static final String AUTHENTICATION_USERNAME_CONSUMER = "consumer";
public static final String AUTHENTICATION_PASSWORD_CONSUMER = "consumer"; public static final String AUTHENTICATION_USERNAME_GUEST = "guest";
public static final String AUTHENTICATION_PASSWORD_GUEST = "guest"; }
Constants.Java
ActiveMQ(3) ActiveMQ创建(simpleAuthenticationPlugin)安全认证的更多相关文章
- 【分布式系列之ActiveMq】ActiveMq入门示例
前言 github地址:https://github.com/AndyFlower/web-back/tree/master/ActiveMq01 下载ActiveMQ :http://activem ...
- 【ActiveMQ】ActiveMQ在CentOS的搭建与使用
下载 到ActiveMQ官网,找到下载点. 目前, 官网为http://activemq.apache.org/. 我们下载目前最新的版本吧,当前的Linux版本下载地址之一为:http://apac ...
- ActiveMQ之一--ActiveMQ入门
MQ的消费-生产者模型的一个典型的代表,一端往消息队列中不断的写入消息,而另一端则可以读取或者订阅队列中的消息.MQ和JMS类似,但不同的是JMS是SUN JAVA消息中间件服务的一个标准和API定义 ...
- ActiveMQ(2) ActiveMQ创建HelloWorld
启动ActiveMQ: 请参见:ActiveMQ(1) 初识ActiveMQ 创建Maven工程: pom文件: <project xmlns="http://maven.apache ...
- ActiveMQ(4)---ActiveMQ原理分析之消息消费
消费端消费消息的原理 我们通过上一节课的讲解,知道有两种方法可以接收消息,一种是使用同步阻塞的MessageConsumer#receive方法.另一种是使用消息监听器MessageListener. ...
- ActiveMQ(3)---ActiveMQ原理分析之消息持久化
持久化消息和非持久化消息的存储原理 正常情况下,非持久化消息是存储在内存中的,持久化消息是存储在文件中的.能够存储的最大消息数据在${ActiveMQ_HOME}/conf/activemq.xml文 ...
- ActiveMQ(2)---ActiveMQ原理分析之消息发送
持久化消息和非持久化消息的发送策略 消息同步发送和异步发送 ActiveMQ支持同步.异步两种发送模式将消息发送到broker上.同步发送过程中,发送者发送一条消息会阻塞直到broker反馈一个确认消 ...
- ActiveMQ Cluster (ActiveMQ 集群) 配置
构建高可用的ActiveMQ系统在生产环境中是非常重要的,对于这个apache的消息中间件实现高可用非常简单,只要在Apache ActiveMQ单点基本配置基础上做一次配置变更(如果在一台设备上部署 ...
- ActiveMQ(4) ActiveMQ JDBC 持久化 Mysql 数据库
ActiveMQ 消息持久化机制: ActiveMQ 消息的持久化机制有 JDBC.AMQ.KahaDB 和 LevelDB,其中本示例版本(5.15.2)默认机制为 KahaDB.无论哪种持久化机制 ...
随机推荐
- poj2230 欧拉回路
http://poj.org/problem?id=2230 Description Bessie's been appointed the new watch-cow for the farm. E ...
- Zookeeper系列(二) Zookeeper配置说明
在配置ZooKeeper配置文件时,有些参数是必需的,有些参数是可选的,这些必需的参数构成了Zookeeper配置文件的最低配置要求,如果需要对ZooKeeper进行更详细的配置,可以 ...
- 1826: [JSOI2010]缓存交换
1826: [JSOI2010]缓存交换 https://www.lydsy.com/JudgeOnline/problem.php?id=1826 分析: 简单的贪心,然后调啊调...最近怎么了,码 ...
- sqlserver一次性删除master数据库中的所有用户添加的表
use master; go sp_msforeachtable @command1="drop table ?" go
- Scala学习笔记(二):运行脚本文件
在某个目录(如:F:\)下新建一个文本文件,命名为:hello.scala 其内容为: println("Hello World!") 那么这个时候该怎么运行这个脚本文件呢? 通过 ...
- 存一些有用的CSS
reset ;} table{} fieldset,img{} address,caption,cite,code,dfn,em,strong,th,var{font-style:normal;fon ...
- 不用找了,比较全的signalR例子已经为你准备好了(2)---JqGrid 服务端刷新方式-注释详细-DEMO源码下载
上次用客户端进行数据刷新的方式,和官方的Demo实现存在差异性,今天花了一点时间好好研究了一下后台时时刷新的方式.将写的代码重新update了一次,在这之前找过好多的资料,发现都没有找到好的例子,自己 ...
- MySQL☞insert value与values
最近公司事情太忙,作为以一挑十的测试,只能苦逼的累死累活的.好不容易临近上线,可以偷个懒写个文章. 简单的说说如何向表中插入数据: 1.向表中所有的列插入数据(插入多行数据): insert int ...
- jmeter接口测试--参数化
接口测试时遇到一些属性不能重复时,可以使用Random 随机函数,除此之外,也可以用用户参数 一..随机参数化 1.在jmeter工具,菜单-选项-函数助手对话框,输入数值,属性,点击生成: 2.在相 ...
- Swiper 常用功能及配置清单
内容来源于Swiper中文在线(http://www.swiper.com.cn/),由于Swiper功能强大,这里只将常用的功能列出来,方便开发. 这里统一使用Swiper最新版 4.0做为演示! ...