深入浅出 JMS(三) - ActiveMQ 安全机制
深入浅出 JMS(三) - ActiveMQ 安全机制
一、认证
认证(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>
代码中的认证方式两种:
在创建Connection的时候认证
//用户认证
Connection conn = connFactory.createConnection("admin", "password");
也可以在创建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、将该配置插入到 conf/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、在代码中的认证方式与 simpleAuthenticationPlugin 相同。
二、授权
基于认证的基础上,可以根据实际用户角色来授予相应的权限,如有些用户有队列写的权限,有些则只能读等等。
两种授权方式
(一)目的地级别授权
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>
<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>
深入浅出 JMS(三) - ActiveMQ 安全机制的更多相关文章
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...
- 深入浅出 JMS(四) - ActiveMQ 消息存储
深入浅出 JMS(四) - ActiveMQ 消息存储 一.消息的存储方式 ActiveMQ 支持 JMS 规范中的持久化消息与非持久化消息 持久化消息通常用于不管是否消费者在线,它们都会保证消息会被 ...
- 深入浅出 JMS(二) - ActiveMQ 入门指南
深入浅出 JMS(二) - ActiveMQ 入门指南 上篇博文深入浅出 JMS(一) – JMS 基本概念,我们介绍了消息通信的规范JMS,这篇博文介绍一款开源的 JMS 具体实现-- Active ...
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 上篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了消息通信的规范JM ...
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...
- 深入浅出 JMS(三) - ActiveMQ 消息传输
深入浅出 JMS(三) - ActiveMQ 消息传输 一.消息协商器(Message Broker) broke:消息的交换器,就是对消息进行管理的容器.ActiveMQ 可以创建多个 Broker ...
- ActiveMQ持久化机制和JMS可靠消息
1.ActiveMQ持久化机制 1.1 JDBC将数据持久化到数据库 1.2 AMQ生成日志文件 1.3 KahaDB:本次磁盘生成数据文件(默认) 1.4 LevelDB:谷歌K/V数据库 1.5 ...
- 深入浅出Java并发包—锁机制(三)
接上文<深入浅出Java并发包—锁机制(二)> 由锁衍生的下一个对象是条件变量,这个对象的存在很大程度上是为了解决Object.wait/notify/notifyAll难以使用的问题. ...
- 深入浅出 JMS(一) - JMS 基本概念
深入浅出 JMS(一) - JMS 基本概念 一.JMS 是个什么鬼 JMS 是 Java Message Service 的简称,即 Java 消息服务.什么是消息服务呢,我们来看一下 Oracle ...
随机推荐
- 折腾了好久,thinkphp5打开提示加载failed to open stream: No such file or directory in think start.php
GIT上下载的THINKPHP5记得先 composer update 我就是没update ,折腾了1个小时,才想起来这个事 thinkphp5默认首页打开空白 打开报错提示 提示thinkphp ...
- 1.vue和react的区别
1.个人感觉Vue好用,react不咋地呀. 2.(网上搜的)Vue的解决方案适用于小型应用,但对于对于大型应用而言不太适合.
- Win10交换Ctrl和大写键
打开注册表 [HKEY_LOCAL_MacHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout] "Scancode Map" ...
- leetcode91
class Solution { public int numDecodings(String s) { if(s.length()==0){ return 0; } int[] dp = new i ...
- Spring MVC 视图及视图解析器
org.springframework.web.servlet.view.InternalResoureceViewResolve 把逻辑视图改为物理视图 可混用多种视图 不进过Handler直接进入 ...
- ABAP-索引
转载:http://blog.sina.com.cn/s/blog_498610450101kbxl.html tables: csks. start-of-selection. select * u ...
- PostgreSql别名区分大小写的问题
PostgreSql是区分大小写的,如果别名的大小不一致就会提示错误: SELECT * FROM ( SELECT cpi."product_item_id" "PRO ...
- Numpy统计
Numpy统计 axis=None 是统计函数的标配参数,默认不输入此参数则为对数组每一个元素进行计算,设定轴则对此轴上元素进行计算 1:常用统计函数 .sum(a,axis=None):数组a求和运 ...
- tensor flow 视频
http://v.youku.com/v_show/id_XMTYxMjQ2NTYyNA==.html?spm=a2h1n.8251843.playList.5!19~5~A.siMjNW&f ...
- mysql datetime与timestamp精确到毫秒的问题
CREATE TABLE `tab1` (`tab1_id` VARCHAR(11) DEFAULT NULL,`create` TIMESTAMP(3) NULL DEFAULT NULL,`cre ...