JMS 之 Active MQ的安全机制

简介:

一、认证

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

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

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

        <plugins>
            <simpleAuthenticationPlugin>
                <users>
                    <authenticationUser username="admin" password="password" groups="admins,publishers,consumers"/>
                    <authenticationUser username="publisher" password="password"  groups="publishers,consumers"/>
                    <authenticationUser username="consumer" password="password" groups="consumers"/>
                    <authenticationUser username="guest" password="password"  groups="guests"/>
                </users>
            </simpleAuthenticationPlugin>
        </plugins>

代码中的认证方式两种:

1、在创建Connection的时候认证

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

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

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

 

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

配置方式:

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

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

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

# 创建四个用户
admin=password  
publisher=password 
consumer=password  
guest=password

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

#创建四个组并分配用户
admins=admin
publishers=admin,publisher
consumers=admin,publisher,consumer
guests=guest

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

<!-- JAAS authentication plug-in -->
        <plugins>
            <jaasAuthenticationPlugin configuration="activemq-domain" />
        </plugins>

5、配置MQ的启动参数:

使用dos命令启动:

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 :

<plugins>
    <jaasAuthenticationPlugin configuration="activemq-domain" />
    <authorizationPlugin>
        <map>
            <authorizationMap>
                <authorizationEntries>
                    <authorizationEntry topic="topic.ch09" read="consumers" write="publishers" admin="publishers" />
                </authorizationEntries>
            </authorizationMap>
        </map>
    </authorizationPlugin></plugins>

(二)、消息级别授权

授权特定的消息。

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

public class AuthorizationPolicy implements MessageAuthorizationPolicy {    private static final Log LOG = LogFactory.
        getLog(AuthorizationPolicy.class);    public boolean isAllowedToConsume(ConnectionContext context,
        Message message) {
        LOG.info(context.getConnection().getRemoteAddress());
        String remoteAddress = context.getConnection().getRemoteAddress();        if (remoteAddress.startsWith("/127.0.0.1")) {
            LOG.info("Permission to consume granted");            return true;
        } else {
        LOG.info("Permission to consume denied");        return false;
    }
    }
}

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

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

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

三、自定义安全插件

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

  • 接入消费者和生产者

  • 提交事务

  • 添加和删除broker的连接

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

package ch02.ptp;import java.util.List;import org.apache.activemq.broker.Broker;import org.apache.activemq.broker.BrokerFilter;import org.apache.activemq.broker.ConnectionContext;import org.apache.activemq.command.ConnectionInfo;public class IPAuthenticationBroker extends BrokerFilter {
    List<String> allowedIPAddresses;    public IPAuthenticationBroker(Broker next, List<String>allowedIPAddresses) {        super(next);        this.allowedIPAddresses = allowedIPAddresses;
    }    public void addConnection(ConnectionContext context, ConnectionInfo info) throws Exception {
        String remoteAddress = context.getConnection().getRemoteAddress();        if (!allowedIPAddresses.contains(remoteAddress)) {        throw new SecurityException("Connecting from IP address "
            + remoteAddress+ " is not allowed" );
        }        super.addConnection(context, info);
    }
}

安装插件:

package ch02.ptp;import java.util.List;import org.apache.activemq.broker.Broker;import org.apache.activemq.broker.BrokerPlugin;public class IPAuthenticationPlugin implements BrokerPlugin {
    List<String> allowedIPAddresses;    public Broker installPlugin(Broker broker) throws Exception {        return new IPAuthenticationBroker(broker, allowedIPAddresses);
    }    public List<String> getAllowedIPAddresses() {        return allowedIPAddresses;
    }    public void setAllowedIPAddresses(List<String> allowedIPAddresses) {        this.allowedIPAddresses = allowedIPAddresses;
    }
}

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

配置自定义插件:

<plugins>
    <bean xmlns="http://www.springframework.org/schema/beans" id="ipAuthenticationPlugin"
       class="org.apache.activemq.book.ch6.IPAuthenticationPlugin">
        <property name="allowedIPAddresses">
            <list>
              <value>127.0.0.1</value>
            </list>
        </property>
    </bean></plugins>













本文转自xsster51CTO博客,原文链接:http://blog.51cto.com/12945177/1951673 ,如需转载请自行联系原作者





相关实践学习
RocketMQ一站式入门使用
从源码编译、部署broker、部署namesrv,使用java客户端首发消息等一站式入门RocketMQ。
消息队列 MNS 入门课程
1、消息队列MNS简介 本节课介绍消息队列的MNS的基础概念 2、消息队列MNS特性 本节课介绍消息队列的MNS的主要特性 3、MNS的最佳实践及场景应用 本节课介绍消息队列的MNS的最佳实践及场景应用案例 4、手把手系列:消息队列MNS实操讲 本节课介绍消息队列的MNS的实际操作演示 5、动手实验:基于MNS,0基础轻松构建 Web Client 本节课带您一起基于MNS,0基础轻松构建 Web Client
相关文章
|
存储 消息中间件 Java
|
消息中间件 网络协议 网络安全
|
存储 消息中间件 监控
|
1月前
|
消息中间件 存储 监控
RabbitMQ:分布式系统中的高效消息队列
RabbitMQ:分布式系统中的高效消息队列
|
4月前
|
消息中间件 NoSQL 数据库
一文讲透消息队列RocketMQ实现消费幂等
这篇文章,我们聊聊消息队列中非常重要的最佳实践之一:消费幂等。
一文讲透消息队列RocketMQ实现消费幂等
|
1月前
|
消息中间件 Java
springboot整合消息队列——RabbitMQ
springboot整合消息队列——RabbitMQ
74 0
|
3月前
|
消息中间件 JSON Java
RabbitMQ消息队列
RabbitMQ消息队列
45 0
|
3月前
|
消息中间件
RabbitMQ 实现消息队列延迟
RabbitMQ 实现消息队列延迟
121 0
|
16天前
|
消息中间件 存储 负载均衡
消息队列学习之RabbitMQ
【4月更文挑战第3天】消息队列学习之RabbitMQ,一种基于erlang语言开发的流行的开源消息中间件。
15 0
|
1月前
|
消息中间件 存储 中间件
【SpringCloud Stream消息驱动、设计思想以及整合rabbitmq消息队列案例--学习笔记】
【SpringCloud Stream消息驱动、设计思想以及整合rabbitmq消息队列案例--学习笔记】
47 0