package com.test;
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.security.Security;
import java.util.HashMap;
/**
* @author zhouyi
* @description
* @date 2021/8/26 9:08
*/
public class JmxClientTest {
final static String RMI_URL = "service:jmx:jmxmp://127.0.0.1:7200";
/**
* 需要引入jar:
* 在java环境中jre/lib/ext中加入jar: jmxremote_optional.jar
* 在测试用例项目中引入jar: bean-validator-4.0.0.Alpha3.jar
*/
public static void main(String[] args) throws Exception {
HashMap<String, Object> env = new HashMap<>();
Security.addProvider(new com.sun.jdmk.security.sasl.Provider());
env.put("jmx.remote.profiles", "SASL/PLAIN");
env.put("jmx.remote.sasl.callback.handler",new UserPasswordCallBackHandler("thanos","thanos123.com"));
JMXConnector jmxConnector = JMXConnectorFactory.connect(new JMXServiceURL(RMI_URL), env);
MBeanServerConnection mBeanServer = jmxConnector.getMBeanServerConnection();
System.out.println("MBean count = " + mBeanServer.getMBeanCount());
String[] domains = mBeanServer.getDomains();
for (String doMain : domains) {
System.out.println("JmxDomain:" + doMain);
}
System.out.println("====== Catalina Jmx =====");
}
}
package com.test;
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
/**
* @author zhouyi
* @description
* @date 2021/8/25 17:00
*/
public class UserPasswordCallBackHandler implements CallbackHandler{
/**
* The username to be provided when prompted.
*/
private String username;
/**
* The password to be provided when prompted.
*/
private String password;
/**
* Create a new NamePasswordCallbackHandler (required Cams default
* constructor that is dynamically called by the authentication server).
*/
public UserPasswordCallBackHandler() {
this.username = null;
this.password = null;
}
/**
* Create a new NamePasswordCallbackHandler (optional constructor used to
* facilitate testing).
*
* @param username
* the username to provide when prompted.
* @param password
* the password to provide when prompted.
*/
public UserPasswordCallBackHandler(String username, String password) {
this.username = username;
this.password = password;
}
/**
* Set the username.
*
* @param username
* the username to be provided to a NameCallback.
*/
public void setUsername(String username) {
if (username == null) {
this.username = "";
return;
}
this.username = username;
}
/**
* Set the password.
*
* @param password
* the password to be provided to a PasswordCallback.
*/
public void setPassword(String password) {
if (password == null) {
this.password = "";
return;
}
this.password = password;
}
/**
* Retrieve or display the information requested in the provided Callbacks.
* The handle method implementation checks the instance(s) of the Callback
* object(s) passed in to retrieve or display the requested information.
*
* @param callbacks
* an array of Callback objects provided by an underlying
* security service which contains the information requested to
* be retrieved or displayed.
*
* @exception IOException
* if an input or output error ocurrs
* @exception UnsupportedCallbackException
* if the implementation of this method does not support one
* or more of the Callbacks specified in the callbacks
* parameter
*/
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
// Loop over all Callbacks
for (int i = 0; i < callbacks.length; i++) {
Callback cb = callbacks[i];
if (cb instanceof NameCallback) {
((NameCallback) cb).setName(username);
} else if (cb instanceof PasswordCallback) {
// JAAS specifies that the password is a char[]
((PasswordCallback) cb).setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(cb, "Unrecognized Callback");
}
}
}
}