zoukankan      html  css  js  c++  java
  • 关于BroadCastReceiver安全性的思考

    关于BroadCastReceiver安全性的思考 - 上善若水 - 博客频道 - CSDN.NET

    BroadCastReceiver是Android 四大组件之一,应用非常广泛,也非常简单,但是我们平时在使用的过程中忽略了一个安全问题。别人很容易通过反编译获取到我们应用中的广播,然后频繁的向你的App中发送广播,这个当然是我们不想看到的现象,那么如何避免应用中注册的广播响应其他应用发送的广播呢?在解决这个问题之前,我们先来了解一下如何发送一个广播。

     

    在Android中发送一个广播通常有两种方式:显示和隐式

    显式:

    1. Intent intent=new Intent(this,MyBroadCastReceiver.class);  
    2.     this.sendBroadcast(intent);  
    Intent intent=new Intent(this,MyBroadCastReceiver.class);
        this.sendBroadcast(intent);


    所谓显示,就是通过制定你要发送的哪个广播,如上例中的MyBroadCastReceiver这个广播

    隐式:

    1. Intent intent=new Intent("com.demo.action");  
    2.     this.sendBroadcast(intent);  
    Intent intent=new Intent("com.demo.action");
        this.sendBroadcast(intent);


    所谓隐式就是通过action来匹配广播,对于匹配成功的广播就会响应

     

    对于显示的广播除非是别人故意攻击,一般很少出现响应别人的广播,但是对于隐式的广播就很容易出现上述问题,因为action很容易是一样的,一旦是一样的就出问题了。

     

    下面就来提出解决方案:

    第一种方案:

    在自己的应用中,在manifest.xml中注册receiver的时候加入export属性,如下:

    1. <receiver android:name="com.baroad.demo.MyBroadCastReceiver" android:exported="false">  
    2.             <intent-filter >  
    3.                <action android:name="com.demo.action"/>  
    4.                  
    5.            </intent-filter>  
    6.        </receiver>  
     <receiver android:name="com.baroad.demo.MyBroadCastReceiver" android:exported="false">
                 <intent-filter >
                    <action android:name="com.demo.action"/>
    
                </intent-filter>
            </receiver>


    加入这个属性之后,这个广播不会响应外部广播的

     

    第二种方案:

    自定义权限,在manifest.xml中加入自定义权限,然后再响应的BroadCastReceiver中加入这个权限即可

    1. <permission   
    2.         android:name="com.yzy.permission.STARTBROAD"    
    3.         android:protectionLevel="normal">    
    <permission
            android:name="com.yzy.permission.STARTBROAD"
            android:protectionLevel="normal">  


    然后将上面的权限注册到BroadCastReceiver

    1. <receiver android:name="com.baroad.demo.MyBroadCastReceiver"  android:permission="com.yzy.permission.STARTBROAD">  
    2.             <intent-filter >  
    3.                <action android:name="com.demo.action"/>  
    4.                  
    5.            </intent-filter>  
    6.        </receiver>  
     <receiver android:name="com.baroad.demo.MyBroadCastReceiver"  android:permission="com.yzy.permission.STARTBROAD">
                 <intent-filter >
                    <action android:name="com.demo.action"/>
    
                </intent-filter>
            </receiver>


    第三种方案:

    前面两种方案都是在接收广播的地方设置,第三种是在发送方便的地方设置,设置你的广播对哪个报名有效

    1. Intent intent=new Intent("com.demo.action");  
    2.     intent.setPackage("com.two.demo");  
    3.     this.sendBroadcast(intent);  
    Intent intent=new Intent("com.demo.action");
        intent.setPackage("com.two.demo");
        this.sendBroadcast(intent);


    第四种方案:

    使用LocalBroadcastManager来实现广播

    1. private LocalBroadcastManager mLocalBroadcastManager;   
    2.  private BroadcastReceiver mReceiver;  
     private LocalBroadcastManager mLocalBroadcastManager;
      private BroadcastReceiver mReceiver;



    1. @Override  
    2. protected void onCreate(Bundle savedInstanceState)  
    3. {  
    4.   super.onCreate(savedInstanceState);  
    5.   setContentView(R.layout.activity_main);  
    6.     
    7.   IntentFilter filter = new IntentFilter();    
    8.   filter.addAction("com.demo.action");    
    9.   
    10.   mReceiver = new MyBroadCastReceiver();    
    11.  mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);  
    12.  mLocalBroadcastManager.registerReceiver(mReceiver, filter);  
    13. }  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.demo.action");  
    
        mReceiver = new MyBroadCastReceiver();
       mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
       mLocalBroadcastManager.registerReceiver(mReceiver, filter);
      }



    1. public void start(View view)  
    2. {  
    3.  mLocalBroadcastManager.sendBroadcast(new Intent("com.demo.action"));  
    4. }  
      public void start(View view)
      {
       mLocalBroadcastManager.sendBroadcast(new Intent("com.demo.action"));
      }



    1. @Override  
    2. protected void onDestroy() {  
    3.    mLocalBroadcastManager.unregisterReceiver(mReceiver);  
    4.    super.onDestroy();  
    5. }   
    @Override
    protected void onDestroy() {
       mLocalBroadcastManager.unregisterReceiver(mReceiver);
       super.onDestroy();
    } 


    好了,就介绍到这里吧,通过以上四种方案,就可以避免自己的应用程序响应其他应用的广播

  • 相关阅读:
    [BZOJ3884] 上帝与集合的正确用法
    [BZOJ3518] 点组计数
    [BZOJ3601] 一个人的数论
    [BZOJ3529] [Sdoi2014]数表
    原生js实现无缝滚动轮播图-点击页码即刻显示该页码的内容
    原生js实现无缝滚动轮播图
    vue封装tinymce富文本组件,图片上传回调方法
    vue-cli项目结合Element-ui基于cropper.js封装vue图片裁剪组件
    js实现多文件上传(二)-- 拖拽上传
    js实现多文件上传(一)-- 图片转base64回显
  • 原文地址:https://www.cnblogs.com/seven1979/p/4346060.html
Copyright © 2011-2022 走看看