除了监视台可以设置用户名和密码外(在conf/jetty.xml中设置),ActiveMQ也可以对各个主题和队列设置用户名和密码(客户端访问broker安全设置)、
1、简单认证插件
SimpleAuthentication Plugin适用于简单的认证需求,或者用于建立测试环境。它允许在XML配置文件中指定用户、用户组和密码等信息。(无法控制到具体的主题队列)
在credentials.properties文件中设置用户名和密码。通过credentials-enc.properties可以对用户名密码进行加密。官方文档地址: http://activemq.apache.org/encrypted-passwords.html
1)credentials.properties:(activemq的conf下)
- activemq.username=system
- activemq.password=manager
- user.username=zhhgtmq
- user.password=123456
- guest.password=password
2)在activemq.xml文件systemUsage标签之前加上
- <plugins>
- <simpleAuthenticationPlugin>
- <users>
- <authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
- <authenticationUser username="${user.username}" password="${user.password}" groups="users"/>
- <authenticationUser username="guest" password="${guest.password}" groups="guests"/>
- </users>
- </simpleAuthenticationPlugin>
- </plugins>
2、JAAS认证插件
JAAS(Java Authentication and Authorization Service)也就是java的验证Authentication)、授权(Authorization)服务。简单来说,验证Authentication就是要验证一个用户的有效性,即用户名、密码是否正确。
授权Authorization就是授予用户某种角色,可以访问哪些资源。JAASAuthentication Plugin依赖标准的JAAS机制来实现认证。通常情况下,你需要通过设置java.security.auth.login.config系统属性来配置login modules的配置文件。如果没有指定这个系统属性,那么JAAS Authentication Plugin会缺省使用login.config作为文件名。
1)login.config:
在conf下简历login.config文件,内容:
- activemq {
- org.apache.activemq.jaas.PropertiesLoginModule required
- org.apache.activemq.jaas.properties.user="users.properties"
- org.apache.activemq.jaas.properties.group="groups.properties";
- };
2)users.config
在conf下简历users.config文件,内容:
- system=manager
- user=password
- guest=password
3)groups.properties:
在conf下简历groups.config文件,内容:
4)在activemq.xml文件systemUsage标签之前加上
- <plugins>
-
- <jaasAuthenticationPlugin configuration="activemq" />
-
-
- <authorizationPlugin>
- <map>
- <authorizationMap>
- <authorizationEntries>
- <authorizationEntry queue=">" read="admins" write="admins" admin="admins" />
- <authorizationEntry queue="USERS.>" read="users" write="users" admin="users" />
- <authorizationEntry queue="GUEST.>" read="guests" write="guests,users" admin="guests,users" />
-
- <authorizationEntry topic=">" read="admins" write="admins" admin="admins" />
- <authorizationEntry topic="USERS.>" read="users" write="users" admin="users" />
- <authorizationEntry topic="GUEST.>" read="guests" write="guests,users" admin="guests,users" />
-
- <authorizationEntry topic="ActiveMQ.Advisory.>" read="guests,users" write="guests,users" admin="guests,users"/>
- </authorizationEntries>
-
- </authorizationMap>
- </map>
- </authorizationPlugin>
- </plugins>
针对不同的queue或者topic设置了可以进行操作的组。里面主要涉及三种操作:read, write, admin
read:可以从queue或者topic里面接收消息
write:可以向queue或者topic发送消息
admin:可以创建queue或者topic(可能还有别的功能)
这些文件配制好时候,ActiveMQ就具有了基本的安全机制,当Client(生产者和消费者)连接ActiveMQ需要使用账号,还可以限制具体的Client对于某个/某些Topic/Queue的操作权限。
例如:
<authorizationEntry queue=">" read="admins" write="admins" admin="admins" />。 ">"是通配符的意思,也就是admins组的角色,拥有read、write、admin的权限。
<authorizationEntry queue="USERS.>" read="users" write="admins" admin="admins" /> 。queue名称以"USERS."开头的,users组只拥有读权限,即只能收消息,不能发消息。
补充:activemq目录下文件说明
- activemq.xml
- broker.ks
- broker.ts
- broker-localhost.cert
- client.ks
- client.ts
- credentials.properties //broker连接使用的账号密码文件,明文密码
- credentials-enc.properties broker连接使用的账号密码文件,加密的密码
- groups.properties
- jetty.xml
- jetty-realm.properties //web console访问的账号密码
- jmx.access //访问控制文件,用于限制JMX访问权限
- jmx.password //JMX访问密码文件,用于设置JMX访问的密码
- log4j.properties
- logging.properties
- login.config //JAAS认证使用的配置文件,用于指定使用users.properties和groups.properties文件