0%

activeMQ基于插件的连接鉴权

文章字数:297,阅读全文大约需要1分钟

activeMQ本身只支持通过配置文件配置用户名和密码的方式管理连接,但是实际使用中 用户名和密码都是动态分配的。最后在网上找到了一个给activeMQ开发插件鉴权的方法。

大致步骤

  1. 创建一个java application,并添加activeMQ的依赖。版本和部署的版本保持一致。

  2. 创建一个broker plunin类,并返回一个Broker的子类。这个子类就是控制连接鉴权的。

1
2
3
4
5
6
7
import org.apache.activemq.broker.*;

public class LoginAhthPlugin implements BrokerPlugin{
public Broker installPlugin(Broker broker) throw Exception{
reutrn new AuthFilter(broker);
}
}

3.实现AuthFilter,重写addConnection方法,进行权限校验。失败抛出SecurityException,提示用户名密码错误。验证成功调用传入的brokeraddConnection方法。

1
2
3
4
5
6
7
8
9
10
// 调用父类构造方法,传入broker
public AuthFilter(Broker next){
super(next);
}
@Override
publuc void addConnection(ConnectionContext context, ConnectionInfo info)throw Exception{
// 鉴权,失败抛出异常
auth(info.getUserName(),info.getPassword(),info.getClientId());
super.addConnection(context,info);
}
  1. 通过配置文件加入插件

activemq.xml

1
2
3
4
5
6
7
8
9
<!-- 其它同级的地方可以配置注入spring的bean,完成数据库的配置 -->
<broker ...>
<!-- 在这个标签中加入 -->
<plugins>
<bean xmlns="http://www.springframework.org/schema/beans" id="LoginAuthPlugin" class="com.study.mqttatuh.LoginAuthPlugin">
</bean>
</plugins>

</broker>