热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

activeMQSecurity--实现登录验证

SecurityActiveMQ支持可插拔的安全机制,用以在不同的provider之间切换。例如JAASAuthenticationPlugin,CustomAu

Security 

     ActiveMQ支持可插拔的安全机制,用以在不同的provider之间切换。例如JAAS Authentication Plugin,Custom Authentication Implementation,Authorization Plugin 
下面以JAAS Authentication Plugin为例。

    JAAS Authentication Plugin依赖标准的JAAS机制来实现认证。通常情况下,你需要通过设置Java.security.auth.login.config系统属性来 配置login modules的配置文件。如果没有指定这个系统属性,那么JAAS Authentication Plugin会缺省使用login.config作为文件名。

    到官网http://activemq.apache.org/下载 activeMQ发布,目前activeMQ5.11需要JDK7支持,下面以activeMQ5.9+JDK6为例。打开conf文件夹如下

    

1.打开文件login.config,

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. activemq-domain {  
  2.     org.apache.activemq.jaas.PropertiesLoginModule required  
  3.     org.apache.activemq.jaas.properties.user="users.properties"  
  4.     org.apache.activemq.jaas.properties.group="groups.properties";  
  5. };  

这个login.config文件中设置了两个属性:org.apache.activemq.jaas.properties.user和 org.apache.activemq.jaas.properties.group分别用来指向user.properties

2.打开文件groups.properties

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #格式:用户组=用户1,用户2,...  
  2. admins=system,  
  3. users=system,client,user  
  4. guests=guest  

3.打开文件user.properties

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #格式user=password  
  2. system=pass0  
  3. user=pass1  
  4. guest=pass2  


4.打开文件activemq.xml

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1.         <plugins>      
  2.       
  3. <jaasAuthenticationPlugin configuration="activemq-domain" />      
  4.       
  5. <authorizationPlugin>    
  6.     <map>    
  7.      <authorizationMap>    
  8.         <authorizationEntries>    
  9.           <authorizationEntry queue=">" read="admins" write="admins" admin="admins" />   
  10.           <authorizationEntry queue="USERS.>" read="users" write="users" admin="users" />    
  11.           <authorizationEntry queue="GUEST.>" read="guests" write="guests,users" admin="guests,users" />    
  12.             
  13.             
  14.               
  15.           <authorizationEntry topic=">" read="admins" write="admins" admin="admins" />    
  16.           <authorizationEntry topic="USERS.>" read="users" write="users" admin="users" />    
  17.           <authorizationEntry topic="GUEST.>" read="guests" write="guests,users" admin="guests,users" />  
  18.           
  19.               
  20.           <authorizationEntry queue="ActiveMQ.Advisory.>" read="guests,users" write="guests,users" admin="guests,users"/>    
  21.           <authorizationEntry topic="ActiveMQ.Advisory.>" read="guests,users" write="guests,users" admin="guests,users"/>    
  22.          authorizationEntries>    
  23.      authorizationMap>    
  24.    map>    
  25.                     authorizationPlugin>       
  26.   plugins>   

In ActiveMQ we use a number of operations which you can associate with user roles and either individual queues or topics or you can use wildcards to attach to hierarchies of topics and queues.

Operation

Description

read

You can browse and consume from the destination

write

You can send messages to the destination

admin

You can lazily create the destination if it does not yet exist. This allows you fine grained control over which new destinations can be dynamically created in what part of the queue/topic hierarchy



项目引入activeMQ的jar包依赖,

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <dependency>  
  2.        <groupId>org.activemqgroupId>  
  3.        <artifactId>activemq-allartifactId>  
  4.        <version>5.9.0version>  
  5.  dependency>  


