Java JMX获得登录用户 [英] Java JMX get the logged in user
问题描述
我有一个由JVM进程启动的JMX服务器,可以选择对其进行保护(目前还没有安全保护,因此我准备考虑采用任何可扩展的方式).例如,作为一种选择,我查看了官方教程,现在可以使用用户/密码身份验证了. MBean是通过spring部署的(使用注释方法).
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).
现在,我想从我的MBean中获得用户名,以执行其他一些操作.完全可以使用JMX技术吗?如果是,怎么办? :)
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? :)
示例:
public class MyMBean {
public void myOp() {
String userName = ... ?; // or whatever object that can bring me the name
makeSomethingWithUserName ( userName );
doMyStuffHere();
}
}
我找到了本教程,但它似乎是特定于玻璃鱼的,我正在寻找用于基于纯Java的解决方案.
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
推荐答案
这应该可行,但是只有在用户通过身份验证时才填写主题.本地JMX连接将提供一个空的Subject!
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<JMXPrincipal> principals = subject.getPrincipals(JMXPrincipal.class);
JMXPrincipal principal = principals.iterator().next();
return principal.getName();
}
return "";
}
这篇关于Java JMX获得登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!