简体   繁体   English

码头 Websocket 认证

[英]Jetty Websocket Authentication

I play the Jetty Websocket use example here:我在这里玩 Jetty Websocket 使用示例:

https://github.com/jetty-project/embedded-jetty-websocket-examples (i use native-jetty-websocket-example) https://github.com/jetty-project/embedded-jetty-websocket-examples (我用的是native-jetty-websocket-example)

I just move from websocketpp (C++) to Java Jetty Websocket and i just wonder is there any way for me to authen the connection before the connection move to the onWebSocketConnect event ?我只是从 websocketpp (C++) 移动到 Java Jetty Websocket 我只是想知道在连接移动到 onWebSocketConnect 事件之前我有什么方法可以验证连接吗?

Back in websocketpp i'll authenticate the connection via url (eg: ws://xxx/?key=123) when i will accept the connection or not.回到 websocketpp,我将通过 url(例如:ws://xxx/?key=123)验证连接,当我接受或不接受连接时。 I can drop the connection before it "upgrade", and the client will result in connect failed我可以在“升级”之前断开连接,客户端将导致连接失败

In Java i don't known how to do that, when the event come to onWebSocketConnect then the connection is etablished在 Java 中,我不知道该怎么做,当事件到达 onWebSocketConnect 时,连接就建立了

You can set up Security Constraints for that path and configure the Jetty authentication mechanisms, see details of how to do that here https://www.eclipse.org/jetty/documentation/jetty-9/index.html#configuring-security .您可以为该路径设置安全约束并配置 Jetty 身份验证机制,请在此处查看如何执行此操作的详细信息https://www.eclipse.org/jetty/documentation/jetty-9/index.html#configuring-security

You could also use the JettyWebSocketCreator to do some checks on the HTTP Request just before the connection upgrades to WebSocket. Instead of this line https://github.com/jetty-project/embedded-jetty-websocket-examples/blob/564c40b56413905cadeb500ade40d53e578ea990/native-jetty-websocket-example/src/main/java/org/eclipse/jetty/demo/EventServer.java#L55您还可以使用JettyWebSocketCreator在连接升级到 WebSocket 之前对 HTTP 请求进行一些检查。而不是这条线https://github.com/jetty-project/embedded-jetty-websocket-examples/blob/564c40b564930161288 native-jetty-websocket-example/src/main/java/org/eclipse/jetty/demo/EventServer.java#L55

You can do something like:你可以这样做:

wsContainer.addMapping("/events/*", (req, resp) ->
{
    if (!Objects.equals(req.getParameterMap().get("key"), "123"))
    {
        resp.sendError(...);
        return null;
    }

    return new EventSocket();
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM