title: spring security jmx authneticator
body: <p>I have a JMX server configured without Spring and am trying to implement Spring Security for the Authorization part.
  (See here, <a href="https://blogs.oracle.com/lmalventosa/entry/jmx_authentication_authorization" rel="nofollow">https://blogs.oracle.com/lmalventosa/entry/jmx_authentication_authorization</a>
  Use Case 4, without the Authorization part)</p>
  
  <p>I would like now to implement the Authorization part using Spring Security.</p>
  
  <p>In my JMX authenticator, I do:</p>
  
  <pre><code>final List&lt;GrantedAuthority&gt; roles = new ArrayList&lt;GrantedAuthority&gt;();
  roles.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
  final Authentication auth = new UsernamePasswordAuthenticationToken(credentialsArr[0], credentialsArr[1],
                          roles);
  SecurityContextHolder.getContext().setAuthentication(auth);
  </code></pre>
  
  <p>And in the MBeans I try to fetch it and see that it has been passed correctly (in the future I plan to add Spring Annotations to check for roles, for method invocation).</p>
  
  <pre><code>final Authentication springAuth = SecurityContextHolder.getContext().getAuthentication();
  </code></pre>
  
  <p>The problem is, that in the standard connection flow:</p>
  
  <pre><code>JMXServiceURL url = ...;
  Map env = ...;
  String[] creds = {"monitorRole", "mrpasswd", "FileRealm"};
  env.put(JMXConnector.CREDENTIALS, creds);
  JMXConnector cc = JMXConnectorFactory.connect(url, env);
  MBeanServerConnection mbsc = cc.getMBeanServerConnection();
  </code></pre>
  
  <p>I get a JMX connector, then connect to the MBean server and invoke a method - it works.
  I get through the authenticator, set the Spring Context and get it in the Mbean.</p>
  
  <p>But when I connect using a Jconsole, for example, I don't get the Spring Context in the Mbean.</p>
  
  <p>I am using the Inheritable Thread strategy.</p>
  
  <ol>
  <li>Is there a way to get the context also in the MBean, when connecting using the JConsole and other connectors?</li>
  <li>If I implement JMX using Spring, will it help me to solve the problem?</li>
  <li>Is my main flow fool proof (is there a chance I will not get the Context in the MBean)? I am asking this, since this flow is critical to me, to be fool proof.</li>
  </ol>
  
  <p>Thanks a lot!</p>