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 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" property name="allowedIPAddresses" list value 127.0.0.1 /value /list /property /bean /plugins
16308.html
cjavaxml相关文章
- 8月业务安全月报 | 多家科技企业遭勒索软件攻击;刷单诈骗暴增;苹果曝严重漏洞
- CSO面对面丨如何通过“联合作战”,加强银行安全体系建设
- 6.Docker镜像与容器安全最佳实践
- 安全在线简历生成器,不试试吗?
- 数据安全新战场,EasyMR为企业筑起“安全防线”
- Nmap安全扫描器
- FunDA(8)- Static Source:保证资源使用安全 – Resource Safety详解编程语言
- java线程安全与共享资源详解编程语言
- Java 单例模式的线程安全实现详解编程语言
- Linux安全机制:保护系统安全的利器.(linux的安全机制)
- “开源的Linux:免费、开放、安全”(linux系统免费么)
- Oracle指定表备份:一种快速安全的方法(oracle备份指定表)
- :红旗Linux:安全高效的开源发行版(红旗linux镜像)
- Oracle 日志:践行安全记录你的行为(oracle有哪些日志)
- Linux账户安全:重要性和实现方法(linux账户密码)
- 256Linux系统下SHA256安全算法的应用(linuxsha)
- 每日安全资讯:黑客论坛被黑,竞争对手公开其数据库
- 管理Oracle 日志组实现安全运行(oracle日志组)
- Linux待机化:让操作简单又安全(linux如何待机)
- MSSQL数据库文件夹——存储宝贵信息的安全宝库(mssql的数据库文件夹)
- Linux系统MD5值校验:安全保障有保障(linuxmd5值)
- 数据库中的应用Oracle数据库中的Alert功能让操作安全更可靠(alert在oracle)
- Shiro整合Redis,实现安全高效的分布式认证(shiro接入redis)
- 配置Redis集群的安全开放如何配置对外端口(redis集群对外端口)
- 基于Redis集群的JWT安全验证(redis集群jwt)