美文网首页
NETTY+WEBSOCKET在握手成功后进行验证

NETTY+WEBSOCKET在握手成功后进行验证

作者: 785ac245e5c9 | 来源:发表于2018-01-19 15:25 被阅读1269次

具体需求:
在建立websocket连接后,对用户传过来的url中的token参数进行验证,再从缓存中获取该用户信息

方法步骤:
1.继承netty包里的WebSocketServerProtocolHandler,重写里面的handlerAdded方法,用自己handshakehandler来替换它

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) {
        ChannelPipeline cp = ctx.pipeline();
        if (cp.get(ZZKWebSocketServerProtocolHandshakeHandler.class) == null) {
            // Add the WebSocketHandshakeHandler before this one.

**********在这里写入自己的HandshakeHandler*********
            ctx.pipeline().addBefore(ctx.name(), ZZKWebSocketServerProtocolHandshakeHandler.class.getName(),
                    new ZZKWebSocketServerProtocolHandshakeHandler(websocketPath, subprotocols,
                            allowExtensions, maxFramePayloadSize, allowMaskMismatch, checkStartsWith));
        }
****************************************************

        if (cp.get(Utf8FrameValidator.class) == null) {
            // Add the UFT8 checking before this one.
            ctx.pipeline().addBefore(ctx.name(), Utf8FrameValidator.class.getName(),
                    new Utf8FrameValidator());
        }
    }

2.复制WebSocketServerProtocolHandshakeHandler这个类,重新命名,在里面的channelRead方法里写入自己的业务逻辑,具体位置在添加channelfuture的闭包里,if(future.isSuccess())..............

    @Override
    public void channelRead(final ChannelHandlerContext ctx, Object msg) throws Exception {
        final FullHttpRequest req = (FullHttpRequest) msg;
        if (isNotWebSocketPath(req)) {
            ctx.fireChannelRead(msg);
            return;
        }

        try {
            if (req.method() != GET) {
                sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
                return;
            }

            final WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
                    getWebSocketLocation(ctx.pipeline(), req, websocketPath), subprotocols,
                    allowExtensions, maxFramePayloadSize, allowMaskMismatch);
            final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
            if (handshaker == null) {
                WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
            } else {
                final ChannelFuture handshakeFuture = handshaker.handshake(ctx.channel(), req);
                handshakeFuture.addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        if (!future.isSuccess()) {
                            ctx.fireExceptionCaught(future.cause());
                        } else {
                            // Kept for compatibility
                            ctx.fireUserEventTriggered(
                                    WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
                            ctx.fireUserEventTriggered(
                                    new ZZKWebSocketServerProtocolHandler.HandshakeComplete(
                                            req.uri(), req.headers(), handshaker.selectedSubprotocol()));
                            //这里写业务逻辑处理
                            BeanHolder.get(MessageHelperService.class).validate(ctx, req);

                        }
                    }
                });
                ZZKWebSocketServerProtocolHandshakeHandler.setHandshaker(ctx.channel(), handshaker);
                ctx.pipeline().replace(this, "WS403Responder",
                        ZZKWebSocketServerProtocolHandler.forbiddenHttpRequestResponder());
            }
        } finally {
            req.release();
        }
    }

最后在netty的handler链中加入该handler

        ChannelPipeline pipeline = channel.pipeline();

        ....
        pipeline.addLast(new ZZKHttpRequestHandler());//区分http请求,读取html页面并写回客户端浏览器
        pipeline.addLast(new ZZKWebSocketServerProtocolHandler());//会拦截该地址的请求,会区分访问是控制针访问还是数据帧访问,数据帧访问会交给自己的控制器处理
        pipeline.addLast(new ZZKChatServerHandler());//向客户端发送数据

相关文章

  • NETTY+WEBSOCKET在握手成功后进行验证

    具体需求:在建立websocket连接后,对用户传过来的url中的token参数进行验证,再从缓存中获取该用户信息...

  • DRF使用篇(四):反序列化

    验证过程 使用序列化器进行反序列化时,需要对数据进行验证后,才能获取验证成功的数据或保存成模型类对象 在获取反序列...

  • 页面元素之表单续篇

    1、提交功能 提交之前通常会进行前端验证,来减轻服务器负担,验证成功后提交到服务器,然后在进行数据库操作(验证登录...

  • Golang实现WebSocket协议

    一、什么是websocket Websocket是一个应用层协议,它必须依赖HTTP协议进行一次握手,握手成功后,...

  • Django-->反序列化的操作

    1. 验证 使用序列化器进行反序列化时,需要对数据进行验证后,才能获取验证成功的数据或保存成模型类对象在获取反序列...

  • axios 实现自动重新请求接口

    目前有这样一个需求:前端对需要验证的接口进行拦截并在弹框中获取验证码,在输入验证码并验证成功后,自动带着验证码重新...

  • Djangorestframework的反序列化

    一 数据验证 后端接收数据,使用序列化器进行反序列化时,需要对数据进行验证后,才能获取验证成功的数据,再保存为模型...

  • websocket iOS开发小记(socket.io实现,含c

    Websocket是应用层第七层上的一个应用层协议,它必须依赖 HTTP 协议进行一次握手 ,握手成功后,数...

  • 登录注册流程

    产品登录流程:登录 输入账号登录(手机号,邮箱,ID等)输入相应密码进行验证,验证成功后,登录成功。第三方登录(微...

  • 1.http和https的理解

    http底层是TCP建立连接会进行三次握手: 为什么需要三次握手,是为了保证传输的可靠性,三次握手可以验证客户端和...

网友评论

      本文标题:NETTY+WEBSOCKET在握手成功后进行验证

      本文链接:https://www.haomeiwen.com/subject/hfnooxtx.html