Apache ActiveMQ是Apache出品,是最流行的,能力很强的开源消息总线。默认情况下,程序连接ActiveMQ是不需要密码的,为了安装起见,需要设置密码,提高安全性。本文分享如何设置访问ActiveMQ的账号密码。

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

一、设置控制台管理密码

    ActiveMQ使用的是jetty服务器,找到 ActiveMQ安装目录下的\conf\jetty.xml文件:
<bean id="adminSecurityConstraint" 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的属性默认为"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, user
wiener: wiener1237, admin

配置信息按顺序解释,分别是:用户名、密码、角色名

二、消息生产者和消费者密码认证

   在\conf\activemq.xml中broker 标签最后添加生产者和消费者密码认证信息:
        <!-- destroy the spring context on shutdown to stop jetty -->
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
</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中配置,见如下步骤。

   设置用户名密码,文件在\conf\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 activemq.username=wiener
activemq.password=wiener1237
guest.password=password

三、Java端配置用户名密码

验证代码是在《【Spring Boot】ActiveMQ 发布/订阅消息模式介绍》的基础上做重构,除了新增类ActiveMQConfig之外,修改部分均用红色字体标注。配置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:61616
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
#默认值false,表示point to point(点到点)模式,true时代表发布订阅模式,需要手动开启
spring.jms.pub-sub-domain=true
spring.activemq.user=wiener
spring.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
*/
@Configuration
public 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);
} /**
* 设置点对点模式,和下面的发布订阅模式二选一即可
* @param connectionFactory
* @return
*/
@Bean
public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setConnectionFactory(connectionFactory);
return bean;
} @Bean
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){
//设置为发布订阅模式, 默认情况下使用生产消费者方式
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
return bean;
}
}

bean.setPubSubDomain(true)配置会覆盖properties文件中spring.jms.pub-sub-domain的属性值,故可以在properties不设置spring.jms.pub-sub-domain属性。另外,这种配置方式可以在系统中同时使用点对点和发布/订阅两种消息模式。修改订阅者:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component; import javax.jms.JMSException; /**
* 消费者
*/
@Component
public 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中的函数JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory)。  Subscriber2同样修改即可,代码省略。如果containerFactory 的值设置为jmsListenerContainerQueue,则开启了点到点消息模式。

    测试函数还可以使用topicTest()。下面提供一个新的测试途径——在controller中测试。新增方法
@Autowired
private Publisher publisher; @GetMapping("/sendTopicMsg")
public String sendTopicMsg(String msg) {
// 指定消息发送的目的地及内容
Destination destination = new ActiveMQTopic("topicListener2");
for (int i = 0; i < 8; i++) {
publisher.publish(destination, msg + i);
}
return msg + " 发送完毕";
}

执行结果省略。

【Spring Boot】ActiveMQ 设置访问密码的更多相关文章

  1. 设置ActiveMQ的访问密码

    1.设置ActiveMQ的访问密码,以提高ActiveMQ的安全性 2.在ActiveMQ的conf目录的activemq.xml中添加账号密码 2.1  添加的代码如下 <!-- 添加访问Ac ...

  2. Spring Boot的数据访问:CrudRepository接口的使用

    示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...

  3. 关于HTML设置访问密码。

    如果你要设置访问密码恐怕要使用sublime_text了 废话不多,开始!!! 先把这些东西加上: <html> <script> 然后开始写代码: 先辨别密码登录正确的情况: ...

  4. Spring boot未授权访问造成的数据库外联

    一.spring boot 日常测试或攻防演练中像shiro,fastjson等漏洞已经越来越少了,但是随着spring boot框架的广泛使用,spring boot带来的安全问题也越来越多,本文仅 ...

  5. spring boot + activeMq 邮件服务

    引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...

  6. (8)Spring Boot 与数据访问

    文章目录 简介 整合基本的JDBC与数据源 整合 druid 数据源 整合 mybatis 简介 对于数据访问层,无论是 SQL 还是 NOSQL ,Spring Boot 默认都采用整合 Sprin ...

  7. spring boot基于DRUID数据源密码加密及数据源监控实现

    前言 随着需求和技术的日益革新,spring boot框架是越来越流行,她也越来越多地出现在我们的项目中,当然最主要的原因还是因为spring boot构建项目实在是太爽了,构建方便,开发简单,而且效 ...

  8. Spring Boot实现数据访问计数器

    1.数据访问计数器   在Spring Boot项目中,有时需要数据访问计数器.大致有下列三种情形: 1)纯计数:如登录的密码错误计数,超过门限N次,则表示计数器满,此时可进行下一步处理,如锁定该账户 ...

  9. Spring boot教程mybatis访问MySQL的尝试

    Windows 10家庭中文版,Eclipse,Java 1.8,spring boot 2.1.0,mybatis-spring-boot-starter 1.3.2,com.github.page ...

  10. spring boot:使用mybatis访问多个mysql数据源/查看Hikari连接池的统计信息(spring boot 2.3.1)

    一,为什么要访问多个mysql数据源? 实际的生产环境中,我们的数据并不会总放在一个数据库, 例如:业务数据库:存放了用户/商品/订单 统计数据库:按年.月.日的针对用户.商品.订单的统计表 因为统计 ...

