Пример #1
0
 /**
  * 获取登录企业号官网的url
  *
  * @param corpId <font color="red">oauth授权的corpid</font>
  * @param targetType 登录跳转到企业号后台的目标页面
  * @param agentId 授权方应用id 小余1时则不传递
  * @return 登陆URL
  * @see <a
  *     href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%99%BB%E5%BD%95%E4%BC%81%E4%B8%9A%E5%8F%B7%E5%AE%98%E7%BD%91%E7%9A%84url">获取登录企业号官网的url</a>
  * @throws WeixinException
  */
 public String getLoginUrl(String corpId, LoginTargetType targetType, int agentId)
     throws WeixinException {
   Token token = cacheStorager.lookup(getLoginTicketCacheKey(corpId));
   if (token == null || StringUtil.isBlank(token.getAccessToken())) {
     throw new WeixinException("maybe oauth first?");
   }
   String oauth_loginurl_uri = getRequestUri("oauth_loginurl_uri");
   JSONObject obj = new JSONObject();
   obj.put("login_ticket", token.getAccessToken());
   obj.put("target", targetType.name());
   if (agentId > 0) {
     obj.put("agentid", agentId);
   }
   WeixinResponse response =
       weixinExecutor.post(
           String.format(oauth_loginurl_uri, providerTokenManager.getAccessToken()),
           obj.toJSONString());
   return response.getAsJson().getString("login_url");
 }
Пример #2
0
 @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);
 }