mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-14 06:03:42 +08:00
feat:ws握手同时认证
This commit is contained in:
@@ -1,7 +1,10 @@
|
|||||||
package com.abin.mallchat.custom.user.websocket;
|
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.ChannelHandlerContext;
|
||||||
import io.netty.channel.ChannelInboundHandlerAdapter;
|
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.FullHttpRequest;
|
||||||
import io.netty.handler.codec.http.HttpHeaders;
|
import io.netty.handler.codec.http.HttpHeaders;
|
||||||
import io.netty.util.AttributeKey;
|
import io.netty.util.AttributeKey;
|
||||||
@@ -14,16 +17,27 @@ public class HttpHeadersHandler extends ChannelInboundHandlerAdapter {
|
|||||||
@Override
|
@Override
|
||||||
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
if (msg instanceof FullHttpRequest) {
|
if (msg instanceof FullHttpRequest) {
|
||||||
HttpHeaders headers = ((FullHttpRequest) msg).headers();
|
FullHttpRequest request = (FullHttpRequest) msg;
|
||||||
|
HttpHeaders headers = request.headers();
|
||||||
String ip = headers.get("X-Real-IP");
|
String ip = headers.get("X-Real-IP");
|
||||||
if (StringUtils.isEmpty(ip)) {//如果没经过nginx,就直接获取远端地址
|
if (StringUtils.isEmpty(ip)) {//如果没经过nginx,就直接获取远端地址
|
||||||
InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
|
InetSocketAddress address = (InetSocketAddress) ctx.channel().remoteAddress();
|
||||||
ip = address.getAddress().getHostAddress();
|
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);
|
NettyUtil.setAttr(ctx.channel(), NettyUtil.IP, ip);
|
||||||
String token = headers.get("token");
|
if (uriSplit.length>=2) {
|
||||||
NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, token);
|
NettyUtil.setAttr(ctx.channel(), NettyUtil.TOKEN, uriSplit[1]);
|
||||||
|
}
|
||||||
|
ctx.fireChannelRead(modifiedRequest);
|
||||||
|
}else {
|
||||||
|
super.channelRead(ctx, msg);
|
||||||
}
|
}
|
||||||
ctx.fireChannelRead(msg);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -93,7 +93,10 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<Tex
|
|||||||
log.info("请求二维码 = " + msg.text());
|
log.info("请求二维码 = " + msg.text());
|
||||||
break;
|
break;
|
||||||
case HEARTBEAT:
|
case HEARTBEAT:
|
||||||
log.info("心跳");
|
break;
|
||||||
|
case AUTHORIZE:
|
||||||
|
getService().authorize(ctx.channel(), JSONUtil.toBean(wsBaseReq.getData(), WSAuthorize.class));
|
||||||
|
log.info("主动认证 = " + msg.text());
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
log.info("未知类型");
|
log.info("未知类型");
|
||||||
|
|||||||
Reference in New Issue
Block a user