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>

原文地址:https://www.cnblogs.com/Soprano/p/10659581.html

时间: 2024-08-03 19:30:25

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

JMS 之 Active MQ 消息存储

一.消息的存储方式 ActiveMQ支持JMS规范中的持久化消息与非持久化消息 持久化消息通常用于不管是否消费者在线,它们都会保证消息会被消费者消费.当消息被确认消费后,会从存储中删除 非持久化消息通常用于发送通知以及实时数据,通常要求性能优先,消息可靠性并不是必须的情况 MQ支持可插拔式的消息存储,如:内存.文件和关系数据库等方式 Queue消息模型在ActiveMQ的存储 采用存储采用先进先出(FIFO),一个消息只能被一个消费者消费,当消息被确认消费之后才会被删除. Topic消息模型(针

JMS 之 Active MQ 的消息传输

本文使用Active MQ5.6 一.消息协商器(Message Broker) broke:消息的交换器,就是对消息进行管理的容器.ActiveMQ 可以创建多个 Broker,客户端与ActiveMQ交互,实际上都是与ActiveMQ中的Broker交互,Broker配置在${MQ_HOME}\conf\activemq.xml. 二.连接器(Connectors) (一).传输连接器 (transportConnectors) transportConnectors 连接器:就是建立bro

JMS 之 Active MQ 启动嵌入式Broke

一.如何启动active MQ 服务 (一).使用命令启动 a./usr/local/activemq-5.9.0/bin 目录下 ./activemq start 默认使用conf/activemq.xml 配置文件 b.[[email protected] bin]# ./activemq start xbean:file:../conf/activemq-slave1.xml 使用指定的配置文件启动 (二).代码启动broker 在程序中可以通过编码的方式启动broker,如果要启动多个b

JMS 之 Active MQ 的spring整合

一.与spring整合实现ptp的同步接收消息 pom.xml: <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.3.7.RE

Active MQ学习笔记

一.Active MQ介绍 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位. 主要特点: 1. 多种语言和协议编写客户端.语言: Java, C, C++, C#, Ruby, Perl, Python, PHP.应用协议: OpenWire,Stomp REST,WS Notif

Active MQ C#实现

原文链接: Active MQ C#实现 内容概要 主要以源码的形式介绍如何用C#实现同Active MQ 的通讯.本文假设你已经正确安装JDK1.6.x,了解Active MQ并有一定的编程基础. 正文 JMS 程序的最终目的是生产和消费的消息能被其他程序使用,JMS 的 Message 是一个既简单又不乏灵活性的基本格式,允许创建不同平台上符合非JMS 程序格式的消息. Message 由消息头,属性和消息体三部份组成. Active MQ支持过滤机制,即生产者可以设置消息的属性(Prope

JMS进阶-Spring整合Active MQ

只能说,Spring太流弊了,啥都能整合~~~~ First of all, start the service of Active MQ 项目目录结构如下 用到的jar包如下 activemq-client-5.13.1.jar commons-logging-1.1.3.jar geronimo-j2ee-management_1.1_spec-1.0.1.jar geronimo-jms_1.1_spec-1.1.1.jar hamcrest-core-1.3.jar junit-4.12

Active MQ 启动报错

ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位. 特性 ⒈ 多种语言和协议编写客户端.语言: Java,C,C++,C#,Ruby,Perl,Python,PHP.应用协议: OpenWire,Stomp REST,WS Notification,XMPP,AMQP ⒉ 完全支持J

active MQ搭建

ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JMS在当今的J2EE应用中间仍然扮演着特殊的地位. 1安装 定义activemq安装目录为/usr/local/activemq 定义activemq数据存放目录为 /data/postmall/activemq/ cd /tmp wget http://archive.apache