Smack SASLPlainMechanism类实现的身份验证问题

时间:2016-07-24 23:54:25

标签: android authentication xmpp smack sasl

尝试连接到FCM CCS时尝试验证我的XMPP Smack连接。我有两种不同的身份验证方式,我正在尝试使用它。两种方式都会抛出异常。我觉得我只需要在这里提示以正确的方式移动。谢谢你的建议。这是我的代码:另请注意,我设置了一个密钥库,以便在我将此身份验证错误排除后启动与FCM CCS的TLS握手。

XMPPTCPConnectionConfiguration conf =      XMPPTCPConnectionConfiguration.builder()
            .setServiceName("rarigames.answerme").setHost("fcm-  xmpp.googleapis.com").setUsernameAndPassword(s1, s2)
            .setSocketFactory(SSLSocketFactory.getDefault())
            .setSendPresence(false)
            .setPort(5236)
            .setCompressionEnabled(false).build();

            other_connection = new XMPPTCPConnection(conf); //AbstractXMPPTCPConnection object       
            XMPPTCPConnection.setUseStreamManagementDefault(true);

other_connection.addConnectionListener(new ConnectionListener(){
           @Override
           public void connected(XMPPConnection connection) { 
//I call either method1 or method2 here to authenticate connection

              method1(); //gives the exception: 
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.jivesoftware.smack.sasl.SASLMechanism.getName()' on a null object reference

method2(); //gives the exception: CallbackHandler not (yet) supported?? 
           } 

          }

new Thread(new Runnable(){
        @Override
        public void run() {
            try {
                // Connect and authenticate with to XMPP server (GCM CCS in this case).
                other_connection.connect();
            } catch (SmackException | IOException | XMPPException e) {
                Log.d("ricky", "Unable to connect or login to FCM CCS. " + e);
            }           
           }).start();

以下两种方法显示了两种不同的身份验证方式:

private void method1{
           SASLMechanism p = new SASLMechanism() {
           @Override
           protected void authenticateInternal(CallbackHandler cbh) throws SmackException {
           }
          @Override
           protected byte[] getAuthenticationText() throws SmackException {
           Log.d("batman", "hiya superman");                        
           byte[] authcid = toBytes('\u0000' + this.authenticationId); //this.authenticationId
           byte[] passw = toBytes('\u0000' + this.password);//this.password
           return ByteUtils.concact(authcid, passw);
           }
           @Override
           public String getName() {
           return "PLAIN";
           }
           @Override
           public int getPriority() {
           return 410;
           }
           @Override
           public void checkIfSuccessfulOrThrow() throws SmackException {
           }
           @Override
           protected SASLMechanism newInstance() {
           return this;
           }
           };
           SASLAuthentication.registerSASLMechanism(p);
           p.instanceForAuthentication(other_connection);
           try {
           p.authenticate("fcm-xmpp.googleapis.com", "rarigames.answerme", new CallbackHandler() {
           @Override
           public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
           for(int i=0;i< callbacks.length;i++){
           Log.d("flacka", "hehe: "+callbacks[i].toString());
           }
           }
           });
           } catch (SmackException e) {
           Log.d("yaas","something "+e);
           e.printStackTrace();
           }         
           }

private void method2(){
        SASLPlainMechanism plaino = new SASLPlainMechanism();
        SASLAuthentication.registerSASLMechanism(plaino);
        plaino.instanceForAuthentication(other_connection);
        try {
        plaino.authenticate("fcm-xmpp.googleapis.com", "rarigames.answerme", new CallbackHandler() {
        @Override
        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
        for(int i=0;i< callbacks.length;i++){
        Log.d("flacka", "hehe: "+callbacks[i].toString());
        }
        }
        });
        } catch (SmackException e) {
        Log.d("yaas","something "+e);
        e.printStackTrace();
        }
        }     

正如您所见,method1()调用给出了异常: java.lang.NullPointerException:尝试调用虚方法&#39; java.lang.String org.jivesoftware.smack.sasl.SASLMechanism.getName()&#39;在空对象引用上

并调用method2()会导致错误 CallbackHandler尚未支持?

这里的任何人都可以提供关于这里可能发生什么的线索,想法或答案吗?我看到mehod getName()正在调用一个null对象引用,但是哪个null对象是引用的日志?

0 个答案:

没有答案