实现代码

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. import org.apache.activemq.ActiveMQConnection;  
  2. import org.apache.activemq.ActiveMQConnectionFactory;  
  3. import org.slf4j.Logger;  
  4. import org.slf4j.LoggerFactory;  
  5.   
  6. import javax.jms.*;  
  7.   
  8. /** 
  9.  * Created by IntelliJ IDEA. 
  10.  * Author: ndong 
  11.  * Date: 2015-2-13 
  12.  * Time: 16:50 
  13.  */  
  14. public class ClientListener implements MessageListener {  
  15.   private static final Logger logger = LoggerFactory.getLogger(ClientListener.class);  
  16.   
  17.   //在点对点(PTP)消息传递域中,目的地被成为队列(queue)  
  18.   private Destination destination = null;  
  19.   //初始化 一个JMS客户端到JMS Provider的连接  
  20.   private Connection connection = null;  
  21.   //初始化  一个接受消息的进程  
  22.   private Session session = null;  
  23.   //初始化 消息消费者  
  24.   private MessageConsumer consumer = null;  
  25.   
  26.   public ClientListener() throws Exception {  
  27.     initialize();  
  28.   }  
  29.   
  30.   private void initialize() throws Exception {  
  31.     String userName = "user";  
  32.     String password = "pass1";  
  33.     String url = "failover://tcp://localhost:61616";  
  34.     if (StringUtil.isEmpty(url)) {  
  35.       logger.error("can't read BROKER.URL in property file");  
  36.       throw new Exception("请在配置文件中,添加服务地址。");  
  37.     }  
  38.     ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(  
  39.       userName, password, url);  
  40.     connection = connectionFactory.createConnection();  
  41.     ((ActiveMQConnection) connection).addTransportListener(new ClientTransportListener());  
  42.     //false 参数表示 为非事务型消息,后面的参数表示消息的确认类型(见4.消息发出去后的确认模式)  
  43.     session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
  44.     String subject = "test.subject";  
  45.     destination = session.createQueue(subject);  
  46.     consumer = session.createConsumer(destination);  
  47.   }  
  48.   
  49.   public void start() throws Exception {  
  50.     logger.info("begin listening...");  
  51.     consumer.setMessageListener(this);  
  52.     connection.start();  
  53.   }  
  54.   
  55.   /** 
  56.      * 消息处理函数 
  57.      * 
  58.      * @param message 
  59.      */  
  60.     public void onMessage(Message message) {  
  61.       try {  
  62.         if (message instanceof TextMessage) {  
  63.           TextMessage txtMsg = (TextMessage) message;  
  64.           String msg = txtMsg.getText();  
  65.           logger.info("received msg:" + msg);  
  66.            
  67.             
  68.         } else {  
  69.           logger.info("consumer received: " + message);  
  70.         }  
  71.       } catch (Exception e) {  
  72.         logger.error(e.getMessage(), e);  
  73.       }  
  74.     }  
  75.   
  76.   
  77. }  


ActiveMQ Web Console Security

ActiveMQ使用的是jetty服务器, 通过控制台可以监控消息,默认端口为8161,可通过浏览器http://localhost:8161/admin/index.jsp查看

默认登录密码为admin/admin,修改默认账户,打开conf/jetty.xml文件,找到


[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <bean id="securityConstraint" class="org.eclipse.jetty.http.security.Constraint">  
  2.         <property name="name" value="BASIC" />  
  3.         <property name="roles" value="admin" />  
  4.         <property name="authenticate" value="false" />  
  5. bean>  


将property name为authenticate的属性value="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


值得注意的是 用户名和密码的格式是

用户名 : 密码 ,角色名


finish



推荐阅读
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • Day2列表、字典、集合操作详解
    本文详细介绍了列表、字典、集合的操作方法,包括定义列表、访问列表元素、字符串操作、字典操作、集合操作、文件操作、字符编码与转码等内容。内容详实,适合初学者参考。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • PHP图片截取方法及应用实例
    本文介绍了使用PHP动态切割JPEG图片的方法,并提供了应用实例,包括截取视频图、提取文章内容中的图片地址、裁切图片等问题。详细介绍了相关的PHP函数和参数的使用,以及图片切割的具体步骤。同时,还提供了一些注意事项和优化建议。通过本文的学习,读者可以掌握PHP图片截取的技巧,实现自己的需求。 ... [详细]
  • 图解redis的持久化存储机制RDB和AOF的原理和优缺点
    本文通过图解的方式介绍了redis的持久化存储机制RDB和AOF的原理和优缺点。RDB是将redis内存中的数据保存为快照文件,恢复速度较快但不支持拉链式快照。AOF是将操作日志保存到磁盘,实时存储数据但恢复速度较慢。文章详细分析了两种机制的优缺点,帮助读者更好地理解redis的持久化存储策略。 ... [详细]
  • 本文介绍了Java高并发程序设计中线程安全的概念与synchronized关键字的使用。通过一个计数器的例子,演示了多线程同时对变量进行累加操作时可能出现的问题。最终值会小于预期的原因是因为两个线程同时对变量进行写入时,其中一个线程的结果会覆盖另一个线程的结果。为了解决这个问题,可以使用synchronized关键字来保证线程安全。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 有没有一种方法可以在不继承UIAlertController的子类或不涉及UIAlertActions的情况下 ... [详细]
  • 本文介绍了关于apache、phpmyadmin、mysql、php、emacs、path等知识点,以及如何搭建php环境。文章提供了详细的安装步骤和所需软件列表,希望能帮助读者解决与LAMP相关的技术问题。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
author-avatar
bunnyvivi
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有