Android 应用互调的实现并添加自定义权限进行安全防护

最近在做一个安全漏洞修复的工作,场景是A应用必须由B应用调起,由于涉及到组件暴露所以我们需要考虑安全的问题,最后添加了自定义权限进行解决。

一、A应用

作为被调起者,需要暴露组件给B应用。所以A的清单文件中要添加自定义权限(注意:这里的权限级别至少是signature或者signatureOrSystem的,关于权限的说明以及为什么这个级别的就安全请查看此处),并且要给需要暴露的组件设置此权限。

(1)A应用的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.leeky.a_test_pemisstion_application">
    <!--自定义权限-->
    <permission
        android:name="com.bao.permission.SAFE_TEST"
        android:protectionLevel="signature" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <!--给暴露的组件设置自定义权限-->
        <activity android:name=".AActivity"
            android:permission="com.bao.permission.SAFE_TEST">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".TestActivity"
            android:permission="com.bao.permission.SAFE_TEST">
            <!-- 给这个activity添加外界访问这个activity的action属性,便于android系统查找 -->
            <intent-filter>
                <action android:name="com.leeky.a_test_pemisstion_application.intent.action.Test"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
        </activity>

    </application>

</manifest>

(2)给两个Activity设置布局,为了方便只放了一个标记页面的TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A应用-----Hello World!" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A应用-----Test" />

</LinearLayout>

二、B应用

作为调起者,首先需要声明A的自定义权限,不然程序会因为没有权限而奔溃。然后在Activity类中写跳转到A应用某一页面的代码(这里提供了setComponent跳转和利用action两种方式)。

(1)B应用的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.leeky.b_test_permission_application">
    <!--B应用申明A应用权限-->
    <uses-permission android:name="com.bao.permission.SAFE_TEST" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

(2)B应用主页面两种跳转的实现

package com.leeky.b_test_permission_application;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Button mButton,mButton2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton=findViewById(R.id.buttonPanel);
        mButton2=findViewById(R.id.buttonPanel2);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //应用间跳转方式一
                Intent intent = new Intent();
                intent.setComponent(new ComponentName("com.leeky.a_test_pemisstion_application","com.leeky.a_test_pemisstion_application.AActivity"));
                startActivity(intent);
            }
        });
        mButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //应用间跳转方式二
                Intent intent = new Intent(
                        "com.leeky.a_test_pemisstion_application.intent.action.Test");
                startActivity(intent);
            }
        });
    }
}

(3)B的主页面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/buttonPanel"
        android:text="跳转到A应用"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:id="@+id/buttonPanel2"
        android:text="跳转到A应用test页"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />


    <TextView
        android:layout_marginTop="100dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="B----Hello World!"/>

</LinearLayout>

三、最后我想说,这样实现存在一个问题是:如果调起的入口是启动页并且设置了自定义权限,被调起的A应用无法自己启动(现象是点击应用图标没反应,显示activity not found或者“应用不存在”)。如果A应用的启动页不暴露出去,A是可以自己启动的;但是好多情况下A的启动页恰好是被调起的入口,这样就尴尬了,目前没有找到解决办法,我会及时更新的,也希望大家去琢磨一下为什么会这样。

猜你喜欢

转载自blog.csdn.net/Wang_WY/article/details/83026893