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>

 


免責聲明!

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



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