一、认证

认证(Authentication):验证某个实体或者用户是否有权限访问受保护资源。

MQ提供两种插件用于权限认证:
(一)、Simple authentication plug-in:直接把相关的权限认证信息配置到XML文件中。

配置 conf/activemq.xml 的 broke元素添加插件:

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

代码中的认证方式两种:

1、在创建Connection的时候认证

  1. //用户认证
  2. Connection conn = connFactory.createConnection("admin","password");

2、也可以在创建ConnectionFactory工厂的时候认证

  1. ConnectionFactory connFactory = new ActiveMQConnectionFactory("admin","password",url);

(二)、JAAS authentication plug-in:实现了JAAS API,提供了一个更强大的和可定制的权限方案。

配置方式:

1、在conf目录中创建 login.config 文件 用户 配置 PropertiesLoginModule:

  1. activemq-domain {
  2. org.apache.activemq.jaas.PropertiesLoginModule required debug=true
  3. org.apache.activemq.jaas.properties.user="users.properties"
  4. org.apache.activemq.jaas.properties.group="groups.properties";
  5. };

2、在conf目录中创建users.properties 文件用户配置用户:

  1. # 创建四个用户
  2. admin=password
  3. publisher=password
  4. consumer=password
  5. guest=password

3、在conf目录中创建groups.properties 文件用户配置用户组:

  1. #创建四个组并分配用户
  2. admins=admin
  3. publishers=admin,publisher
  4. consumers=admin,publisher,consumer
  5. guests=guest

4、将该配置插入到activemq.xml中:

  1. <!-- JAAS authentication plug-in -->
  2. <plugins>
  3. <jaasAuthenticationPlugin configuration="activemq-domain" />
  4. </plugins>

5、配置MQ的启动参数:

使用dos命令启动:

  1. D:\tools\apache-activemq-5.6.0-bin\apache-activemq-5.6.0\bin\win64>activemq.bat -Djava.security.auth.login.config=D:/tools/apache-activemq-5.6.0-bin/apache-activemq-5.6.0/conf/login.config

6、在代码中的认证方式与Simple authentication plug-in 相同。

二、授权

基于认证的基础上,可以根据实际用户角色来授予相应的权限,如有些用户有队列写的权限,有些则只能读等等。
两种授权方式
(一)、目的地级别授权

JMS目的地的三种操作级别:
  Read :读取目的地消息权限
  Write:发送消息到目的地权限
  Admin:管理目的地的权限

配置方式  conf/activemq.xml :

  1. <plugins>
  2. <jaasAuthenticationPlugin configuration="activemq-domain" />
  3. <authorizationPlugin>
  4. <map>
  5. <authorizationMap>
  6. <authorizationEntries>
  7. <authorizationEntry topic="topic.ch09" read="consumers" write="publishers" admin="publishers" />
  8. </authorizationEntries>
  9. </authorizationMap>
  10. </map>
  11. </authorizationPlugin>
  12. </plugins>

(二)、消息级别授权

授权特定的消息。

开发步骤:
1、实现消息授权插件,需要实现MessageAuthorizationPolicy接口

  1. public class AuthorizationPolicy implements MessageAuthorizationPolicy {
  2. private static final Log LOG = LogFactory.
  3. getLog(AuthorizationPolicy.class);
  4. public boolean isAllowedToConsume(ConnectionContext context,
  5. Message message) {
  6. LOG.info(context.getConnection().getRemoteAddress());
  7. String remoteAddress = context.getConnection().getRemoteAddress();
  8. if (remoteAddress.startsWith("/127.0.0.1")) {
  9. LOG.info("Permission to consume granted");
  10. return true;
  11. } else {
  12. LOG.info("Permission to consume denied");
  13. return false;
  14. }
  15. }
  16. }

2、把插件实现类打成JAR包,放入到activeMq 的 lib目录中

3、在activemq.xml中设置<messageAuthorizationPolicy>元素

  1. <messageAuthorizationPolicy>
  2. <bean class="org.apache.activemq.book.ch6.AuthorizationPolicy" xmlns="http://www.springframework.org/schema/beans" />
  3. </messageAuthorizationPolicy>

三、自定义安全插件

插件逻辑需要实现BrokerFilter类,并且通过BrokerPlugin实现类来安装,用于拦截,Broker级别的操作:

  • 接入消费者和生产者
  • 提交事务
  • 添加和删除broker的连接

demo:基于IP地址,限制Broker连接。

  1. package ch02.ptp;
  2. import java.util.List;
  3.  
  4. import org.apache.activemq.broker.Broker;
  5. import org.apache.activemq.broker.BrokerFilter;
  6. import org.apache.activemq.broker.ConnectionContext;
  7. import org.apache.activemq.command.ConnectionInfo;
  8.  
  9. public class IPAuthenticationBroker extends BrokerFilter {
  10. List<String> allowedIPAddresses;
  11. public IPAuthenticationBroker(Broker next, List<String>allowedIPAddresses) {
  12. super(next);
  13. this.allowedIPAddresses = allowedIPAddresses;
  14. }
  15. public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception {
  16. String remoteAddress = context.getConnection().getRemoteAddress();
  17. if (!allowedIPAddresses.contains(remoteAddress)) {
  18. throw new SecurityException("Connecting from IP address "
  19. + remoteAddress+ " is not allowed" );
  20. }
  21. super.addConnection(context, info);
  22. }
  23. }

