【问题标题】:Android In app billing - remove Security class dependencyAndroid In app billing - 移除安全类依赖
【发布时间】:2012-08-27 15:21:50
【问题描述】:

我正在使用 In App Billing 示例应用将此功能添加到我的应用程序中。 在我完成将它添加到我的应用程序并测试所有工作后,我注意到Security class 中的评论:

与安全相关的方法。为了安全实施,所有 此代码应在与之通信的服务器上实现 设备上的应用程序。为了简单和 这个例子的清晰度,这个代码被包含在这里并被执行 在设备上。如果您必须在手机上验证购买,您 应该混淆这段代码,使攻击者更难 用将所有购买视为已验证的存根替换代码。

正如 Google 建议的那样,我在服务器端进行购买验证,因此我的项目中确实不需要 Security 类。 问题是,我不知道如何删除 Security 类中的 BillingService 类依赖项。

我首先删除了 Security 类,并跟踪了 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);
        }
    }

如果有人可以在不使用 Security 类的情况下分享他/她的 purchaseStateChanged 方法(基于应用内计费示例应用),我会很高兴。

【问题讨论】:

    标签: android in-app-purchase in-app-billing


    【解决方案1】:

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

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

    我保留了 Security 类并让它现在管理对服务器的调用,而不是它最初在示例中执行的操作。因此,当您看到对 Security 的调用时,我会调用我的服务器并执行签名验证。

    /**
     * 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);
                }
            }
    
    });        
    

    }

    【讨论】:

    • 所以基本上你还在使用 Security 类?您的回调和 VerifiedPurchase 都基于它。无论如何,看起来是我的问题的一个很好的解决方案。谢谢!
    • 我有一个名为 Security 的类,它是从示例中修改的。签名验证全部移至服务器。所以我只保留了 Security.VerifiedPurchase 之类的东西,因为它们似乎可以很好地让 BillingService 发送通知并使用计费服务确认购买。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多