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.无论哪种持久化机制 ...
随机推荐
- Create Fiori List App Report with ABAP CDS view – PART 2
In the Part 1 blog, we have discussed below topics CDS annotations for Fiori List Report. How to cre ...
- 炒鸡简单的javaScript的call和apply方法
解释一 作者:杨志 链接:https://www.zhihu.com/question/20289071/answer/14644278 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商 ...
- cgi、fastcgi、php-cgi、php-fpm的关系
1. CGI CGI全称是"公共网关接口"(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行"交谈"的一种工具,其 ...
- ExtJs工具篇(1)——在Aptana3中安装ExtJS 代码提示插件
首先得下载Aptana 这个软件,我下载的是Aptana3这个版本.下载后,在"帮助"菜单中选择"安装新软件",弹出下面的对话框: 我们需要安装一个叫做&quo ...
- 责任链模式的使用-Netty ChannelPipeline和Mina IoFilterChain分析
本文来自网易云社区 作者:乔安然 1. Chain of Responsiblity 定义: 使多个对象都有机会处理请求,从而避免请求的发送者和接受者之间的耦合关系.将这个对象连成一条链,并沿着这条链 ...
- runtime总结 iOS
Runtime的特性主要是消息(方法)传递,如果消息(方法)在对象中找不到,就进行转发,具体怎么实现的呢.我们从下面几个方面探寻Runtime的实现机制. Runtime介绍 Runtime消息传递 ...
- 获取已安装app的bundle id
备注:以下是私有api 苹果审核会被拒绝. 导入头文件 #import <objc/runtime.h> /// 获取其他APP信息(iOS11无效) + (NSArray *)getOt ...
- IOException: win32 io returned 267. Path:
unity3d在导出android项目时出现了这个错误,找了一圈也没找到原因,最后把项目名中空格去掉后OK了,坑啊!!!!
- 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下)
基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下) 昨天谈到了Mysql实现主从复制,但由于时间原因并未讲有关读写分离的实现,之所以有读写分离,是为了使数据库拥有双机热备功能,至于双 ...
- Anytime项目开发记录4
做事情列表,我在程序中命名为“正在做”. 这是一个Fragment,应用的主页面,由一个MainActivity加上DoingListFragment和PersonFragment组成.PersonF ...