[英]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.