mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-14 06:03:42 +08:00
feat:ws握手同时认证
This commit is contained in:
@@ -10,7 +10,6 @@ import org.apache.commons.lang3.StringUtils;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class HttpHeadersHandler extends ChannelInboundHandlerAdapter {
|
||||
private AttributeKey<String> key = AttributeKey.valueOf("Id");
|
||||
|
||||
@Override
|
||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||
@@ -22,6 +21,8 @@ public class HttpHeadersHandler extends ChannelInboundHandlerAdapter {
|
||||
ip = address.getAddress().getHostAddress();
|
||||
}
|
||||
NettyUtil.setAttr(ctx.channel(), NettyUtil.IP, ip);
|
||||
String token = headers.get("token");
|
||||
NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, token);
|
||||
}
|
||||
ctx.fireChannelRead(msg);
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import io.netty.util.AttributeKey;
|
||||
|
||||
public class NettyUtil {
|
||||
|
||||
public static AttributeKey<String> TOKEN = AttributeKey.valueOf("token");
|
||||
public static AttributeKey<String> IP = AttributeKey.valueOf("ip");
|
||||
public static AttributeKey<Long> UID = AttributeKey.valueOf("uid");
|
||||
|
||||
|
||||
@@ -66,6 +66,7 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<Tex
|
||||
}
|
||||
} else if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
|
||||
getService().connect(ctx.channel());
|
||||
getService().authorize(ctx.channel(), new WSAuthorize(NettyUtil.getAttr(ctx.channel(), NettyUtil.TOKEN)));
|
||||
}
|
||||
super.userEventTriggered(ctx, evt);
|
||||
}
|
||||
@@ -92,10 +93,7 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<Tex
|
||||
log.info("请求二维码 = " + msg.text());
|
||||
break;
|
||||
case HEARTBEAT:
|
||||
break;
|
||||
case AUTHORIZE:
|
||||
getService().authorize(ctx.channel(), JSONUtil.toBean(wsBaseReq.getData(), WSAuthorize.class));
|
||||
log.info("主动认证 = " + msg.text());
|
||||
log.info("心跳");
|
||||
break;
|
||||
default:
|
||||
log.info("未知类型");
|
||||
|
||||
Reference in New Issue
Block a user