mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-17 00:03:48 +08:00
fix:优化ws握手同时认证 加上token为空判断 去掉异步认证
This commit is contained in:
@@ -29,11 +29,14 @@ import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
import org.springframework.context.ApplicationEventPublisher;
|
||||||
import org.springframework.scheduling.annotation.Async;
|
|
||||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.Date;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.locks.Condition;
|
import java.util.concurrent.locks.Condition;
|
||||||
@@ -141,10 +144,7 @@ public class WebSocketServiceImpl implements WebSocketService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 这里可以加异步或者事件去处理登录
|
|
||||||
// 因为登录和推送消息其实是不用同步的两个步骤 这样可以加快连接的速度不占用nio线程
|
|
||||||
@Override
|
@Override
|
||||||
@Async
|
|
||||||
public void authorize(Channel channel, WSAuthorize wsAuthorize) {
|
public void authorize(Channel channel, WSAuthorize wsAuthorize) {
|
||||||
//校验token
|
//校验token
|
||||||
boolean verifySuccess = loginService.verify(wsAuthorize.getToken());
|
boolean verifySuccess = loginService.verify(wsAuthorize.getToken());
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.abin.mallchat.custom.user.websocket;
|
package com.abin.mallchat.custom.user.websocket;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
import cn.hutool.extra.spring.SpringUtil;
|
import cn.hutool.extra.spring.SpringUtil;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.abin.mallchat.custom.user.domain.enums.WSReqTypeEnum;
|
import com.abin.mallchat.custom.user.domain.enums.WSReqTypeEnum;
|
||||||
@@ -19,6 +20,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
|
public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<TextWebSocketFrame> {
|
||||||
|
|
||||||
private WebSocketService webSocketService;
|
private WebSocketService webSocketService;
|
||||||
|
|
||||||
// 当web客户端连接后,触发该方法
|
// 当web客户端连接后,触发该方法
|
||||||
@Override
|
@Override
|
||||||
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
||||||
@@ -67,9 +69,10 @@ public class NettyWebSocketServerHandler extends SimpleChannelInboundHandler<Tex
|
|||||||
}
|
}
|
||||||
} else if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
|
} else if (evt == WebSocketServerProtocolHandler.ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) {
|
||||||
this.webSocketService.connect(ctx.channel());
|
this.webSocketService.connect(ctx.channel());
|
||||||
// 这里不用判断token是否为空 交给authorize方法中去判断
|
String token = NettyUtil.getAttr(ctx.channel(), NettyUtil.TOKEN);
|
||||||
// 在为空或者验证不通过的时候给前端返回相应的状态码
|
if (StrUtil.isNotBlank(token)) {
|
||||||
this.webSocketService.authorize(ctx.channel(), new WSAuthorize(NettyUtil.getAttr(ctx.channel(), NettyUtil.TOKEN)));
|
this.webSocketService.authorize(ctx.channel(), new WSAuthorize());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
super.userEventTriggered(ctx, evt);
|
super.userEventTriggered(ctx, evt);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user