JMS的安全性

转载:http://shutiao2008.iteye.com/blog/562381

对于JMS服务的安全控制,ActiveMQ提供两种方式:简单授权方式和JAAS授权方式。我们在这里采用简单的授权方式。如果要实现复杂的权限和角色机制,可以使用JAAS方式,这里不予讲述。 

JMS安全的配置,在文件${activemq.base}/conf/activemq-security.xml中有范例。打开该文件,可以看到,有两个plugins:simpleAuthenticationPlugin和authorizationPlugin,后者是JAAS授权方式需要用到的,在这里,我们只需要用到前者。 

编辑${activemq.base}/conf/activemq.xml文件,加入如下一段代码: 

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

${activemq.username}和${activemq.password}是在credentials.properties文件中设置的用户名和密码。该文件位于${activemq.base}/conf/目录下,打开该文件,设置你的用户名和密码即可。 

配置完毕。这样,你的ActiveMQ就需要用户名和密码才能发送和接收JMS消息了。连接ActiveMQ的Java代码相应的改为如下: 
ConnectionFactory cf = new ActiveMQConnectionFactory("myusername", "mypassword", "tcp://192.168.1.109:61616") 
在Spring中,则如下: 
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> 
    <property name="brokerURL" value="tcp://192.168.1.109:61616" /> 
    <property name="userName" value="myusername" /> 
    <property name="password" value="password" /> 
</bean>

猜你喜欢

转载自sagewsg.iteye.com/blog/1609342