限制对内容提供商的访问

Fab*_*biF 7 android android-contentprovider

我没有找到一个要求与我相同限制的帖子.

我有一个应用程序,它向其他应用程序(称为客户端应用程序)提供内容提供程序(称为主应用程序).我想限制从客户端应用程序访问内容提供程序,以仅支持插入和可能的查询方法.

我不想要的:

  • 使内容提供者保密,因为主要目标是为客户端应用程序提供数据库.
  • 使用客户端应用程序的签名限制访问权限,因为任何人都必须能够编写使用主应用程序平台的客户端应用程序.

我看到的最明显的解决方案是编写两个内容提供程序,一个是主应用程序的完全访问私有,另一个是受限公共.但我认为这绝对不是一个正确的方法.

根据Google发布的帖子,我打算Binder.getCallingUid()在内容提供商调用中使用,以检测调用是否来自主应用程序.因此,如果调用不是来自主应用程序,我无法在更新删除方法中执行任何操作.

我如何才能让主应用程序UID进行比较?如果有可能,这个解决方案是否安全?

谢谢你的建议.

nan*_*esh 8

使用protectionLevel签名定义如下所示的权限,此WRITE权限仅限于使用相同私钥签名的应用程序

<permission android:name="com.yourapp.WRITE.PERMISSION"
    android:protectionLevel="signature"
        android:label="@string/permission_label"
        android:description="@string/permission_desc">
</permission>

<permission android:name="com.yourapp.READ.PERMISSION"
        android:label="@string/permission_label"
        android:description="@string/permission_desc">
</permission>
Run Code Online (Sandbox Code Playgroud)

然后在contentprovider标记中使用读写权限标记.您可以强制执行读取权限,也可以将其全部删除

android:readPermission="com.yourapp.READ.PERMISSION"
android:writePermission="com.yourapp.WRITE.PERMISSION"
Run Code Online (Sandbox Code Playgroud)

因此,只有通过相同签名签名的应用才能使用您的内容提供商

编辑:

也许你可以用它

 private Collection<String> getCallingPackages() {
     int caller = Binder.getCallingUid();
     if (caller == 0) {
         return null;
     }
     return Lists.newArrayList(mContext.getPackageManager().getPackagesForUid(caller));
 }
Run Code Online (Sandbox Code Playgroud)

并检查此列表中是否包含您的包名称.我觉得这很安全