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