|
@@ -4,6 +4,7 @@ import com.dzdy.dim.contants.Session;
|
|
|
import com.dzdy.dim.protocol.command.JWTUser;
|
|
|
import com.dzdy.dim.protocol.request.LoginRequestPacket;
|
|
|
import com.dzdy.dim.protocol.response.LoginResponsePacket;
|
|
|
+import com.dzdy.dim.util.ApplicationContextUtils;
|
|
|
import com.dzdy.dim.util.IDUtil;
|
|
|
import com.dzdy.dim.util.JWTUtil;
|
|
|
import com.dzdy.dim.util.SessionUtil;
|
|
@@ -11,20 +12,25 @@ import io.jsonwebtoken.ExpiredJwtException;
|
|
|
import io.netty.channel.ChannelHandler;
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
import io.netty.channel.SimpleChannelInboundHandler;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
/**
|
|
|
* 登录请求处理器
|
|
|
+ *
|
|
|
* @author : wangzhiyong
|
|
|
* @date : 2019/5/28 14:38
|
|
|
* description :
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@ChannelHandler.Sharable
|
|
|
public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginRequestPacket> {
|
|
|
|
|
|
public static final LoginRequestHandler INSTANCE = new LoginRequestHandler();
|
|
|
|
|
|
- private LoginRequestHandler() {
|
|
|
+ private final JWTUtil jwtUtil;
|
|
|
|
|
|
+ private LoginRequestHandler() {
|
|
|
+ jwtUtil = ApplicationContextUtils.getBean(JWTUtil.class);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -34,14 +40,14 @@ public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginReques
|
|
|
|
|
|
String token = packet.getToken();
|
|
|
try {
|
|
|
- JWTUser user = JWTUtil.decode(token);
|
|
|
+ JWTUser user = jwtUtil.decode(token);
|
|
|
if (null != user) {
|
|
|
responsePacket.setSuccess(true);
|
|
|
String sessionId = IDUtil.randomId();
|
|
|
Session session = new Session(sessionId, user.getUser_name(), null);
|
|
|
responsePacket.setSession(session);
|
|
|
SessionUtil.bind(session, ctx.channel());
|
|
|
- System.out.println(String.format("[%s]登录成功", user.getUser_name()));
|
|
|
+ log.info(String.format("[%s] login success .", user.getUser_name()));
|
|
|
}
|
|
|
} catch (ExpiredJwtException e) {
|
|
|
responsePacket.setSuccess(false);
|
|
@@ -55,7 +61,7 @@ public class LoginRequestHandler extends SimpleChannelInboundHandler<LoginReques
|
|
|
|
|
|
@Override
|
|
|
public void channelInactive(ChannelHandlerContext ctx) {
|
|
|
- System.out.println("客户端和客户端断开连接");
|
|
|
+ log.info("有客户端与服务端断开连接");
|
|
|
SessionUtil.unBind(ctx.channel());
|
|
|
}
|
|
|
}
|