ActiveMQ的安全验证配置存在的原因是:
我们在自己的服务器上假设avtivemq 消息代理,如果不加入安全机制的话,任何连入internet的人,只要知道消息服务的具体地址(包括ip,端口,消息地址[队列或者主题地址],),都可以肆无忌惮的发送、接收消息。想象如果没有安全机制,这将会引起上么样的后果。
主要是配置activemq.xml
在activemq.xml中(下面加粗部分为新增部分)
...
<transportConnectors>
<transportConnector name="openwire" uri="tcp://localhost:61616" discoveryUri="multicast://default"/>
<transportConnector name="ssl" uri="ssl://localhost:61617"/>
<transportConnector name="stomp" uri="stomp://localhost:61613"/>
<transportConnector name="xmpp" uri="xmpp://localhost:61222"/>
</transportConnectors>
<plugins>
<simpleAuthenticationPlugin>
<!--该部分是配置发送或接受的用户名和密码,以及所处的用户组-->
<users>
<authenticationUser username="system" password="manager" groups="users,admins"/>
<authenticationUser username="user" password="password" groups="users"/>
<authenticationUser username="guest" password="password" groups="guests"/>
</users>
</simpleAuthenticationPlugin>
<!-- use JAAS to authenticate using the login.config file on the classpath to configure JAAS -->
<!--以下采用的是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 queue="USERS.>"
read="users" write="users" admin="users" />
<authorizationEntry queue="GUEST.>"
read="guests" write="guests,users" admin="guests,users" />
<authorizationEntry topic=">" read="admins,users"
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>
<!-- let's assign roles to temporary destinations. comment this entry if we don't want any roles assigned to temp destinations -->
<tempDestinationAuthorizationEntry>
<tempDestinationAuthorizationEntry
read="tempDestinationAdmins" write="tempDestinationAdmins"
admin="tempDestinationAdmins" />
</tempDestinationAuthorizationEntry>
</authorizationMap>
</map>
</authorizationPlugin>
</plugins>
...
把红色部分加如到activemq.xml中
然后你在生产者和发送者连接的地方:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("guest", "password", url);
该连接表示用户以guest用户登陆
标记你的连接用户名和密码,activemq会根据你的用户来判断该用户拥有什么权限