AuthServerHandler.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.inspur.netty.handler;
  2. import com.inspur.device.domain.vo.SmsbDeviceVo;
  3. import com.inspur.device.service.ISmsbDeviceService;
  4. import com.inspur.netty.message.push.PushMessageType;
  5. import com.inspur.netty.util.NettyConstants;
  6. import io.netty.buffer.ByteBuf;
  7. import io.netty.buffer.Unpooled;
  8. import io.netty.channel.Channel;
  9. import io.netty.channel.ChannelHandlerContext;
  10. import io.netty.channel.ChannelInboundHandlerAdapter;
  11. import lombok.extern.slf4j.Slf4j;
  12. import org.dromara.common.core.utils.SpringUtils;
  13. import org.dromara.common.core.utils.StringUtils;
  14. import java.nio.charset.Charset;
  15. /**
  16. * 设备鉴权 handler
  17. *
  18. * @author lihao16
  19. */
  20. @Slf4j
  21. public class AuthServerHandler extends ChannelInboundHandlerAdapter {
  22. private static final ISmsbDeviceService smsbDeviceService = SpringUtils.getBean(ISmsbDeviceService.class);
  23. /**
  24. * 当客户端连接服务器完成就会触发该方法
  25. */
  26. @Override
  27. public void channelActive(ChannelHandlerContext ctx) throws Exception {
  28. log.info("AuthServerHandler: channelId = " + ctx.channel().id() + ",login channelGroup");
  29. }
  30. /**
  31. * 此方法现在只会在接收到一条完整的消息时被调用(已被解码器处理过)。
  32. * 框架会自动释放 msg 这个 ByteBuf。
  33. *
  34. * @param ctx 上下文
  35. * @param msg 一条完整的、去除了分隔符的消息
  36. */
  37. @Override
  38. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
  39. // 解码器已经将 "####" 分隔符去除,这里得到的是完整的消息体
  40. String message = ((ByteBuf) msg).toString(Charset.forName("utf-8"));
  41. if (StringUtils.isEmpty(message)) {
  42. log.warn("AuthServerHandler: 从 channelId = {} 收到解码后的空消息", ctx.channel().id());
  43. return;
  44. }
  45. log.info("AuthServerHandler: 接收到客户端的完整消息: {}", message);
  46. // 获取消息中的SN 256数据
  47. String identifier = message.split("/")[0];
  48. if (StringUtils.isEmpty(identifier)) {
  49. log.warn("AuthServerHandler: 无法从消息 {} 中解析出设备标识", message);
  50. // 消息格式不正确,关闭连接
  51. ctx.close();
  52. return;
  53. }
  54. if (validateDevice(identifier)) {
  55. ctx.fireChannelRead(message);
  56. } else {
  57. // 发送消息鉴权失败
  58. String replayMsg = identifier + PushMessageType.INIT_REPLAY.getValue() + "/fail:auth fail";
  59. ByteBuf byteBuf = Unpooled.copiedBuffer(replayMsg + NettyConstants.DATA_PACK_SEPARATOR, Charset.forName("utf-8"));
  60. Channel channel = ctx.channel();
  61. channel.writeAndFlush(byteBuf);
  62. ctx.close();
  63. }
  64. }
  65. private boolean validateDevice(String identifier) {
  66. // 根据Mac地址查询设备是否在平台录入
  67. SmsbDeviceVo smsbDeviceVo = smsbDeviceService.getDeviceByIdentifier(identifier);
  68. if (null == smsbDeviceVo) {
  69. log.info("AuthServerHandler: device not in smsb plus,identifier = " + identifier);
  70. return false;
  71. }
  72. return true;
  73. }
  74. }