ActiveMQ配置用户认证信息

以 apache-activemq-5.15.13-bin.tar.gz (从清华镜像 下载 )为例,编辑activemq.xml

在 <broker> 节点内增如下xml片段:

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

查看 credentials.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 credentials that will be used by components (like web console) to access the broker

activemq.username=system
activemq.password=manager
guest.password=password

标红的地方则为账户密码

编辑 jetty-realm.properties 配置文件,在最后一行新增 system: manager, admin,如下标红所示

## ---------------------------------------------------------------------------
## 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
user: user, user
system: manager, admin

请保持两个 properties 配置文件中的账户密码配置一致,如需修改请同时修改两个配置文件

重启activemq生效

curl验证

curl -u system:manager --data-urlencode "body=$(date)" http://localhost:8161/api/message/wangrui-queue?type=queue

代码修改:

在定义activemq连接池的时候添加用户密码信息即可,示例如下:(代码中使用的消息服务器使用的是自定义的账户密码)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>
    <!-- 配置生产者连接池 -->
    <bean id="producerConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
        destroy-method="stop">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL">
                    <value>${activemq.url}</value>
                </property>
                <property name="userName">
                    <value>gfstack</value>
                </property>
                <property name="password">
                    <value>gfstack</value>
                </property>
            </bean>
        </property>
        <property name="maxConnections" value="${producer.maxConnections}"></property>
        <property name="maximumActiveSessionPerConnection" value="${producer.maximumActiveSessionPerConnection}"></property>
        <property name="idleTimeout" value="${producer.idleTimeout}"></property>
    </bean>
    
    <!-- 配置消费者连接池 -->
    <bean id="consumerConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
        destroy-method="stop">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL">
                    <value>${activemq.url}</value>
                </property>
                <property name="userName">
                    <value>gfstack</value>
                </property>
                <property name="password">
                    <value>gfstack</value>
                </property>
            </bean>
        </property>
        <property name="maxConnections" value="${consumer.maxConnections}"></property>
        <property name="maximumActiveSessionPerConnection" value="${consumer.maximumActiveSessionPerConnection}"></property>
        <property name="idleTimeout" value="${consumer.idleTimeout}"></property>
    </bean>
</beans>
原文地址:https://www.cnblogs.com/nihaorz/p/13030428.html