Exemple #1
0
 /**
  * Return the set of allowed {@link HttpMethod HTTP methods}, as specified by the {@code Allow}
  * header.
  *
  * <p>Returns an empty set when the allowed methods are unspecified.
  */
 public Set<HttpMethod> getAllow() {
   String value = getFirst(ALLOW);
   if (StringUtil.isNotBlank(value)) {
     List<HttpMethod> allowedMethod = new ArrayList<HttpMethod>(5);
     String[] tokens = value.split(",\\s*");
     for (String token : tokens) {
       allowedMethod.add(HttpMethod.valueOf(token));
     }
     return EnumSet.copyOf(allowedMethod);
   } else {
     return EnumSet.noneOf(HttpMethod.class);
   }
 }
Exemple #2
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");
 }
 protected void addHeaders(HttpHeaders headers, HttpRequestBase uriRequest) {
   if (headers == null) {
     headers = new HttpHeaders();
   }
   // Add default accept headers
   if (!headers.containsKey(HttpHeaders.ACCEPT)) {
     headers.set(HttpHeaders.ACCEPT, "*/*");
   }
   // Add default user agent
   if (!headers.containsKey(HttpHeaders.USER_AGENT)) {
     headers.set(HttpHeaders.USER_AGENT, "apache/httpclient4");
   }
   for (Iterator<Entry<String, List<String>>> headerIterator = headers.entrySet().iterator();
       headerIterator.hasNext(); ) {
     Entry<String, List<String>> header = headerIterator.next();
     if (HttpHeaders.COOKIE.equalsIgnoreCase(header.getKey())) {
       uriRequest.addHeader(header.getKey(), StringUtil.join(header.getValue(), ';'));
     } else {
       for (String headerValue : header.getValue()) {
         uriRequest.addHeader(header.getKey(), headerValue != null ? headerValue : "");
       }
     }
   }
 }
 @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);
 }
Exemple #5
0
 /**
  * Set the set of allowed {@link HttpMethod HTTP methods}, as specified by the {@code Allow}
  * header.
  */
 public void setAllow(Set<HttpMethod> allowedMethods) {
   set(ALLOW, StringUtil.join(allowedMethods, ','));
 }
Exemple #6
0
 /** Set the (new) value of the {@code Access-Control-Allow-Methods} response header. */
 public void setAccessControlAllowMethods(List<HttpMethod> allowedMethods) {
   set(ACCESS_CONTROL_ALLOW_METHODS, StringUtil.join(allowedMethods, ','));
 }