2017-01-19 7 views
0

私は、サーバーのメッセージを聞くbayeuxクライアントを持つことを目指しています。しかし、私はまだbayeuxクライアントのサーバとの接続に苦労しています。サーバーにはログイン資格情報が必要です。しかし、私はログインの詳細なしで多くの例を発見した。いくつかの例を使用して、私は、BayeuxクライアントオブジェクトにLongPollingTransportオブジェクトが必要であることを発見しました。これにはhttpclientが含まれています。しかし、私は最初に接続をしていることを知っているので、私はチャネル/テスト/一時的にチャネルのサーバーを聞いて開始する必要があります。サーバーのURLは(URL = "https://manse.abcd.fi";)とクライアントBayuex http urlはログインが必要なので、どのように私は接続を確立するためにログイン(ユーザーとパスワード)を渡すことができます。私は、それがLongPollingTransportクラスのどこかに与えられるべきだと感じています。これまで私は接続に失敗しました。bayuexclientでサーバのログイン認証情報を渡すには?

import org.cometd.bayeux.Channel; 
    import org.cometd.bayeux.Message; 
    import org.cometd.bayeux.client.ClientSessionChannel; 
    import org.cometd.bayeux.client.ClientSessionChannel.MessageListener; 
    import org.cometd.client.BayeuxClient; 
    import org.cometd.client.transport.ClientTransport; 
    import org.cometd.client.transport.LongPollingTransport; 
    import org.eclipse.jetty.client.ContentExchange; 
    import org.eclipse.jetty.client.HttpClient; 

    import java.net.MalformedURLException; 
    import java.net.URL; 
    import java.util.HashMap; 
    import java.util.Map; 

    public class Test { 

      public static void main(String[] args) { 
       HttpClient httpClient = new HttpClient(); 
       // LongPollingTransport transport = new LongPollingTransport(options, httpClient); 
       String url = "https://manse.soneraiox.fi"; 

       //LongPollingTransport transport = new LongPollingTransport(url, null, httpClient); 

       // String url = "https://manse.soneraiox.fi"; 
       BayeuxClient client = new BayeuxClient(url, LongPollingTransport.create(null)); 
       //System.out.println(client.isHandshook); 
       // Handshake 
       client.handshake(); 
       Boolean a=client.isHandshook(); 
       System.out.println(a); 

       System.out.println(client.isConnected()); 

このコードはまだfalseを返します。 LongPollingTransport.create(null)が動作しているかどうかはまだわかりません。あなたが提案したのと同じ名前空間を使用しました。それは沖です。

import org.cometd.bayeux.Channel; 
import org.cometd.bayeux.Message; 
import org.cometd.bayeux.client.ClientSessionChannel; 
import org.cometd.bayeux.client.ClientSessionChannel.MessageListener; 
import org.cometd.client.BayeuxClient; 
import org.cometd.client.transport.ClientTransport; 
import org.cometd.client.transport.LongPollingTransport; 
import org.eclipse.jetty.client.ContentExchange; 
import org.eclipse.jetty.client.HttpClient; 

import java.net.MalformedURLException; 
import java.net.URL; 
import java.util.HashMap; 
import java.util.Map; 

public class Stest { 

     public static void main(String[] args) { 
      HttpClient httpClient = new HttpClient(); 
      // LongPollingTransport transport = new LongPollingTransport(options, httpClient); 
      String url = "https://manse.soneraiox.fi"; 

      //LongPollingTransport transport = new LongPollingTransport(url, null, httpClient); 

      // String url = "https://manse.soneraiox.fi"; 
      BayeuxClient client = new BayeuxClient(url, LongPollingTransport.create(null)); 
      //System.out.println(client.isHandshook); 
      // Handshake 
      Map<String, Object> extra = new HashMap<>(); 
      Map<String, Object> ext = new HashMap<>(); 
      extra.put(Message.EXT_FIELD, ext); 
      Map<String, Object> authn = new HashMap<>(); 
      ext.put("com.acme.authn", authn); 
      authn.put("username", "[email protected]"); 
      authn.put("password", "xxxxx"); 
      client.handshake(extra); 
      //client.handshake(); 
      Boolean a=client.isHandshook(); 
      System.out.println(a); 

      System.out.println(client.isConnected()); 

     } 
     } 

答えて

0

CometD authenticationに関するドキュメントを参考にしてください。

この質問に対する回答は、実際の認証の詳細によって異なります。

あなたが(ない標準のHTTPベーシック/ダイジェストを経由して)アプリケーションを介して認証を管理する場合、あなたはハンドシェイク中に余分なフィールドとして認証資格情報を渡すことができます。

BayeuxClient client = ...; 
Map<String, Object> extra = new HashMap<>(); 
Map<String, Object> ext = new HashMap<>(); 
extra.put(Message.EXT_FIELD, ext); 
Map<String, Object> authn = new HashMap<>(); 
ext.put("com.acme.authn", authn); 
authn.put("username", "foo"); 
authn.put("password", "bar"); 
client.handshake(extra); 

それは「名前空間」することをお勧めします上記の例ではcom.acme.authnフィールドのように、余分なフィールドがあります。

CometDによる認証を実行すると、HTTP(HTTP/1.1またはHTTP/2)またはWebSocketのいずれの転送でも動作するという利点があります。

また、他の方法で認証を実行し、認証トークンをCometDに渡すこともできます。

たとえば、HTTPベースの認証では、後続の各要求で渡され、サーバーによって検証されるCookieが確立されます。

// External authentication yields a cookie. 
HttpCookie cookie = authenticate(); 

BayeuxClient client = ...; 
client.putCookie(cookie); 
client.handshake(); 

ありますが、外部認証してからCometDに認証トークンを渡すことができる方法についての事実上無限のバリエーションがありますが、これまでCometDはほとんどないにそれらのすべてを収容するのに十分な柔軟性となっています。

正確な回答のために十分な詳細は述べていませんが、上記を開始する必要があります。

+0

Sbordet、私はあなたが提案した方法を試しました。しかし、握手はうまくいかなかった。ネームスペースとクラスLongPollingTransportを正しく使用したかどうか教えてください。 –

+0

あなたはどのバージョンのCometDを使用していますか? 'LongPollingTransport.create()'はCometD 3.xには存在しません。 CometD 3.1.xとスティックハンドシェイクは非同期操作であるため、 'handshake()'を呼び出した直後に 'BayeuxClient'のステータスをコンソールに出力することはできません。 [documentation](https://docs.cometd.org/current/reference/#_java_client_handshake)をお読みください。サーバーに資格情報を正しく渡したら、[認証のドキュメント](https://docs.cometd.org/current/reference/#_java_server_authentication)で説明されているように、資格情報を検証する必要があります。 – sbordet

+0

http://stackoverflow.com/questions/41830883/how-the-java-client-in-cumulosity-listens-to-events –

関連する問題