2012-08-27 42 views
1

我正在使用In App Billing示例应用程序将此功能添加到我的应用程序。 当我完成它添加到我的应用程序,并测试了所有的工作,我注意到在这个Security class评论:Android在应用程序结算 - 删除安全类依赖

安全相关的方法。为了安全实施,所有 此代码应在与设备上的应用程序通信的服务器上实施。为了简化和本示例的清晰起见,此代码包括在此处并在设备上执行 。如果您必须在手机上验证购买内容,则您应该对此代码进行混淆处理,以便让攻击者难以用 替换具有将所有购买视为已验证的存根的代码。

正如谷歌所说,我在服务器端进行购买验证,所以我真的不需要我的项目中的安全类。 问题是,我无法弄清楚如何删除Security类中的BillingService类依赖项。

我开始删除安全类并按照除在一个地方它的使用,我可以很容易地消除在BillingService有错误和大多数地方:

private void purchaseStateChanged(int startId, String signedData, String signature) { 
     ArrayList<Security.VerifiedPurchase> purchases; 
     purchases = Security.verifyPurchase(signedData, signature); 
     if (purchases == null) { 
      return; 
     } 

     ArrayList<String> notifyList = new ArrayList<String>(); 
     for (VerifiedPurchase vp : purchases) { 
      if (vp.notificationId != null) { 
       notifyList.add(vp.notificationId); 
      } 
      ResponseHandler.purchaseResponse(this, vp.purchaseState, vp.productId, 
        vp.orderId, vp.purchaseTime, vp.developerPayload); 
     } 
     if (!notifyList.isEmpty()) { 
      String[] notifyIds = notifyList.toArray(new String[notifyList.size()]); 
      confirmNotifications(startId, notifyIds); 
     } 
    } 

会爱,如果有人可以分享他/她的purchaseStateChanged方法(基于应用内结算示例应用程序),无需使用安全类。

回答

1

所以这就是我所做的。首先,对BillingService的调用发生在应用程序主线程上,因此您需要在后台线程中发出您的服务器调用。我选择完成主线程的处理,因为我不确定在后台线程上调用诸如'confirmNotifications'之类的方法会产生什么影响。

我创建了一个回调接口VerifyTransactionCompletion,可以在远程调用完成后将其分派回主线程。

我围绕安全类,让它现在管理对服务器的调用,而不是它最初在示例中执行的操作。因此,当您看到对安全性的呼叫时,这就是我呼叫到我的服务器并执行签名验证的地方。

/** 
* Callback interface to <em>finish</em> processing a transaction once the remote 
* servers have processed it. 
*/ 
public interface VerifyTransactionCompletion { 
    public void transactionVerified(List<Security.VerifiedPurchase> purchases); 
} 

private void purchaseStateChanged(final int startId, String signedData, String signature) { 
    // verifyPurchase issues remote call to server (in a background thread), then 
    // calls transactionVerified on the main thread to continue processing. 
    Security.verifyPurchase(signedData, signature, new VerifyTransactionCompletion() { 

     @Override 
    public void transactionVerified(List<VerifiedPurchase> purchases) { 
      if (purchases == null) { 
       return; 
      } 

      ArrayList<String> notifyList = new ArrayList<String>(); 
      for (VerifiedPurchase vp : purchases) { 
       if (vp.notificationId != null) { 
        notifyList.add(vp.notificationId); 
       } 
       ResponseHandler.purchaseResponse(BillingService.this, vp.purchaseState, vp.productId, 
         vp.orderId, vp.purchaseTime, vp.developerPayload); 
      } 
      if (!notifyList.isEmpty()) { 
       String[] notifyIds = notifyList.toArray(new String[notifyList.size()]); 
       confirmNotifications(startId, notifyIds); 
      } 
     } 

});   

}

+0

所以基本上你还是使用安全类?你的回调和VerifiedPurchase都是基于它的。无论如何,看起来像是我的问题很好的解决方案。谢谢! –

+1

我有一个名为Security的类,它是从示例中修改的。签名验证全部移至服务器。所以我只保留了诸如Security.VerifiedPurchase之类的东西,因为他们似乎在让BillingService发送通知并使用结算服务确认购买行为方面工作得很好。 –