文章字数:297,阅读全文大约需要1分钟
activeMQ
本身只支持通过配置文件配置用户名和密码的方式管理连接,但是实际使用中 用户名和密码都是动态分配的。最后在网上找到了一个给activeMQ
开发插件鉴权的方法。
大致步骤
创建一个java application
,并添加activeMQ
的依赖。版本和部署的版本保持一致。
创建一个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
,提示用户名密码错误。验证成功调用传入的broker
的addConnection
方法。
1 2 3 4 5 6 7 8 9 10
| 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); }
|
- 通过配置文件加入插件
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>
|