电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

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

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

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

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

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

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

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',登錄管理界面時(shí)需要輸入賬戶和密碼;如果是“false”,需要改為'true'。

修改管理界面登錄時(shí)的用戶名和密碼,在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)者和消費(fèi)者密碼認(rèn)證

在confactivemq.xml中broker 標(biāo)簽最后添加生產(chǎn)者和消費(fèi)者密碼認(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端配置用戶名密碼

驗(yàn)證代碼是在《【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(點(diǎn)到點(diǎn))模式,true時(shí)代表發(fā)布訂閱模式,需要手動(dòng)開啟spring.jms.pub-sub-domain=truespring.activemq.user=wienerspring.activemq.password=wiener1237

在項(xiàng)目中配置 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è)置點(diǎn)對(duì)點(diǎn)模式,和下面的發(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)消費(fèi)者方式 DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory(); bean.setPubSubDomain(true); bean.setConnectionFactory(connectionFactory); return bean; }}

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

import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.jms.annotation.JmsListener;import org.springframework.stereotype.Component;import javax.jms.JMSException;/*** 消費(fèi)者*/@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 收到的報(bào)文:{}', text); }}

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

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

@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é)果省略。

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 哈尔滨京科脑康神经内科医院-哈尔滨治疗头痛医院-哈尔滨治疗癫痫康复医院 | 大型果蔬切片机-水果冬瓜削皮机-洗菜机切菜机-肇庆市凤翔餐饮设备有限公司 | 钢托盘,铁托盘,钢制托盘,镀锌托盘,饲料托盘,钢托盘制造商-南京飞天金属13260753852 | 焦作网 WWW.JZRB.COM | 全自动过滤器_反冲洗过滤器_自清洗过滤器_量子除垢环_量子环除垢_量子除垢 - 安士睿(北京)过滤设备有限公司 | 地磅-电子地磅维修-电子吊秤-汽车衡-无人值守系统-公路治超-鹰牌衡器 | H型钢切割机,相贯线切割机,数控钻床,数控平面钻,钢结构设备,槽钢切割机,角钢切割机,翻转机,拼焊矫一体机 | 注塑机-压铸机-塑料注塑机-卧式注塑机-高速注塑机-单缸注塑机厂家-广东联升精密智能装备科技有限公司 | 艾乐贝拉细胞研究中心 | 国家组织工程种子细胞库华南分库 | 广州番禺搬家公司_天河黄埔搬家公司_企业工厂搬迁_日式搬家_广州搬家公司_厚道搬迁搬家公司 | 户外健身路径_小区健身器材_室外健身器材厂家_价格-浩然体育 | 山东led显示屏,山东led全彩显示屏,山东LED小间距屏,临沂全彩电子屏-山东亚泰视讯传媒有限公司 | 不锈钢监控杆_监控立杆厂家-廊坊耀星光电科技有限公司 | 高柔性拖链电缆-聚氨酯卷筒电缆-柔性屏蔽电缆厂家-玖泰电缆 | 冷却塔厂家_冷却塔维修_冷却塔改造_凉水塔配件填料公司- 广东康明节能空调有限公司 | 烟台螺纹,烟台H型钢,烟台钢材,烟台角钢-烟台市正丰金属材料有限公司 | 北京乾茂兴业科技发展有限公司| 中医中药治疗血小板减少-石家庄血液病肿瘤门诊部 | 煤机配件厂家_刮板机配件_链轮轴组_河南双志机械设备有限公司 | 2025第九届世界无人机大会| 电缆接头-防爆电缆接头-格兰头-金属电缆接头-防爆填料函 | 炒货机-炒菜机-炒酱机-炒米机@霍氏机械 | 电磁流量计_智能防腐防爆管道式计量表-金湖凯铭仪表有限公司 | 钢衬四氟管道_钢衬四氟直管_聚四氟乙烯衬里管件_聚四氟乙烯衬里管道-沧州汇霖管道科技有限公司 | 100国际学校招生 - 专业国际学校择校升学规划 | 云南成考网_云南成人高考报名网| 结晶点测定仪-润滑脂滴点测定仪-大连煜烁 | 中医中药治疗血小板减少-石家庄血液病肿瘤门诊部 | 青岛美佳乐清洁工程有限公司|青岛油烟管道清洗|酒店|企事业单位|学校工厂厨房|青岛油烟管道清洗 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 蒸压釜_蒸养釜_蒸压釜厂家-山东鑫泰鑫智能装备有限公司 | 冷却塔风机厂家_静音冷却塔风机_冷却塔电机维修更换维修-广东特菱节能空调设备有限公司 | 【德信自动化】点胶机_全自动点胶机_自动点胶机厂家_塑料热压机_自动螺丝机-深圳市德信自动化设备有限公司 | 油罐车_加油机_加油卷盘_加油机卷盘_罐车人孔盖_各类球阀_海底阀等车用配件厂家-湖北华特专用设备有限公司 | 杭州厂房降温,车间降温设备,车间通风降温,厂房降温方案,杭州嘉友实业爽风品牌 | 雪花制冰机(实验室雪花制冰机)百科 | 九爱图纸|机械CAD图纸下载交流中心| 钢托盘,铁托盘,钢制托盘,镀锌托盘,饲料托盘,钢托盘制造商-南京飞天金属13260753852 | 电加热导热油炉-空气加热器-导热油加热器-翅片电加热管-科安达机械 | 生物制药洁净车间-GMP车间净化工程-食品净化厂房-杭州波涛净化设备工程有限公司 | 我爱古诗词_古诗词名句赏析学习平台| 旗帜网络笔记-免费领取《旗帜网络笔记》电子书|