diff --git a/src/main/java/com/iot/transport/jt808/service/handler/terminal/AuthenticationHandler.java b/src/main/java/com/iot/transport/jt808/service/handler/terminal/AuthenticationHandler.java index b5e2f4d..8f601b3 100644 --- a/src/main/java/com/iot/transport/jt808/service/handler/terminal/AuthenticationHandler.java +++ b/src/main/java/com/iot/transport/jt808/service/handler/terminal/AuthenticationHandler.java @@ -11,48 +11,57 @@ import com.iot.transport.jt808.service.handler.MessageHandler; /** * 终端鉴权 ==> 平台通用应答 + * * @author huaxl */ public class AuthenticationHandler extends MessageHandler { private final Logger logger = LoggerFactory.getLogger(getClass()); - public AuthenticationHandler() { super(); } @Override public void process(DataPack packageData) { - // - + // + PackHead header = packageData.getPackHead(); - logger.info("[终端鉴权],msgid={}, phone={},flowid={}", header.getId(), header.getTerminalPhone(), header.getFlowId()); + logger.info("[终端鉴权],msgid={}, phone={},flowid={}", header.getId(), header.getTerminalPhone(), + header.getFlowId()); try { - + AuthenticationPack msg = new AuthenticationPack(packageData); - //this.msgProcessService.processAuthMsg(authenticationMsg); - log.debug("终端鉴权:{}", msg); + // this.msgProcessService.processAuthMsg(authenticationMsg); + log.info("终端鉴权:{}", msg); - final String sessionId = Session.buildId(msg.getChannel()); - Session session = sessionManager.findBySessionId(sessionId); - if (session == null) { - session = Session.buildSession(msg.getChannel(), msg.getPackHead().getTerminalPhone()); - } - session.setAuthenticated(true); - session.setTerminalPhone(msg.getPackHead().getTerminalPhone()); - sessionManager.put(session.getId(), session); + boolean isSuccess = false; + if ("testtoken".equals(msg.getAuthCode())) { + isSuccess = true; + } else { + logger.warn("终端鉴权失败: msgId={}, phone={}, token={}", header.getId(), header.getTerminalPhone(), + msg.getAuthCode()); + } + + final String sessionId = Session.buildId(msg.getChannel()); + Session session = sessionManager.findBySessionId(sessionId); + if (session == null) { + session = Session.buildSession(msg.getChannel(), msg.getPackHead().getTerminalPhone()); + } + session.setAuthenticated(isSuccess); + session.setTerminalPhone(msg.getPackHead().getTerminalPhone()); + sessionManager.put(session.getId(), session); + + ServerBodyPack respMsgBody = new ServerBodyPack(); + respMsgBody.setReplyCode(isSuccess ? ServerBodyPack.success : ServerBodyPack.failure); + respMsgBody.setReplyFlowId(msg.getPackHead().getFlowId()); + respMsgBody.setReplyId(msg.getPackHead().getId()); + int flowId = super.getFlowId(msg.getChannel()); + byte[] bs = this.msgEncoder.encode4ServerCommonRespMsg(msg, respMsgBody, flowId); + super.send2Client(msg.getChannel(), bs); - ServerBodyPack respMsgBody = new ServerBodyPack(); - respMsgBody.setReplyCode(ServerBodyPack.success); - respMsgBody.setReplyFlowId(msg.getPackHead().getFlowId()); - respMsgBody.setReplyId(msg.getPackHead().getId()); - int flowId = super.getFlowId(msg.getChannel()); - byte[] bs = this.msgEncoder.encode4ServerCommonRespMsg(msg, respMsgBody, flowId); - super.send2Client(msg.getChannel(), bs); - } catch (Exception e) { - logger.error("[终端鉴权]错误,err={}", e.getMessage()); + logger.error("[终端鉴权]错误,err={}", e.getMessage()); } }