6.1Authentication

在ActiveMQ中所有的安全concepts以插件的方式实现。这样允许简单配置和通过ActiveMQ XML 配置文件的<plugin>元素来定制。在ActiveMQ中对于授权用户有两个插件:
■Simple authentication plug-in--在XML配置文件或属性文件中直接地直接操作credential.
■JAAS authentication plug-in--实现JAAS API和提供更强大和定制化的身份认证解决方案。
让我们看看这两种插件吧。
6.1.1配置 simple authentication plug-in
最简单保证代理安全是通过使用授权credential,它被直接放在代理的XML配置文件中。这样的功能通过简单的授权插件来提供,它作为ActiveMQ的有部分。下面所列举的提供了使用这个插件的例子:
Listing 6.1  Configuring the simple authentication plug-in
<broker ...>
<plugins>
<simpleAuthenticationPlugin>
<users>
<!--Four authentication users with their groups -->
<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>
通过使用这个简单的配置片段,4个用户能使用ActiveMQ。显然地,为了授权的目的,每个用户必须有个用户名和密码。除此之外,groups属性提供了一个comma-separated的group列表属性赋值给用户所属的组。这个信息被用作授权的目的,将如我们看到的那样。
最好的理解方法是看看股票示例。首先,代理必须使用预先定义的配置文件来开始:
$ mvn exec:java \
-Dexec.mainClass=org.apache.activemq.book.ch3.portfolio.Publisher \
-Dexec.args="CSCO ORCL"
...
Exception in thread "main"
javax.jms.JMSException: User name or password is invalid.
...
上面的异常如我们所预想的因为安全插件被激活了但是授权credential还没在publisher客户端定义。为了解决这个异常,需要修改publisher来添加用户名和密码。下面提供了一个这样的示例:
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);
}

