java获取 jmx,Java JMX获得登录用户

这篇博客探讨了如何在未启用安全性的JMX服务器中获取用户身份信息。作者考虑使用官方教程中提到的用户/密码认证,并在MBean内部进行额外操作。文章提供了一个示例代码片段,展示如何利用AccessControlContext和Subject获取当前用户的用户名。虽然找到的教程可能与GlassFish特定,但作者寻求的是纯Java解决方案。
摘要由CSDN通过智能技术生成

I have a JMX server started by JVM process that can be optionally secured (for now its not, so I'm ready to consider any extensible way for it). For example as an option I looked at official tutorial and its ok for now to use user/password authentication.

The MBeans are deployed via spring (annotations approach is used).

Now I would like to get from within my MBean the user name for some additional actions. Is it possible to do in JMX technology at all? If Yes how? :)

Example:

public class MyMBean {

public void myOp() {

String userName = ... ?; // or whatever object that can bring me the name

makeSomethingWithUserName ( userName );

doMyStuffHere();

}

}

I've found this tutorial but it seems to be glassfish specific and I'm looking for plain java based solution.

Thanks a lot and have a nice day

解决方案

This should work, but the Subject is only filled if the user authenticated. A local JMX connection will give an empty Subject!

private String getUserName() {

AccessControlContext acc = AccessController.getContext();

Subject subject = Subject.getSubject(acc);

if (subject != null) {

Set principals = subject.getPrincipals(JMXPrincipal.class);

JMXPrincipal principal = principals.iterator().next();

return principal.getName();

}

return "";

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值