有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用自定义JMXAuthenticator以编程方式从客户端远程连接到JMX的身份验证

我是JMX的新手。我希望使用服务url(使用客户端)连接到JMX MBean,但希望在连接之前对用户进行身份验证

为此,我编写了自定义JMX身份验证类,即JaasCustomAuthenticator。java。课程内容如下所述:

public class JaasCustomAuthenticator implements JMXAuthenticator {
//    public JaasCustomAuthenticator(String s, boolean b) {
//    }

    @Override
    public Subject authenticate(Object credentials) {
        System.out.println("inside authenticaeee");
        System.setProperty("java.security.auth.login.config", "jaasproj01.config");
        Map<String, Object> myCredentials = new HashMap<String, Object>();
        if (credentials instanceof String[]) {
            // JConsole sends the credentials as string array
            // credentials[0] is the username
            // credentials[1] is the password
            String[] args = (String[]) credentials;

            if (args.length == 2) {

                //myCredentials.put(USERNAME, args[0]);
                myCredentials.put("username", args[0]);

                char[] pw = null;

                if (args[1] != null) {
                    pw = args[1].toCharArray();
                }

                //myCredentials.put(PASSWORD, pw);
                myCredentials.put("password", pw);
            } else {
                throw new SecurityException();
            }
        } else if (credentials instanceof Map) {
            myCredentials.putAll((Map) credentials);
        } else {
            throw new SecurityException();
        }

        LoginContext lc = null;

        try {
            lc = new LoginContext("ZaJaasExample01", new ZaCallbackHandler(myCredentials));
            System.out.println("JAAS authenticator called ...");
        } catch (LoginException le) {
            le.printStackTrace();
        }

        try {
            lc.login();
//            try {
//                Subject.doAsPrivileged(lc.getSubject(), new PrintCodeBaseAndPrincipalsAction(), null);
//            } catch (PrivilegedActionException ex) {
//                if (ex.getException() instanceof SecurityException) {
//                    throw (SecurityException) ex.getException();
//                } else {
//                    throw new SecurityException(ex.getException());
//                }
//            }

            return lc.getSubject();

        } catch (LoginException ex) {
            throw new SecurityException(ex);
        } catch (SecurityException ex) {
            throw ex;
        } catch (Throwable ex) {
            throw new SecurityException(ex);
        }
    }
}

我想从哪里连接到远程jmx的客户端是SystemConfigClient。java

public class SystemConfigClient {
    public static final String HOST = "localhost";
    public static final String PORT = "1234";
    public static void main(String[] args) throws IOException, MalformedObjectNameException, LoginException {
      
        Map<String, String[]> env = new HashMap<>();
        String[] credentials = {"user1", "MYP@SSWORD"};
        env.put(JMXConnector.CREDENTIALS, credentials);

        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://" + HOST + ":" + PORT + "/jmxrmi");
        JMXConnector jmxConnector = JMXConnectorFactory.connect(url,env);

        MBeanServerConnection mBeanServerConnection = jmxConnector.getMBeanServerConnection();
        ObjectName name = new ObjectName("com.eq.jmxdemo:type=SystemConfig");

        SystemConfigMBean mBeanProxy =  MBeanServerInvocationHandler.newProxyInstance(mBeanServerConnection,
                name,SystemConfigMBean.class,true);
        System.out.println("Current system config : " + mBeanProxy.doConfig());
        mBeanProxy.setSchemaName("NewSchemaa");
        mBeanProxy.setThreadCount(5);
        System.out.println("Neww system config : " + mBeanProxy.doConfig());
        mBeanProxy.setThreadCount(0);
        jmxConnector.close();
    }
}

以及我正在尝试连接的JMX,即SystemConfigManagement。java很强大:

public class SystemConfigManagement {
    private static final int DEFAULT_THREAD_NUMBER = 10;
    private static final String DEFAULT_DB_NAME = "default";
    static String[] strArray = {"user1","pass1"};
    public static void main(String[] args) throws MalformedObjectNameException,InterruptedException, InstanceAlreadyExistsException,
            MBeanRegistrationException, NotCompliantMBeanException {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        SystemConfig mBean = new SystemConfig(DEFAULT_THREAD_NUMBER,DEFAULT_DB_NAME);
        ObjectName name = new ObjectName("com.eq.jmxdemo:type=SystemConfig");
        JaasCustomAuthenticator j = new JaasCustomAuthenticator();
        System.out.println("Authnticator: "+j.authenticate(strArray).getPrincipals());

        mbs.registerMBean(mBean,name);
        do {
            Thread.sleep(3000);
            System.out.println("[Inside do-while] Thread count= " + mBean.getThreadCount()+ " :::: Schema Name: " + mBean.getSchemaName());
        }while(mBean.getThreadCount()!=0);
    }
}

现在在系统配置管理中。java我打电话给JaasCustomAuthenticator。javaclassauthenticate()方法,但我想在SystemConfigClient中调用它。在连接到JMX服务URL之前使用java

有没有办法做到这一点,因为我在任何地方都找不到解决办法


共 (0) 个答案