使用身份验证 cookie 打开 WebSocket 连接

Open WebSocket connection with authentication cookie(使用身份验证 cookie 打开 WebSocket 连接)
本文介绍了使用身份验证 cookie 打开 WebSocket 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我遇到了与 Android 中的 Websockets 和 cookie 相同的问题,并且我有一直在尝试按照第一条评论的建议解决它,

I have the same issue as Websockets and cookies in Android, and I have been trying to solve it as the first comment suggested,

WebSocketClient( URI serverUri , Draft protocolDraft , Map httpHeaders , int connectTimeout)

WebSocketClient( URI serverUri , Draft protocolDraft , Map httpHeaders , int connectTimeout)

使用 Java-WebSocket,以及查看许多其他 API,例如 Jetty 和AndroidAsync.但尽管如此,我无法打开 websocket 连接.

using Java-WebSocket, as well as looking at many other APIs such as Jetty and AndroidAsync. But despite this I am unable to open up a websocket connection.

我有一个 Apache http cookie,并且需要这个来通过 WebSocket 向服务器验证我自己的身份.将 cookie 转换为 httpHeader 的正确方法是什么,或者有什么巧妙的方法可以在连接到 websocket 时简单地将整个 cookie 添加到身份验证中?也许我只是错过了明显的..

I have an Apache http cookie, and need this to authenticate myself to the server with a WebSocket. What is the correct way of translating a cookie into a httpHeader, or is there any neat way to simply add the entire cookie in the authentication when connection to a websocket? Maybe I am just missing the obvious..

对可能的术语误用表示歉意,但我希望您能大致了解.

Apologies for possible misuses of terms, but I hope you get the general idea.

任何帮助将不胜感激,谢谢!

Any help would be much appreciated, thank you!

推荐答案

所以我实际上设法解决了它,结果发现实际问题不是cookie,而是websocket没有初始化一个有效的 ssl 上下文.这很容易解决:

So I actually managed to solve it, and it turned out that it was not the cookie that was the actual issue, but rather that the websocket is not initialized with a valid sslcontext. This was solved rather easily by:

WebSocketOrderClient webSocketOrderClient = new WebSocketOrderClient(uri, new Draft_17(), cmap, TIMEOUT);
SSLContext sslContext = null;
sslContext = SSLContext.getInstance( "TLS" );
sslContext.init( null, null, null ); // will use java's default key and trust store which is sufficient unless you deal with self-signed certificates

webSocketOrderClient.setWebSocketFactory(new DefaultSSLWebSocketClientFactory(sslContext));
webSocketOrderClient.connectBlocking();

使用 WebSocketOrderClient:

with WebSocketOrderClient:

private class WebSocketOrderClient extends WebSocketClient {
    public WebSocketOrderClient( URI serverUri, Draft draft, Map<String, String> headers, int timeout) {
        super( serverUri, draft, headers, timeout );
    }
    @Override
    public void onOpen( ServerHandshake handshakedata ) {
        Log.w("connected", "true");
    }
    @Override
    public void onMessage( String message ) {
        Log.w( "got: ", message );
    }
    @Override
    public void onClose( int code, String reason, boolean remote ) {
        Log.w( "Disconnected", ""+code  );
    }
    @Override
    public void onError( Exception ex ) {
        ex.printStackTrace();
    }
}

希望这对将来可能遇到此问题的任何人有所帮助.

Hope this helps anyone who might run into this problem in the future.

这篇关于使用身份验证 cookie 打开 WebSocket 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How to implement RecyclerView with section header depending on category?(如何根据类别实现带有节标题的 RecyclerView?)
How to generate JNI header file in Eclipse(如何在 Eclipse 中生成 JNI 头文件)
Setting a custom HTTP header dynamically with Spring-WS client(使用 Spring-WS 客户端动态设置自定义 HTTP 标头)
Could you technically call the string[] anything in the main method?(从技术上讲,您可以在 main 方法中调用 string[] 吗?)
What is the proper way of setting headers in a URLConnection?(在 URLConnection 中设置标头的正确方法是什么?)
How to overwrite http-header quot;Hostquot; in a HttpURLConnection?(如何覆盖 http-header “主机在 HttpURLConnection 中?)