ActiveMQ 5.x 的安全配置
前言:activeMQ分為2個安全配置。一個是web控制臺的安全配置;另外一個是對于隊列/主題的訪問安全配置。
1.控制臺安全配置
ActiveMQ使用的是jetty服務器, 打開conf/jetty.xml文件,找到
<bean id="securityConstraint" class="org.eclipse.jetty.http.security.Constraint"> <property name="name" value="BASIC" /> <property name="roles" value="admin" /> <property name="authenticate" value="false" /> </bean>
將property name為authenticate的屬性value="false" 改為"true",
控制臺的登錄用戶名密碼保存在 conf/jetty-realm.properties文件中,內容如下:
## --------------------------------------------------------------------------- ## Licensed to the Apache Software Foundation (ASF) under one or more ## contributor license agreements. See the NOTICE file distributed with ## this work for additional information regarding copyright ownership. ## The ASF licenses this file to You under the Apache License, Version 2.0 ## (the "License"); you may not use this file except in compliance with ## the License. You may obtain a copy of the License at ## ## http://www.apache.org/licenses/LICENSE-2.0 ## ## Unless required by applicable law or agreed to in writing, software ## distributed under the License is distributed on an "AS IS" BASIS, ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ## See the License for the specific language governing permissions and ## limitations under the License. ## --------------------------------------------------------------------------- # Defines users that can access the web (console, demo, etc.) # username: password [,rolename ...] admin: admin, admin
用戶格式定義: 用戶名:密碼[,角色...] , 以上配置就是用戶名為admin,密碼為admin,角色為admin的用戶
重啟,訪問 http://127.0.0.1:8161/admin/ 將彈出:
要求輸入用戶名密碼
2.對于JMS的安全配置
對于JMS的安全配置這里又介紹兩種。現在先介紹關于“角色/權限”的一個介紹
Authorization
In ActiveMQ we use a number of operations which you can associate with user roles and either individual queues or topics or you can use wildcards to attach to hierarchies of topics and queues.
Operation | Description |
---|---|
read | You can browse and consume from the destination |
write | You can send messages to the destination |
admin | You can lazily create the destination if it does not yet exist. This allows you fine grained control over which new destinations can be dynamically created in what part of the queue/topic hierarchy |
以上是apache官網的描述.這里稍微解釋下.
其實對于隊列/主題的訪問只有3個操作類型,read和write我就不解釋了.而admin是什么呢?這是我們在寫我們自己客戶端程序訪問activeMQ時候,如果隊列/主題不存在.則admin就可指定該"角色"是否有權限建立這個隊列(沒錯,不像weblogic的jms那樣,隊列/主題沒有從后臺建立,則客戶端無法訪問.)
2.1Simple Authentication(簡單的身份驗證)
在conf/activemq.xml文件中加入以下內容即可(如配置了systemUsage,應該放到systemUsage前):
<plugins> <!-- Configure authentication; Username, passwords and groups --> <simpleAuthenticationPlugin> <users> <authenticationUser username="system" password="${activemq.password}" groups="users,admins"/> <authenticationUser username="user" password="${guest.password}" groups="users"/> <authenticationUser username="guest" password="${guest.password}" groups="guests"/> </users> </simpleAuthenticationPlugin> </plugins>
以上占位引用可在 conf/credential.properties中配置
2.2 JAAS身份驗證
a)在conf/activemq.xml文件中加上
<plugins> <!--use JAAS to authenticate using the login.config file on the classpath to configure JAAS --> <jaasAuthenticationPlugin configuration="activemq-domain" /> <!-- lets configure a destination based authorization mechanism --> <authorizationPlugin> <map> <authorizationMap> <authorizationEntries> <!-->表示通配符,例如USERS.>表示以USERS.開頭的主題,>表示所有主題,read表示讀的權限,write表示寫的權限,admin表示角色組--> <authorizationEntry queue=">" read="admins" write="admins" admin="admins" /> <authorizationEntry topic=">" read="admins" write="admins" admin="admins" /> <authorizationEntry queue="ActiveMQ.Advisory.>" read="admins" write="admins" admin="admins" /> <authorizationEntry topic="ActiveMQ.Advisory.>" read="admins" write="admins" admin="admins" /> </authorizationEntries> </authorizationMap> </map> </authorizationPlugin> </plugins>在配置中 ">" 代表所有的意思. 而 "ActiveMQ.Advisory.>" 則代表.名為 "ActiveMQ.Advisory."下的所有.
b)在conf目錄下增加login.config,groups.properties,users.properties
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"; };
groups.properties 內容如下:
#group=userName admins=system
users.properties 內容如下:
#userName=password system=manager
以上兩種配置方式到conf下 activemq-security.xml文件都能看到,看樣子這個就是配置安全的配置文件.
最后,附上官網的配置連接:http://activemq.apache.org/security.html