【Spring Boot】ActiveMQ 设置访问密码
Apache ActiveMQ是Apache出品,是最流行的,能力很强的开源消息总线。默认情况下,程序连接ActiveMQ是不需要密码的,为了安装起见,需要设置密码,提高安全性。本文分享如何设置访问ActiveMQ的账号密码。
小编使用的ActiveMQ版本是apache-activemq-5.15.13。
一、设置控制台管理密码
<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
配置信息按顺序解释,分别是:用户名、密码、角色名
二、消息生产者和消费者密码认证
<!-- 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中配置,见如下步骤。
## ---------------------------------------------------------------------------
## 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,则开启了点到点消息模式。
@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 设置访问密码的更多相关文章
- 设置ActiveMQ的访问密码
1.设置ActiveMQ的访问密码,以提高ActiveMQ的安全性 2.在ActiveMQ的conf目录的activemq.xml中添加账号密码 2.1 添加的代码如下 <!-- 添加访问Ac ...
- Spring Boot的数据访问:CrudRepository接口的使用
示例 使用CrudRepository接口访问数据 创建一个新的Maven项目,命名为crudrepositorytest.按照Maven项目的规范,在src/main/下新建一个名为resource ...
- 关于HTML设置访问密码。
如果你要设置访问密码恐怕要使用sublime_text了 废话不多,开始!!! 先把这些东西加上: <html> <script> 然后开始写代码: 先辨别密码登录正确的情况: ...
- Spring boot未授权访问造成的数据库外联
一.spring boot 日常测试或攻防演练中像shiro,fastjson等漏洞已经越来越少了,但是随着spring boot框架的广泛使用,spring boot带来的安全问题也越来越多,本文仅 ...
- spring boot + activeMq 邮件服务
引入依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...
- (8)Spring Boot 与数据访问
文章目录 简介 整合基本的JDBC与数据源 整合 druid 数据源 整合 mybatis 简介 对于数据访问层,无论是 SQL 还是 NOSQL ,Spring Boot 默认都采用整合 Sprin ...
- spring boot基于DRUID数据源密码加密及数据源监控实现
前言 随着需求和技术的日益革新,spring boot框架是越来越流行,她也越来越多地出现在我们的项目中,当然最主要的原因还是因为spring boot构建项目实在是太爽了,构建方便,开发简单,而且效 ...
- Spring Boot实现数据访问计数器
1.数据访问计数器 在Spring Boot项目中,有时需要数据访问计数器.大致有下列三种情形: 1)纯计数:如登录的密码错误计数,超过门限N次,则表示计数器满,此时可进行下一步处理,如锁定该账户 ...
- 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 ...
- spring boot:使用mybatis访问多个mysql数据源/查看Hikari连接池的统计信息(spring boot 2.3.1)
一,为什么要访问多个mysql数据源? 实际的生产环境中,我们的数据并不会总放在一个数据库, 例如:业务数据库:存放了用户/商品/订单 统计数据库:按年.月.日的针对用户.商品.订单的统计表 因为统计 ...
随机推荐
- 牛逼了!16.2K Star!推荐一款开源的网络爬虫和浏览器自动化库:Crawlee!
在当今的互联网世界中,网络爬虫作为一种重要的工具,被广泛应用于数据收集.内容监控.SEO优化以及自动化测试等多个领域.随着技术的不断进步,各种开源的网络爬虫库也应运而生.今天,我向大家推荐一款非常优秀 ...
- MATLAB R2024b 安装教程
MATLAB R2024b 安装教程 软件介绍 MATLAB 是由 "Matrix" 和 "Laboratory" 两个词组合而成,意为"矩阵工厂&q ...
- 【Bug记录】node-sass安装失败解决方案
node-sass 安装失败解决办法 前言 很多小伙伴在安装 node-sass 的时候都失败了,主要的原因是 node 版本和项目依赖的 node-sass 版本不匹配. 解决方案 解决方案:把项目 ...
- Huawei Cloud EulerOS上安装sshpass
下载源码 git clone https://github.com/kevinburke/sshpass.git 由于网络问题,这里我用了一个代理下载 git clone https://ghprox ...
- Mac 干净彻底地卸载 MySQL
前言 卸载MySQL,首先得知道MySQL的路径.默认的话是在/usr/local文件夹下的. 在系统偏好设置面板中可以看到之前安装的MySQL,此时若想卸载MySQL,可以按照如下步骤来. 之前安装 ...
- 使用Semantic Kernel框架和C#.NET 实现大模型Function Calling
最近研究Function Call,总结了一篇文章,分享给大家 一.GPT-4中实现函数调用功能 定义函数:首先,开发一个函数.例如,一个获取天气信息的函数可能如下: def get_current_ ...
- 【抓包】Fidder Script自动修改包
Fiddler Script的本质是用JScript.NET编写的一个脚本文件CustomRules.js 但是它的语法很像C#但又有些不一样,比如不能使用@符号 通过修改CustomRules.js ...
- codelite常用快捷键积累
博客地址:https://www.cnblogs.com/zylyehuo/ 编译整个工作空间 workplace Ctrl+shift+B 编译当前文件 file Ctrl+F7 编译项目 proj ...
- 全国省市区基础数据SQL插入脚本
整理了一份全国省市区SQL插入脚本,并配上抓取数据读取插入数据库源码,附件下载地址:https://files.cnblogs.com/files/101Love/Region.rar
- 【Linux】3.7 定时任务调度
3.7定时任务调度 1. 任务调度原理 crond任务调度:crontab进行定时任务调度 使用方法:crontab [选项] crontab [选项] -e:编辑crontab定时任务 -i:查询c ...