安全机制

ActiveMQ中所有安全相关的概念都是通过插件的形式实现的.这样可以通过ActiveMQ的XML
配置文件的<plugin>元素来简化配置和自定义安全认证机制.ActiveMQ提供两种认证方式:

简单认证插件 -- 直接通过XML配置文件或者属性文件处理认证
JAAS认证插件 -- 实现了JAAS API,提供一种更强大的可自定义的认证解决方案

[b]简单认证[/b]
<broker ...>
<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>
</broker>


连接代码可能需要变成类似
private String username = "publisher";
private String password = "password";
public Publisher() throws JMSException
{
factory = new ActiveMQConnectionFactory(brokerURL);
connection = factory.createConnection(username, password);
connection.start();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
producer = session.createProducer(null);
}


简单认证插件时,密码存储和传输时都是使用明文,这可能对代理的安全造成隐患.可以将简单认证插件和SSL传输连接器配合起来使用,这样至少可以避免在网络中发送明文形式的密码.


[b]JAAS插件[/b]
需要login.config文件,也可以指定JVM的java.security.auth.login.config属性,来指定文件名,如
-D java.security.auth.login.config=src/main/resources/org/apache/activemq/book/ch6/login.config

基本内容可以是
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";
};

activemq-domain模块内容是主要属性。
第一 org.apache.activemq.jaas.PropertiesLoginModule required debug=true,指定了实现类是org.apache.activemq.jaas.PropertiesLoginModule,required表示验证需要加载该类后进行,否则无法进行,debug=true登陆模块开启调试日志配置
第二个和第三个表示用户和组分别读取文件。

users.properties中每一行定义一个用户,使用用户名=密码的格式,如下所示:

admin=password
publisher=password
consumer=password
guest=password

groups.properties中同样每一行定义一个群组.但是群组=后面是一组通过逗号分割的用户名,
表示这些用户属于该群组,如下所示:

admins=admin
publishers=admin,publisher
consumers=admin,publisher,consumer
guests=guest

activemq配置文件的插件部分修改为
<plugins>
<jaasAuthenticationPlugin configuration="activemq-domain" />
</plugins>

其他的实现可以再activemq中找到。

[b]授权[/b]
有两种机制
操作级别授权和消息级别授权.这两者授权方式提供了
比简单认证方式更细致的访问控制

目的地级别的授权
针对JMS消息目的地一共有三种用户操作权限:
读 -- 具有从特定目的地接收消息的权限
写 -- 具有发送消息到特定目的地的权限
管理 -- 具有管理消息目的地的权限

示例

<plugins>
<jaasAuthenticationPlugin configuration="activemq-domain" />
<authorizationPlugin>
<map>
<authorizationMap>
<authorizationEntries>
<authorizationEntry topic=">" read="admins" write="admins" admin="admins" />
<authorizationEntry topic="STOCKS.>" read="consumers" write="publishers" admin="publishers" />
<authorizationEntry topic="STOCKS.ORCL" read="guests" />
<authorizationEntry topic="ActiveMQ.Advisory.>"
read="admins,publishers,consumers,guests"
write="admins,publishers,consumers,guests"
admin="admins,publishers,consumers,guests" />
</authorizationEntries>
</authorizationMap>
</map>
</authorizationPlugin>
</plugins>




[b]消息级别的授权[/b]
消息级别的授权需要通过编码方式实现,实现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;
}
}
}


如上,只有本机的消息允许被发送和访问。
另外还需要把该实现类通过配置文件配置进activemq的配置文件。

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


[b]插件[/b]
两种方式,
1.JAAS安全插件,如上JAAS。

2.自定义插件

先实现过滤器
BrokerFilter

public class IPAuthenticationBroker extends BrokerFilter 
{
List<String> allowedIPAddresses;
Pattern pattern = Pattern.compile("^/([0-9\\.]*):(.*)");

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();
Matcher matcher = pattern.matcher(remoteAddress);
if (matcher.matches())
{
String ip = matcher.group(1);
if (!allowedIPAddresses.contains(ip))
{
throw new SecurityException("Connecting from IP address " + ip + " is not allowed" );
}
}
else
{
throw new SecurityException("Invalid remote address " + remoteAddress);
}
super.addConnection(context, info);
}
}


由于拦截其是链结构的,public IPAuthenticationBroker(Broker next, List<String> allowedIPAddresses)
{
super(next);
this.allowedIPAddresses = allowedIPAddresses;
}

如上构造函数是为了保证自己的拦截器不会破坏原本的链。
接着是实现一个插件,该插件的作用是向Activemq注册过滤器,

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;
}
}


接着是把插件配置进去

<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.base}/data">
<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>

<transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616" />
</transportConnectors>
</broker>


[b]基于证书[/b]
activemq的安全机制实现过证书的授权,更多详情查看最新的activemq文档。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值