feat:ws握手同时认证 以Sec-Websocket-Protocol的形式传递token

This commit is contained in:
zbzbzzz
2023-07-02 15:26:00 +08:00
parent 89a4eb0b0a
commit f35eca9a14
3 changed files with 32 additions and 21 deletions

View File

@@ -1,13 +1,9 @@
package com.abin.mallchat.custom.user.websocket;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultFullHttpRequest;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.util.AttributeKey;
import org.apache.commons.lang3.StringUtils;
import java.net.InetSocketAddress;
@@ -17,27 +13,14 @@ public class HttpHeadersHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
HttpHeaders headers = request.headers();
HttpHeaders headers = ((FullHttpRequest) msg).headers();
String ip = headers.get("X-Real-IP");
if (StringUtils.isEmpty(ip)) {//如果没经过nginx就直接获取远端地址
InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
ip = address.getAddress().getHostAddress();
}
String[] uriSplit = request.uri().split("[?]");
FullHttpRequest modifiedRequest = new DefaultFullHttpRequest(request.protocolVersion(), request.method(), uriSplit[0]);
modifiedRequest.headers().add(headers);
modifiedRequest.content().writeBytes(request.content());
NettyUtil.setAttr(ctx.channel(), NettyUtil.IP, ip);
if (uriSplit.length>=2) {
NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, uriSplit[1]);
}
ctx.fireChannelRead(modifiedRequest);
}else {
super.channelRead(ctx, msg);
}
ctx.fireChannelRead(msg);
}
}

View File

@@ -10,7 +10,6 @@ import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
@@ -89,7 +88,7 @@ public class NettyWebSocketServer {
* 4. WebSocketServerProtocolHandler 核心功能是把 http协议升级为 ws 协议,保持长连接;
* 是通过一个状态码 101 来切换的
*/
pipeline.addLast(new WebSocketServerProtocolHandler("/"));
pipeline.addLast(new WebSocketHandshakeHandler());
// 自定义handler ,处理业务逻辑
pipeline.addLast(new NettyWebSocketServerHandler());
}

View File

@@ -0,0 +1,29 @@
package com.abin.mallchat.custom.user.websocket;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
public class WebSocketHandshakeHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof FullHttpRequest) {
FullHttpRequest request = (FullHttpRequest) msg;
String token = request.headers().get("Sec-Websocket-Protocol");
NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, token);
// 构建WebSocket握手处理器
WebSocketServerHandshakerFactory handshakeFactory = new WebSocketServerHandshakerFactory(
request.uri(), token, false);
WebSocketServerHandshaker handshake = handshakeFactory.newHandshaker(request);
handshake.handshake(ctx.channel(), request);
// 手动触发WebSocket握手状态事件
ctx.pipeline().fireUserEventTriggered(WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE);
} else {
super.channelRead(ctx, msg);
}
}
}