Java JMX client with SSL
Asked Answered
D

1

12

I configured Apache Tomcat 8 using this tutorial https://tomcat.apache.org/tomcat-7.0-doc/monitoring.html and I generated SSL certificate.

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://10.16.70.161:9999/jmxrmi");

            HashMap environment = new HashMap();
            String[] credentials = new String[]
            {
                "user", "passw"
            };
            environment.put(JMXConnector.CREDENTIALS, credentials);

            JMXConnector jmxc = JMXConnectorFactory.connect(url, environment);
            MBeanServerConnection server = jmxc.getMBeanServerConnection();

            Set<ObjectName> s2 = server.queryNames(new ObjectName("Catalina:type=Server,*"), null);
            for (ObjectName obj : s2)
            {
                ObjectName objname = new ObjectName(obj.getCanonicalName());
                System.out.println("serverInfo " + server.getAttribute(objname, "serverInfo"));
                System.out.println("address " + server.getAttribute(objname, "address"));
                System.out.println("stateName " + server.getAttribute(objname, "stateName"));
            }

How I need to extend this JMX client in order to use it with SSL certificate? I can't find any good example on Internet.

Diskson answered 22/12, 2015 at 8:50 Comment(0)
S
7

You're almost there, your code is correct, you only need to start your JMX client with the following command-line after having added your SSL certificate to a trust store using the keytool command-line utility:

java -Djavax.net.ssl.trustStore=/your/path/to/truststore.jks \ 
  -Djavax.net.ssl.trustStorePassword=truststore_pwd \ 
  YourJMXClient
Shit answered 27/12, 2015 at 5:16 Comment(3)
Why do you add SslRMIServerSocketFactory and SslRMIClientSocketFactory? Is it mandatory?Diskson
Sorry about that, it's only necessary when you want to create a JMXServer. In this case, all you need to do is to configure your trust store and pass it to your client when you start.Shit
Can I somehow use Java code to set trustStore location and password without complete custom JMX client? I made a new post here https://mcmap.net/q/1011629/-replace-system-setpropertyDiskson

© 2022 - 2024 — McMap. All rights reserved.