ActiveMQ 認證與授權


使用ActiveMQ自帶simpleAuthenticationPlugin

1.直接將用戶名密碼寫入activemq.xml文件

<plugins>
     <simpleAuthenticationPlugin>
            <users>
                  <authenticationUser username="username" password="password" groups="users, admins" />
            </users>
     </simpleAuthenticationPlugin>
 </plugins>

2.使用credentials.properties存儲明文憑證

<plugins>
     <simpleAuthenticationPlugin>
           <users>
                <authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users, admins" />
           </users>
     </simpleAuthenticationPlugin>
</plugins>

vim credentials.properties
    activemq.username=username
    activemq.password=password

activemq.xml 頂部導入了file:${activemq.conf}/credentials.properties,我們可以使用變量的方式引用該文件內部的屬性
groups="users, admins" 表示定義用戶所屬的用戶組, activemq按照用戶組分配權限,注意不能刪除groups屬性,可以置空

3.使用credentials-enc.properties存儲加密憑證

ActiveMQ允許我們對憑證加密后存儲在配置文件中,雖然更加安全但也麻煩一些,這里根據實驗結果對官方文檔給出的方法進行梳理

# --- 創建加密憑證
bin/activemq encrypt --password activemq --input password
# --password 憑證加密密碼 --input 憑證原文
# 返回內容:
# ...
# Encrypted text: FJnN6inNmqDigYEs4wDgkwbe3l2B7mQr
# 解密過程相反,但要輸入相同的憑證加密密碼
bin/activemq decrypt  --password activemq --input FJnN6inNmqDigYEs4wDgkwbe3l2B7mQr
# 返回內容:
# ...
# Decrypted text: password

# --- 將加密憑證寫入credentials-enc.properties
activemq.username=admin
activemq.password=ENC(FJnN6inNmqDigYEs4wDgkwbe3l2B7mQr)
# 格式上注意使用ENC()包裹加密憑證

# --- 修改activemq.xml頂部的配置導入部分
    <!-- Allows us to use system properties as variables in this configuration file -->
    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:${activemq.conf}/credentials.properties</value>
        </property>
    </bean> -->

    <bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig">
        <property name="algorithm" value="PBEWithMD5AndDES" />
        <property name="passwordEnvName" value="ACTIVEMQ_ENCRYPTION_PASSWORD" />
    </bean>

    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="config" ref="environmentVariablesConfiguration" />
    </bean>

    <bean id="propertyConfigurer" class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor" />
        <property name="location" value="file:${activemq.base}/conf/credentials-enc.properties"/>
    </bean>
# 注釋掉原有的credentials.properties導入部分,新增三個bean,三個bean的意思是從環境變量ACTIVEMQ_ENCRYPTION_PASSWORD中加載憑證加密密碼,然后對credentials-enc.properties中的加密憑證解密,所以啟動mq之前還需要設置環境變量。
# 也可以直接將加密密碼寫在配置文件中:
    <!-- Allows us to use system properties as variables in this configuration file -->
    <!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:${activemq.conf}/credentials-enc.properties</value>
        </property>
    </bean> -->

    <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor">
        <property name="algorithm" value="PBEWithMD5AndDES"/>
        <property name="password" value="activemq"/>
    </bean>

    <bean id="propertyConfigurer" class="org.jasypt.spring31.properties.EncryptablePropertyPlaceholderConfigurer">
        <constructor-arg ref="configurationEncryptor" />
        <property name="location" value="file:${activemq.base}/conf/credentials-enc.properties"/>
    </bean>
# 將原來三個bean中前兩個替換成新的bean

# --- 啟動MQ實例
# 如果采用環境變量方式存儲憑證加密密碼,那么這里要設置一下
export ACTIVEMQ_ENCRYPTION_PASSWORD=activemq
bin/activemq start
unset ACTIVEMQ_ENCRYPTION_PASSWORD
# 如果直接將憑證加密密碼寫入配置文件,那么這里直接啟動實例即可

安全這件事強調一萬遍也不為過,但如果可以攻破服務器直接看到配置文件的話那問題一定不是MQ的。

使用JAASAuthentication Plugin

JAAS 全稱為Java Authentication and Authorization Service JAVA認證授權服務
JAASAuthentication Plugin依賴標准的JAAS機制來實現認證,默認使用login.config文件作為配置

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

login.config配置中引用了users.properties和groups.properties分別進行用戶名密碼和用戶組配置

vim users.properties
    username=password
vim groups.properties
    groups=username1, username2...

在activemq.xml添加JAASAuthentication Plugin配置

<plugins>
    <jaasAuthenticationPlugin configuration="activemq" />
</plugins>

configuration="activemq" 指向login.config中配置的名稱

使用authorizationPlugin進行授權

activemq可以對Queue,Topic的讀寫創建等進行權限控制,權限按照用戶組分配

<plugins>
    <authorizationPlugin>
            <map>
                  <authorizationMap>
                       <authorizationEntries>
                            <authorizationEntry queue="activemq.>" read="users" write="users" admin="users"/>
                            <authorizationEntry topic="USER.>" read="admins" write="admins" admin="admins"/>
                       </authorizationEntries>
                   </authorizationMap>
             </map>
    </authorizationPlugin>
</plugins>

> 表示通配符 上述配置表明只有users組才能讀寫創建activemq.開頭的隊列,只有admins組才能讀寫創建USER.開頭的主題
值的注意的是,"activemq.>"中通配符前的點符號必須存在否則無法生效,也就是說前綴必須以點號結尾
> 也可用 * 替換


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM