详解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=trueorg.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><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 之 Active MQ的安全机制的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

热门话题

在Windows11操作系统中,安全中心是一个重要的功能,它帮助用户监控系统安全状态、防御恶意软件和保护个人隐私。然而,有时候用户可能需要临时关闭安全中心,例如在安装某些软件或进行系统调优时。本文将详细介绍Windows11安全中心的关闭方法,帮助您正确和安全地操作系统。1.如何关闭Windows11安全中心在Windows11中,关闭安全中心并不

Windows操作系统作为全球用户数量最庞大的操作系统之一,一直以来备受用户青睐。然而,在使用Windows系统时,用户们可能会遇到诸多安全隐患,如病毒攻击、恶意软件等威胁。为了强化系统安全,Windows系统内置了许多安全保护机制,其中之一便是Windows安全中心的实时保护功能。今天,我们将会详细介绍Windows安全中心实时保护的关闭方法。首先,让我们

在今天的数字化社会中,计算机已经成为我们生活中不可或缺的一部分。而作为最为普及的操作系统之一,Windows系统在全球范围内被广泛使用。然而,随着网络攻击手段的不断升级,保护个人计算机安全变得尤为重要。Windows操作系统提供了一系列的安全功能,其中“Windows安全中心”是其重要组成部分之一。在Windows系统中,“Windows安全中心”可帮助我们

通过平衡安全需求和业务需求,Java框架设计可实现安全:识别关键业务需求,优先考虑相关安全要求。制定弹性安全策略,分层应对威胁,定期调整。考虑架构灵活性,支持业务演变,抽象安全功能。优先考虑效率和可用性,优化安全措施,提高可见性。

为保护Struts2应用程序,可以使用以下安全配置:禁用未使用的功能启用内容类型检查验证输入启用安全令牌防止CSRF攻击使用RBAC限制基于角色的访问

Slim和Phalcon在PHP微框架的安全性对比中,Phalcon内置有CSRF和XSS防护、表单验证等安全特性,而Slim缺乏开箱即用的安全特性,需手动实施安全措施。对于安全至关重要的应用程序,Phalcon提供了更全面的保护,是更好的选择。

生成性AI的快速发展在隐私和安全方面带来了前所未有的挑战,引发了对监管干预的紧迫呼吁。上周,我有机会在华盛顿特区与一些国会议员及其工作人员讨论AI与安全相关的影响。今天的生成性AI让我想起80年代末的互联网,基础研究、潜在潜力和学术用途,但它还没有为公众做好准备。这一次,不受约束的供应商野心,受到小联盟风险资本的推动和Twitter回声室的激励,正在快速推进AI的“美丽新世界”。“公共”基础模型存在缺陷,不适用于消费者和商业用途;隐私抽象,即使存在,也像筛子一样泄漏;安全结构非常重要,因为攻击面