随机推荐

  1. Elasticsearch搜索引擎学习笔记(五)

    搜索功能 数据准备 1.自定义词库 慕课网 慕课 课网 慕 课 网 2.新建立索引shop 3.建立mappings POST /shop/_mapping (7.x之前的版本:/shop/_mapp ...

  2. 分享一个裁剪图片Chrome 扩展 —— Crop Image

    1. 前言 在日常工作和设计过程中,我们常常需要对图片进行裁剪,以适配不同的使用场景.无论是社交媒体头像.网站图片优化,还是艺术设计,精确的图片裁剪都是必不可少的.然而,许多在线工具使用复杂,或者功能 ...

  3. OpenGL ES与GLSL ES各版本对应说明

    OpenGL ES 3.2 OpenGL ES 3.2 and OpenGL ES Shading Language 3.20 OpenGL ES 3.1 OpenGL ES 3.1 and Open ...

  4. 基于pandas的数据清洗 -- 缺失值(空值)的清洗

    博客地址:https://www.cnblogs.com/zylyehuo/ 开发环境 anaconda 集成环境:集成好了数据分析和机器学习中所需要的全部环境 安装目录不可以有中文和特殊符号 jup ...

  5. Delphi让网页只允许在WebBrowser里面打开

    [添加组件] 添加 Internet->WebBrowser //显示网页 [添加事件] 鼠标点击WebBrowser组件,在Events事件选项框中找到. OnNewWindows2,OnSt ...

  6. 为什么不建议通过Executors构建线程池

    Executors类看起来功能还是比较强大的,又用到了工厂模式.又有比较强的扩展性,重要的是用起来还比较方便,如: ExecutorService executor = Executors.newFi ...

  7. VirtualBox 新建虚拟电脑时没有64-bit选项?

    好久没用VirtualBox了,没事下载了个准备看下新版的Ubuntu 16.04 & umake命令. 下载&安装完成,准备新建的时候,发现个问题:没有64-bit的选项? 目测了下 ...

  8. 学习unigui【18】unidbgrid的GridsGroupingSorting

    折腾二天,你不按照demo里的代码来,就是没有效果.功力不够导致的.学习学习再学习!努力努力再努力! procedure TUniGridsGroupingSorting.UniDBGrid1Mult ...

  9. PMP学习记录

    本人在2020年12月已经顺利拿到PMP证书. 第一次听说PMP证书是2016年,一个同事说考试通过拿到了PMP证书,当时对PMP不是很了解.也未作深入了解,当时认为俺是做技术的,这个证书没啥用.O( ...

  10. ESP32+Arduino入门(三):连接WIFI获取当前时间

    ESP32内置了WIFI模块连接WIFI非常简单方便. 代码如下: #include <WiFi.h> const char* ssid = "WIFI名称"; con ...