简体   繁体   English

如何在bayuexclient中传递服务器的登录凭据?

[英]How to pass the login credentials of the server in bayuexclient?

I aim to have a bayeux client that listens to the server for messages. 我的目标是有一个Bayeux客户端来监听服务器中的消息。 However, I am still struggling with making the connection with server of the bayeux client. 但是,我仍在努力与Bayeux客户端的服务器建立连接。 The server requires login credentials. 服务器需要登录凭据。 But i found many examples without any login details. 但是我发现了许多没有任何登录详细信息的示例。 Using some examples i found that the bayeux client object needs LongPollingTransport object. 通过一些示例,我发现bayeux客户端对象需要LongPollingTransport对象。 which contains an httpclient. 其中包含一个httpclient。 However, i want to make first a handshake so that i know the connection is working then i should start listening to the server on the channel /test/temp .The url of the server is (url = " https://manse.abcd.fi "; ) and the client definition given below.But since the Bayuex http url requires login. 但是,我想先握手,以便我知道连接正在工作,然后我应该开始在/ test / temp通道上监听服务器。服务器的url为(url =“ https://manse.abcd .fi “;)和下面给出的客户端定义。但是由于Bayuex http网址需要登录。 How can i pass the login(user and password) to establish the connection. 我如何通过登录名(用户名和密码)建立连接。 I feel like it should be given somewhere in the LongPollingTransport class. 我觉得应该在LongPollingTransport类中的某个位置给出它。 So far i have failed to connect. 到目前为止,我未能连接。

    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());

This code still gives false. 此代码仍为false。 I am still not sure if the LongPollingTransport.create(null) is working or not. 我仍然不确定LongPollingTransport.create(null)是否正常工作。 I used the same namespace you suggested. 我使用了您建议的相同名称空间。 Is that oki. 那是oki吗?

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", "irz@abc.com");
             authn.put("password", "xxxxx");
             client.handshake(extra);
             //client.handshake();
             Boolean a=client.isHandshook();
             System.out.println(a);

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

       }
       }

You may want to read the documentation about CometD authentication as a basis. 您可能需要阅读有关CometD身份验证的文档。

The answer to this question really depends on the authentication details. 这个问题的答案实际上取决于身份验证详细信息。

If you manage authentication via the application (not via standard HTTP Basic/Digest), then you can pass the authentication credentials as extra fields in the handshake: 如果您通过应用程序(而不是通过标准的HTTP Basic / Digest)管理身份验证,则可以将身份验证凭据作为握手中的其他字段进行传递:

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);

It is good practice to "namespace" extra fields, like done in the example above with the com.acme.authn field. 优良作法是“命名空间”多余的字段,就像上面示例中使用com.acme.authn字段所做的那样。

Performing the authentication via CometD has the advantage that it works with any transport, either HTTP (HTTP/1.1 or HTTP/2) or WebSocket. 通过CometD执行身份验证的优点在于,它可与任何传输(HTTP(HTTP / 1.1或HTTP / 2)或WebSocket)一起使用。

Alternatively, you can perform authentication via other means, and then pass the authentication tokens to CometD. 或者,您可以通过其他方式执行身份验证,然后将身份验证令牌传递给CometD。

For example, typically HTTP-based authentications establish a cookie that must be passed in each subsequent request and that is validated by the server. 例如,通常基于HTTP的身份验证会建立一个cookie,该cookie必须在每个后续请求中传递,并由服务器进行验证。

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

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

There are virtually infinite variations about how you can authenticate externally and then pass the authentication token to CometD, but so far CometD has been flexible enough to accommodate most if not all of them. 关于如何从外部进行身份验证然后将身份验证令牌传递给CometD的方法实际上存在着无限的变化,但是到目前为止,CometD已经足够灵活,可以容纳大多数(如果不是全部)它们。

You don't give enough details for a precise answer, but the above should get you started. 您没有提供足够的详细信息来获得准确的答案,但是以上内容可以帮助您入门。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将登录凭据添加到URL - How to add login credentials to URL Boost Beast,如何提供登录凭据 - Boost Beast, how to supply login credentials 尝试通过 HTTP 传递登录凭据,需要解决方法 - Trying to pass login credentials through HTTP, need workaround 服务器如何知道请求是使用凭据发送的? - How does a server know a request is sent with credentials? Android Http凭证登录 - Android Http Credentials Login 当 http 服务器具有“Access-Control-Allow-Credentials”="true" 时,如何使用 crossOrigin="use-credentials" 在图像上发送凭据? - how to send credentials on an image with crossOrigin="use-credentials" when the http-server has "Access-Control-Allow-Credentials"="true"? 如何在 Flutter 移动应用程序的 API 调用中传递基本身份验证凭据? - How to pass basic auth credentials in API call for a Flutter mobile application? 如何将用户重定向到其他服务器并包含HTTP基本身份验证凭据? - How to redirect a user to a different server and include HTTP basic authentication credentials? 基本身份验证自动登录,但URL中没有凭据 - Basic authentication auto login but no credentials shows in URL 使用错误凭据登录 Django 返回 200 而不是 401 - Django login with wrong credentials returns 200 not 401
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM