当前位置: 首页 >服务端 > ActiveMQ 认证与授权

ActiveMQ 认证与授权

使用ActiveMQ自带simpleAuthenticationPlugin

1.直接将用户名密码写入activemq.xml文件

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

2.使用credentials.properties存储明文凭证

<plugins> <simpleAuthenticationPlugin><users><authenticationUser useame="${activemq.useame}" password="${activemq.password}" groups="users, admins" /></users> </simpleAuthenticationPlugin></plugins>vim credentials.propertiesactivemq.useame=useameactivemq.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.propertiesactivemq.useame=adminactivemq.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=activemqbin/activemq startunset ACTIVEMQ_ENCRYPTION_PASSWORD# 如果直接将凭证加密密码写入配置文件,那么这里直接启动实例即可

安全这件事强调一万遍也不为过,但如果可以攻破服务器直接看到配置文件的话那问题一定不是MQ的。

使用JAASAuthentication Plugin

JAAS 全称为Java Authentication and Authorization Service JAVA认证授权服务
JAASAuthentication Plugin依赖标准的JAAS机制来实现认证,默认使用login.config文件作为配置

vim login.configactivemq {org.apache.activemq.jaas.PropertiesLoginModule requiredorg.apache.activemq.jaas.properties.user="users.properties"org.apache.activemq.jaas.properties.group="groups.properties";};

login.config配置中引用了users.properties和groups.properties分别进行用户名密码和用户组配置

vim users.propertiesuseame=passwordvim groups.propertiesgroups=useame1, useame2...

在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.>"中通配符前的点符号必须存在否则无法生效,也就是说前缀必须以点号结尾
> 也可用 * 替换

作者:Peterer~王勇
来源链接:https://www.cnblogs.com/Peter2014/p/10981986.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/112809.html

标签:ActiveMQ
分享给朋友:

“ActiveMQ 认证与授权” 的相关文章