安装插件:

  1. package ch02.ptp;
  2.  
  3. import java.util.List;
  4.  
  5. import org.apache.activemq.broker.Broker;
  6. import org.apache.activemq.broker.BrokerPlugin;
  7.  
  8. public class IPAuthenticationPlugin implements BrokerPlugin {
  9. List<String> allowedIPAddresses;
  10. public Broker installPlugin(Broker broker) throws Exception {
  11. return new IPAuthenticationBroker(broker, allowedIPAddresses);
  12. }
  13. public List<String> getAllowedIPAddresses() {
  14. return allowedIPAddresses;
  15. }
  16. public void setAllowedIPAddresses(List<String> allowedIPAddresses) {
  17. this.allowedIPAddresses = allowedIPAddresses;
  18. }
  19. }

ps:将这连个类打成jar包放到activemq的lib目录下

配置自定义插件:

  1. <plugins>
  2. <bean xmlns="http://www.springframework.org/schema/beans" id="ipAuthenticationPlugin"
  3. class="org.apache.activemq.book.ch6.IPAuthenticationPlugin">
  4. <property name="allowedIPAddresses">
  5. <list>
  6.   <value>127.0.0.1</value>
  7. </list>
  8. </property>
  9. </bean>
  10. </plugins>

JMS 之 Active MQ的安全机制的更多相关文章

  1. JMS 之 Active MQ 消息存储

    一.消息的存储方式 ActiveMQ支持JMS规范中的持久化消息与非持久化消息 持久化消息通常用于不管是否消费者在线,它们都会保证消息会被消费者消费.当消息被确认消费后,会从存储中删除 非持久化消息通 ...

  2. JMS 之 Active MQ 启动嵌入式Broke

    一.如何启动active MQ 服务 (一).使用命令启动 /bin 目录下 ./activemq start 默认使用conf/activemq.xml 配置文件 b.[root@localhost ...

  3. JMS 之 Active MQ 的消息传输

    本文使用Active MQ5.6 一.消息协商器(Message Broker) broke:消息的交换器,就是对消息进行管理的容器.ActiveMQ 可以创建多个 Broker,客户端与Active ...

  4. JMS 之 Active MQ 的spring整合

    一.与spring整合实现ptp的同步接收消息 pom.xml: <!-- https://mvnrepository.com/artifact/org.springframework/spri ...

  5. Active MQ C#实现

    原文链接: Active MQ C#实现 内容概要 主要以源码的形式介绍如何用C#实现同Active MQ 的通讯.本文假设你已经正确安装JDK1.6.x,了解Active MQ并有一定的编程基础. ...

  6. 使用Active MQ在.net和java系统之间通信

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现 一.特性列表 ⒈ 多种语言和 ...

  7. Active MQ的初步探索

    参考链接: http://blog.csdn.net/jiuqiyuliang/article/details/46701559 JMS是jee规范,active MQ是该规范的实现 Active M ...

  8. Active MQ C++实现通讯记录

    Active MQ  C++实现通讯 背景知识: ActiveMQ是一个易于使用的消息中间件. 消息中间件 我们简单的介绍一下消息中间件,对它有一个基本认识就好,消息中间件(MOM:Message O ...

  9. 初探active mq

    mq(message queue),即消息队列,目前比较流行消息队列是active mq 和kafka.本文介绍如何简单的使用active mq. ActiveMQ官网下载地址:http://acti ...

随机推荐

  1. 黄聪:如何使用Add-on SDK开发一个自己的火狐扩展

    火狐开放了扩展的开发权限给程序员们,相信很多人都会希望自己做一些扩展来方便一些使用. 我最近做一些项目也需要开发一个火狐扩展,方便收集自己需要的数据,因此研究了几天怎么开发,现在已经差不多完成了,就顺 ...

  2. Django 组件-cookie与session

    Cookie概述 什么叫Cookie Cookie翻译成中文是小甜点,小饼干的意思.在HTTP中它表示服务器送给客户端浏览器的小甜点.其实Cookie是key-value结构,类似于一个python中 ...

  3. xss 攻击 sql 注入

    XSS测试 "/><script>alert(document.cookie)</script><!-- <script>alert(docu ...

  4. if __name__=="__main__": 这个结尾的理解

    print "别人应用我做为模块导入,就只看到我" if __name__=="__main__": print "自己文件执行就看到我输出" ...

  5. centos6.9 x64安装http,php5.6,curl5.29,mysql最后安装zabbix3.4+zabbix客户端

    https://www.zabbix.com/documentation/3.4/zh/manual/installation/requirementshttps://www.zabbix.com/d ...

  6. 分析iOS Crash文件:符号化iOS Crash文件的3种方法

    转自:http://www.cocoachina.com/industry/20140514/8418.html 转自wufawei的博客 当你的应用提交到App Store或者各个渠道之后,请问你多 ...

  7. vmware12中ubuntu16.10的vmware tools失效,导致不能复制粘贴文字以及自动适应窗口分辨率

    问题: 复制命令后,在vmware的ubuntu中粘贴不了,网上说要安装VMWare Tools,但是安装了VMWare Tools 还是不行! 最终找到如下方法: 新安装或异常关机和重新划分分区导致 ...

  8. iframe高度从内向外撑起

    index.html: <div style="height: 200px;"></div> <iframe id="gys" s ...

  9. C++ 栈 (数组实现)

    上一篇用链表实现了stack,这篇我们采用数组来存储数据,数组更容易理解,直接贴代码 第一.代码实现 #pragma once #include <iostream> using name ...

  10. [OpenCV Qt教程] 如何在内存中压缩图像

    本文译自:http://www.robot-home.it/blog/en/software/tutorial-opencv-qt-comprimere-un-immagine-in-memoria/ ...