Java JMX get the logged in user

孤人 提交于 2020-01-04 11:05:46

问题


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


回答1:


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 "";
}


来源:https://stackoverflow.com/questions/12929335/java-jmx-get-the-logged-in-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!