2017-07-06 8 views
1

次のコードを使用して、Oracleのマニュアルに従ってTLSとJMXMPを使用してカスタムJMXサーバーを作成しています。それは正常に動作し、問題なくサーバーに接続できますが、 "password.properties"と "access.properties"を指定しても動作していませんが、認証に "USER"と "PASSWORD" JMXはこれらの2つのオプションを無視しているようです。誰かがUSERとPASSWORDを設定してこの問題を解決する正しい方法を明らかにすることができますか?おかげTLSとJMXMPを使用してカスタムJMXサーバーを使用してユーザーとパスワードを認証する方法

 private JMXServiceURL url() { 
     final String url = String.format("service:jmx:jmxmp://%s:%s", host(), port()); 
     try { 

      return new JMXServiceURL(url); 

     } catch(Throwable exception) { 
      throw new RuntimeException(String.format("Failed to create JMX Service URL: %s", url), exception); 
     } 
    } 

    private Map<String, Object> env() { 
     final Map<String, Object> env = new LinkedHashMap<String, Object>(); 


     try { 

      String keystore = "jmx.keystore"; 

      char keystorepass[] = "12345678".toCharArray(); 
      char keypassword[] = "12345678".toCharArray(); 

      KeyStore ks = KeyStore.getInstance("JKS"); 
      ks.load(new FileInputStream(keystore), keystorepass); 
      KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); 

      kmf.init(ks, keypassword); 
      SSLContext ctx = SSLContext.getInstance("TLSv1"); 
      ctx.init(kmf.getKeyManagers(), null, null); 
      SSLSocketFactory ssf = ctx.getSocketFactory(); 

      env.put("jmx.remote.profiles", "TLS"); 
      env.put("jmx.remote.tls.socket.factory", ssf); 
      env.put("jmx.remote.tls.enabled.protocols", "TLSv1"); 
      env.put("jmx.remote.tls.enabled.cipher.suites","SSL_RSA_WITH_NULL_MD5"); 


      env.put("jmx.remote.x.password.file", "password.properties"); 
      env.put("jmx.remote.x.access.file","access.properties"); 


     } catch (Exception e) { 
      e.printStackTrace(); 
     } 


     return env; 
    } 

    private MBeanServer server() { 
     return ManagementFactory.getPlatformMBeanServer(); 
    } 

    private JMXConnectorServer connector() { 
     try { 

      ServerProvider.class.getName(); 
      return JMXConnectorServerFactory.newJMXConnectorServer(url(), env(), server()); 

     }catch(Throwable exception) { 
      throw new RuntimeException("Failed to create JMX connector server factory", exception); 
     } 
    } 

答えて

1

は私が最終的に私はパスワード検証

ための簡単なのCallbackHandlerを実装し

MBeanServer mbs = MBeanServerFactory.createMBeanServer(); 
Security.addProvider(new com.sun.jdmk.security.sasl.Provider()); 

HashMap env = new HashMap(); 
env.put("jmx.remote.profiles", "TLS SASL/PLAIN"); 
env.put("jmx.remote.sasl.callback.handler", 
    new PropertiesFileCallbackHandler("password.properties")); 
env.put("jmx.remote.x.access.file",access.properties"); 

JMXServiceURL url = new JMXServiceURL("jmxmp", null, 5555); 
JMXConnectorServer cs = 
    JMXConnectorServerFactory.newJMXConnectorServer(url, 
                env, 
                mbs); 
cs.start(); 

Oracleのドキュメントから次のコードでJMXMP接続のために追加のユーザーとパスワードを設定することができました

public final class PropertiesFileCallbackHandler 
    implements CallbackHandler { 

    private Properties pwDb; 

    /** 
    * Contents of files are in the Properties file format. 
    * 
    * @param pwFile name of file containing name/password 
    */ 
    public PropertiesFileCallbackHandler(String pwFile) throws IOException { 

     if (pwFile != null) { 

      File file = new File(pwFile); 

      if(file.exists()) { 
       pwDb = new Properties(); 
       pwDb.load(new FileInputStream(file)); 
      } else { 
       throw new IOException("File " + pwFile + " not found"); 
      } 
     } 
    } 

    public void handle(Callback[] callbacks) 
     throws UnsupportedCallbackException { 

     AuthorizeCallback acb = null; 
     AuthenticateCallback aucb = null; 

     for (int i = 0; i < callbacks.length; i++) {  
      if (callbacks[i] instanceof AuthorizeCallback) { 
       acb = (AuthorizeCallback) callbacks[i]; 
      } else if (callbacks[i] instanceof AuthenticateCallback) { 
       aucb = (AuthenticateCallback)callbacks[i]; 
      } else { 
       throw new UnsupportedCallbackException(callbacks[i]); 
      } 
     } 

     // Process retrieval of password; can get password if 
     // username is available 
     if (aucb != null) { 
      String username = aucb.getAuthenticationID(); 
      String password = new String(aucb.getPassword()); 
      String pw = pwDb.getProperty(username); 

      if (pw != null) { 
       if(pw.equals(password)){ 
        aucb.setAuthenticated(true); 
       } 
      } 
     } 

     // Check for authorization 
     if (acb != null) { 
      String authid = acb.getAuthenticationID(); 
      String authzid = acb.getAuthorizationID(); 
      if (authid.equals(authzid)) { 
       // Self is always authorized 
       acb.setAuthorized(true); 
      } 
     } 
    } 
} 
+0

どうやってこれになりましたか? –

+0

同様にクライアントを設定する必要がありましたか? – Adam

+0

はい、このタイプの認証も使用するようにクライアントを設定する必要があります –

関連する問題