正如先前片段显示的,唯一需要改变的是定义用户名和密码,它们被用作调用createConnection()参数.编译运行修改过的publisher将出现正常的情况,如下面输出的:
$ mvn exec:java \
-Dexec.mainClass=org.apache.activemq.book.ch6.Publisher
-Dexec.args="CSCO ORCL"
...
Sending: {price=35.25020234334, stock=ORCL, offer=35.28545254568,
up=true} on destination: topic://STOCKS.ORCL
Sending: {price=35.018408299624, stock=ORCL, offer=35.053426707924,
up=false} on destination: topic://STOCKS.ORCL
Sending: {price=34.722966908601, stock=ORCL, offer=34.75768987551,
up=false} on destination: topic://STOCKS.ORCL
Sending: {price=1.651542629939308, stock=CSCO, offer=1.653194172569,
up=true} on destination: topic://STOCKS.CSCO
Sending: {price=34.598719623046, stock=ORCL, offer=34.63331834266,
up=false} on destination:topic://STOCKS.ORCL
Sending: {price=34.43900856142, stock=ORCL, offer=34.47344756998,
up=false} on destination: topic://STOCKS.ORCL
Sending: {price=1.6580787335090, stock=CSCO, offer=1.659736812242,
up=true} on destination: topic://STOCKS.CSCO
Sending: {price=34.458768559093, stock=ORCL, offer=34.49322732765,
up=true} on destination: topic://STOCKS.ORCL
Sending: {price=1.6547727745488, stock=CSCO, offer=1.6564275473233,
up=false} on destination:topic://STOCKS.CSCO
Sending: {price=1.665375738897, stock=CSCO, offer=1.6670411146368,
up=true} on destination: topic://STOCKS.CSCO
Published '10' of '10' price messages
...
注意在输出中,我们的producer成功地连上了代理并发送了消息。
不幸的是,使用simple authentication 插件,密码以明文存储(和传输),它将影响代理的安全性。但是即使明文密码也能阻止未授权的客户端连接代理,并且在一些环境中这就是要的效果。除此之外,你可以考虑使用simple authentication 结合SSL传输,它至少能解决明文密码在网络上的传输问题。
对于ActiveMQ需要去整合一个更安全的安装和/或一个已经存在的安全基础的情况,JAAS 插件可能更合适。
6.1.2配置JAAS插件
若对JAAS详细解释则超出了本书的范围。相对,本节将简要介绍JAAS基本的concepts和demonstrate如何建立一个PropertiesLoginModule,它能被用来实现和简单安全插件相同的功能。更多的关于JAAS的信息见( http://mng.bz/BvvB)
JAAS提供了插件式authentication,它意味着ActiveMQ将使用相同的authentication API而不关心修改用户credential的技术(text文件,relational数据库,LDAP等等)。需要的所有东西是对javax.security.auth.spi.LoginModule( http://mng.bz/8zLV)  接口的实现和一个对ActiveMQ的配置修改。幸运的是,ActiveMQ实现了一些module能授权用户使用properties文件,LDAP和SSL证书,这将足够应付许多使用案例。因为JAAS登录模块符合一个specification,它们的一个优点是配置起来它们相对straightforward。最好的理解方式是亲自配置一下。对于这项工作,和properties文件一起工作的登录模块将被使用到。
第一步是定义PropertiesLoginModule这样ActiveMQ能识别到它。而为了能如此,你必须建立一个文件名叫login.config,它包含了用户配置JAAS用户和组 (http://mng.bz/IIEB)的standardized格式。下面是文件的内容:
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";
};
显示在这里的login.config文件包含了配置JAAS模块的不同的项。activemq-domain是predominant项它包含了配置登录模块的所有配置。第一行是PropertiesLoginModule的完全qualified名和标识它的拖曳符号required。这意味着authentication不能没有login模块。第二行是为登录模块激活debug日志;这是可选项。第三行org.apache.activemq.jaas.properties.user 属性指向users.properties文件。第四行org.apache.activemq.jaas.properties.group 属性指向groups.properties 文件。一旦所有都定义了,这两个properies文件必须都已建立了。
注意:在本节中使用的PropertiesLoginModule是JAAS登录模块的一个实现,it comes with ActiveMQ。
在properties文件中定义用户的credential很简单。users.properties文件安行定义每个用户和它的密码,如下面所示:
admin=password
publisher=password
consumer=password
guest=password
groups.properties文件也以行的格式定义组。但是每一组包含comma-separated列表的它的用户,如下面所示:
admins=admin
publishers=admin,publisher
consumers=admin,publisher,consumer
guests=guest
一旦这里的文件被建立,JAAS插件必须在ActiveMQ的XML配置文件中定义。下面的是一个示例,显示该必要的修改:
...
<plugins>
    <jaasAuthenticationPlugin configuration="activemq-domain" />
</plugins>
...
为了readability和显示enable JAAS登录模块的必要修改,示例已被简化。如你所见,JAAS插件仅需要在 login.config文件中的JAAS domain的名字。ActiveMQ将在classpath(另外一种方法是为login.config file的位置使用java.security.auth.login.config system property)中定位。为了测试刚建立的JAAS登录模块,使用这些修改启动ActiveMQ。下面是使用到的命令:
${ACTIVEMQ_HOME}/bin/activemq console \
-Djava.security.auth.login.config=\
src/main/resources/org/apache/activemq/book/ch6/login.config \
xbean:src/main/resources/org/apache/activemq/book/ch6/activemq-jaas.xml
...
Loading message broker from:
xbean:src/main/resources/org/apache/activemq/book/ch6/activemq-jaas.xml
INFO | PListStore:
/Users/bsnyder/amq/apache-activemq-5.4.1/data/localhost/tmp_storage
started
INFO | Using Persistence Adapter: KahaDBPersistenceAdapter
[/Users/bsnyder/amq/apache-activemq-5.4.1/data/localhost/KahaDB]
INFO | JMX consoles can connect to service:
jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
INFO | ActiveMQ 5.4.1 JMS Message Broker (localhost) is starting
INFO | For help or more information please see:
http://activemq.apache.org/
INFO | Scheduler using directory:
/Users/bsnyder/amq/apache-activemq-5.4.1/data/localhost/scheduler
INFO | JobSchedulerStore:
/Users/bsnyder/amq/apache-activemq-5.4.1/data/localhost/scheduler
started
INFO | Listening for connections at: tcp://localhost:61616
INFO | Connector openwire Started
INFO | ActiveMQ JMS Message Broker
(localhost, ID:mongoose.local-61955-1289966951514-0:0) started
如前面一节使用简单authentication一样代理已被secured,眼下JAAS standard被使用了。现在我们启动stock portfolio的publisher并使用合适的credential并预期它能进入代理:
mvn exec:java \
-Dexec.mainClass=org.apache.activemq.book.ch6.Publisher \
-Dexec.args="CSCO ORCL"
...
Sending: {price=44.84266119470, stock=ORCL, offer=44.88750385590,
up=true} on destination: topic://STOCKS.ORCL
Sending: {price=44.5575471806, stock=ORCL, offer=44.60210472778,
up=false} on destination: topic://STOCKS.ORCL
Sending: {price=44.49794307251, stock=ORCL, offer=44.54244101559,
up=false} on destination: topic://STOCKS.ORCL
Sending: {price=44.48574009628, stock=ORCL, offer=44.530225836380,
up=false} on destination: topic://STOCKS.ORCL
Sending: {price=55.89763705357, stock=CSCO, offer=55.953534690630,
up=true} on destination: topic://STOCKS.CSCO
Sending: {price=44.09643970531, stock=ORCL, offer=44.140536145020,
up=false} on destination: topic://STOCKS.ORCL
Sending: {price=44.20879151845, stock=ORCL, offer=44.25300030997,
up=true} on destination: topic://STOCKS.ORCL
Sending: {price=44.38257378288, stock=ORCL, offer=44.426956356664,
up=true} on destination: topic://STOCKS.ORCL
Sending: {price=44.660334580924, stock=ORCL, offer=44.704994915505,
up=true} on destination: topic://STOCKS.ORCL
Sending: {price=44.77852477644, stock=ORCL, offer=44.8233033012,
up=true} on destination: topic://STOCKS.ORCL
Published '10' of '10' price messages
...
如我们所见,JAAS插件准确地提供了如simple authentication插件相同的功能。但是它能做到这样是是使用了standardized Java原理,意味着你能用它嵌入到你的organizatin内部的任何存在的安全policy中。
除了能够鉴定进入代理服务的身份,ActiveMQ也能在细微的层级上authorize特别的操作。下一节将彻底探索这个主题。

猜你喜欢

转载自flxchy4.iteye.com/blog/1708565
6.1