public static final class Methods { public static final String CONNECT = HttpMethod.CONNECT.name(); public static final String DELETE = HttpMethod.DELETE.name(); public static final String GET = HttpMethod.GET.name(); public static final String HEAD = HttpMethod.HEAD.name(); public static final String OPTIONS = HttpMethod.OPTIONS.name(); public static final String PATCH = HttpMethod.PATCH.name(); public static final String POST = HttpMethod.POST.name(); public static final String PUT = HttpMethod.PUT.name(); public static final String TRACE = HttpMethod.TRACE.name(); private Methods() {} }
@Override protected void channelRead0(ChannelHandlerContext ctx, WeixinRequest request) throws WeixinException { final AesToken aesToken = request.getAesToken(); if (aesToken == null || (StringUtil.isBlank(request.getSignature()) && StringUtil.isBlank(request.getMsgSignature()))) { ctx.writeAndFlush(HttpUtil.createHttpResponse(BAD_REQUEST)) .addListener(ChannelFutureListener.CLOSE); return; } /** 公众平台:无论Get,Post都带signature参数,当开启aes模式时带msg_signature参数 企业号:无论Get,Post都带msg_signature参数 */ if (request.getMethod().equals(HttpMethod.GET.name())) { if (!StringUtil.isBlank(request.getSignature()) && MessageUtil.signature(aesToken.getToken(), request.getTimeStamp(), request.getNonce()) .equals(request.getSignature())) { ctx.write(new SingleResponse(request.getEchoStr())); return; } if (!StringUtil.isBlank(request.getMsgSignature()) && MessageUtil.signature( aesToken.getToken(), request.getTimeStamp(), request.getNonce(), request.getEchoStr()) .equals(request.getMsgSignature())) { ctx.write( new SingleResponse( MessageUtil.aesDecrypt(null, aesToken.getAesKey(), request.getEchoStr()))); return; } ctx.writeAndFlush(HttpUtil.createHttpResponse(FORBIDDEN)) .addListener(ChannelFutureListener.CLOSE); return; } else if (request.getMethod().equals(HttpMethod.POST.name())) { if (!StringUtil.isBlank(request.getSignature()) && !MessageUtil.signature(aesToken.getToken(), request.getTimeStamp(), request.getNonce()) .equals(request.getSignature())) { ctx.writeAndFlush(HttpUtil.createHttpResponse(FORBIDDEN)) .addListener(ChannelFutureListener.CLOSE); return; } if (request.getEncryptType() == EncryptType.AES && !MessageUtil.signature( aesToken.getToken(), request.getTimeStamp(), request.getNonce(), request.getEncryptContent()) .equals(request.getMsgSignature())) { ctx.writeAndFlush(HttpUtil.createHttpResponse(FORBIDDEN)) .addListener(ChannelFutureListener.CLOSE); return; } } else { ctx.writeAndFlush(HttpUtil.createHttpResponse(METHOD_NOT_ALLOWED)) .addListener(ChannelFutureListener.CLOSE); return; } WeixinMessageTransfer messageTransfer = MessageTransferHandler.parser(request); ctx.channel().attr(Consts.MESSAGE_TRANSFER_KEY).set(messageTransfer); messageDispatcher.doDispatch(ctx, request, messageTransfer); }