色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Spring Boot ActiveMQ如何設(shè)置訪問密碼

瀏覽:146日期:2023-08-29 17:56:31

Apache ActiveMQ是Apache出品,是最流行的,能力很強(qiáng)的開源消息總線。默認(rèn)情況下,程序連接ActiveMQ是不需要密碼的,為了安裝起見,需要設(shè)置密碼,提高安全性。本文分享如何設(shè)置訪問ActiveMQ的賬號密碼。

小編使用的ActiveMQ版本是apache-activemq-5.15.13。

一、設(shè)置控制臺管理密碼

ActiveMQ使用的是jetty服務(wù)器,找到 ActiveMQ安裝目錄下的confjetty.xml文件:

<bean class='org.eclipse.jetty.util.security.Constraint'> <property name='name' value='BASIC' /> <property name='roles' value='admin' /> <!-- set authenticate=false to disable login --> <property name='authenticate' value='true' /></bean>

注意:authenticate的屬性默認(rèn)為'true',登錄管理界面時需要輸入賬戶和密碼;如果是“false”,需要改為'true'。

修改管理界面登錄時的用戶名和密碼,在conf/jetty-realm.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 users that can access the web (console, demo, etc.)# username: password [,rolename ...]# admin: admin, admin# user: user, userwiener: wiener1237, admin

配置信息按順序解釋,分別是:用戶名、密碼、角色名

二、消息生產(chǎn)者和消費者密碼認(rèn)證

在confactivemq.xml中broker 標(biāo)簽最后添加生產(chǎn)者和消費者密碼認(rèn)證信息:

<!-- destroy the spring context on shutdown to stop jetty --> <shutdownHooks> <bean xmlns='http://www.springframework.org/schema/beans' /> </shutdownHooks> <!-- add plugins --> <plugins> <simpleAuthenticationPlugin><users> <authenticationUser username='${activemq.username}' password='${activemq.password}' groups='users,admins'/></users> </simpleAuthenticationPlugin> </plugins> </broker>

activemq.username和activemq.password的值在文件credentials.properties中配置,見如下步驟。

設(shè)置用戶名密碼,文件在confcredentials.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=passwordactivemq.username=wieneractivemq.password=wiener1237guest.password=password

三、Java端配置用戶名密碼

驗證代碼是在《【Spring Boot】ActiveMQ 發(fā)布/訂閱消息模式介紹》的基礎(chǔ)上做重構(gòu),除了新增類ActiveMQConfig之外,修改部分均用紅色字體標(biāo)注。配置application.properties連接信息:

## URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`# failover:(tcp://localhost:61616,tcp://localhost:61617)# tcp://localhost:61616spring.activemq.broker-url=tcp://localhost:61616spring.activemq.in-memory=truespring.activemq.pool.enabled=false#默認(rèn)值false,表示point to point(點到點)模式,true時代表發(fā)布訂閱模式,需要手動開啟spring.jms.pub-sub-domain=truespring.activemq.user=wienerspring.activemq.password=wiener1237

在項目中配置 ActiveMQ連接屬性,新增ActiveMQConfig類:

import org.apache.activemq.ActiveMQConnectionFactory;import org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.jms.config.DefaultJmsListenerContainerFactory;import org.springframework.jms.config.JmsListenerContainerFactory;/*** 配置 ActiveMQ** @author east7* @date 2020/6/23 11:27*/@Configurationpublic class ActiveMQConfig { @Value('${spring.activemq.user}') private String usrName; @Value('${spring.activemq.password}') private String password; @Value('${spring.activemq.broker-url}') private String brokerUrl; @Bean public ActiveMQConnectionFactory connectionFactory() { System.out.println('password =========== ' + password); return new ActiveMQConnectionFactory(usrName, password, brokerUrl); } /** * 設(shè)置點對點模式,和下面的發(fā)布訂閱模式二選一即可 * @param connectionFactory * @return */ @Bean public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){ DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory(); bean.setConnectionFactory(connectionFactory); return bean; } @Bean public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){ //設(shè)置為發(fā)布訂閱模式, 默認(rèn)情況下使用生產(chǎn)消費者方式 DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory(); bean.setPubSubDomain(true); bean.setConnectionFactory(connectionFactory); return bean; }}

bean.setPubSubDomain(true)配置會覆蓋properties文件中spring.jms.pub-sub-domain的屬性值,故可以在properties不設(shè)置spring.jms.pub-sub-domain屬性。另外,這種配置方式可以在系統(tǒng)中同時使用點對點和發(fā)布/訂閱兩種消息模式。修改訂閱者:

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;import javax.jms.JMSException;/*** 消費者*/@Componentpublic class Subscriber1 { private static Logger logger = LoggerFactory.getLogger(Subscriber1.class); /** * 訂閱 topicListener1,僅僅加入containerFactory即可 * * @param text * @throws JMSException */ @JmsListener(destination = 'topicListener1', containerFactory = 'jmsListenerContainerTopic') public void subscriber(String text) { logger.info('Subscriber1 收到的報文:{}', text); }}

containerFactory 的值 'jmsListenerContainerTopic' 會自動匹配到ActiveMQConfig中的函數(shù)JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory)。 Subscriber2同樣修改即可,代碼省略。如果containerFactory 的值設(shè)置為jmsListenerContainerQueue,則開啟了點到點消息模式。

測試函數(shù)還可以使用topicTest()。下面提供一個新的測試途徑——在controller中測試。新增方法

@Autowiredprivate Publisher publisher;@GetMapping('/sendTopicMsg')public String sendTopicMsg(String msg) { // 指定消息發(fā)送的目的地及內(nèi)容 Destination destination = new ActiveMQTopic('topicListener2'); for (int i = 0; i < 8; i++) { publisher.publish(destination, msg + i); } return msg + ' 發(fā)送完畢';}

執(zhí)行結(jié)果省略。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久精品免费播放 | 亚洲在线观看 | 一级毛片不卡免费看老司机 | 国产欧美一区二区三区沐欲 | 草草影院ccyy | 欧美日韩顶级毛片www免费看 | 黄色香蕉视频 | 久久久久久综合对白国产 | 99香蕉网 | 免费人成观看在线网 | 日本一级大毛片a一 | 毛片在线播放网站 | 噜噜噜狠狠夜夜躁精品 | 国产精品揄拍一区二区久久 | 欧美日韩亚洲综合另类ac | 国产大乳喷奶水在线看 | 精品欧美一区二区三区在线 | 美女扒开腿让男人桶尿口 | 97视频免费观看 | 最新国产三级在线观看不卡 | 成人性免费视频 | 欧美成人午夜毛片免费影院 | 久久精品无遮挡一级毛片 | 中文国产成人精品久久无广告 | 国产精品外围在线观看 | 亚洲国产一区二区三区四区五区 | 亚洲区一区 | 国产精品私人玩物在线观看 | 欧美日韩一区二区不卡三区 | 波多野结衣被强在线视频 | 99热久久国产精品这 | 久久一日本道色综合久久m 久久伊人成人网 | 久久久久久久久久久福利观看 | 美女成人网 | 欧美精品一区二区三区在线 | 毛片在线免费观看网站 | 免费观看情趣v视频网站 | 国产视频自拍一区 | 一本大道香蕉久在线不卡视频 | 国产成人亚洲综合一区 | 久久99热只有视精品6国产 |