Securing php api to use in android application

前端 未结 7 632
余生分开走
余生分开走 2021-01-31 18:40

I am newbie to android development. I am using android studio for developing an application. Things i have done

  1. Created a DB with two ta
相关标签:
7条回答
  • 2021-01-31 19:05

    Use Session control mechanism all the above methods are also talking for the same. In Simple words use encrypted private key to detect the valid source

    0 讨论(0)
  • 2021-01-31 19:07

    You can choose you Authentication for PHP here:

    http://pear.php.net/packages.php?catpid=1&catname=Authentication

    I think that Basic or Digest Auth (+ https:// SSL with self signed certificate) will be good choose for you Rest-Service (PHP RESTful JSON API).

    For Android application I think the best choose for RestClient library is:

    http://square.github.io/retrofit/

    (I recommend you to keep all source code in one Language only -Java. You can easy create Java Rest Service and add Spring Security Authentication)

    0 讨论(0)
  • 2021-01-31 19:13

    First add the dependencies in build gradle (app)
    implementation 'com.mcxiaoke.volley:library:1.0.16' Then follow the below code:

     final ProgressDialog loading = ProgressDialog.show(getActivity(), "", "Please wait...", false, false);
            StringRequest stringRequest = new StringRequest(Request.Method.POST, ServerLinks.TOTAL_COUNT_URL, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    loading.dismiss();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
    
                        String status = jsonObject.getString("status");
    
                        if (status.equals("success")) {
                            String data = jsonObject.getString("data");
    
                            JSONObject jsonObject1 = new JSONObject(data);
                            String male = jsonObject1.getString("male");
                            String female = jsonObject1.getString("female");
    
    
                        } else {
                            // no_data_found.setVisibility(View.VISIBLE);
                            Toast.makeText(getActivity(), "No Data Found", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }//onResponse()
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    loading.dismiss();
                    //Toast.makeText(getActivity(), "Check Your Internet Connection", Toast.LENGTH_SHORT).show();
                }
            });
            int socketTimeout = 30000; // 30 seconds. You can change it
            RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    
            stringRequest.setRetryPolicy(policy);
            RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
            requestQueue.add(stringRequest);
    
    0 讨论(0)
  • 2021-01-31 19:16

    The only method to secure your api is making your android request to be unique .Collect more specific data from your app.

    1 - Get Android Unique ID -

    String UniqueID=Secure.getString(getActivity().getContentResolver(),Secure.ANDROID_ID);
    

    And pass it through your api Eg .

    http://192.168.100.9:8000/MobileApp/GET_DATA.php?yourvalue=something&id=UniqueID
    

    In your php, deny access if there is no Android Unique ID(should change with complex variable name).Eg :

    if($_REQUEST['UniqueID']=="" || strlen($_REQUEST['UniqueID'])<9){ //do something about abuse }else{ //your code }
    

    2 - Create your own random variable in Android App

    Create your own variable to decide to make sure the request comes from your app Eg:

    Random r = new Random();
    int i1 = r.nextInt(9999 - 1000) + 1000;
    

    And also pass this value via your request and validate when it comes to php .

    if($_REQUEST['r']>=1000 && $_REQUEST['r']<=9999){//}
    

    Deny request if not passing or wrong value.

    3 - Make sure requests come from Android

    I want to recommend to use free best php library http://mobiledetect.net/ Check whether it is from android and write deny function on invalid abuses.

    4 - Validate request via User-Agent string in PHP

    $agent = $_SERVER['HTTP_USER_AGENT'];
    $agent=strtolower($agent);
    
    if (strpos($agent, 'android') !== false) {
    $os = 'Android';
    }
    

    And deny if not from android in php.

    5 - Record the attackers

    You need to track if someone is breaking one of your above securities. Currently I am using ip-api.com to track attackers.

    you need to write deny function with mysql insert. according to ip-api, you will get

    1- Attackers' ip
    2- Attackers' geolocation
    3- Attackers' ISP

    So you can deny them statically.

    It is about to safe to use your api from android and almost denied pc requests. But three is a chance to break your app with reverse engineering like dex2jar or ShowJava and grab your simple data structure. As a programmer, above functions and codes are very easy for them and they will get in with fake data inputs.

    So you should not write a program with static values, such important link "http://192.168.100.9:8000/MobileApp/GET_DATA.php" as hard coded as in your app. You should split data,simple encrypt and get all of your main urls dynamically as above secured php/mysql api method.

    If you covered just like 2 step dynamic system, there is very very few chances to break in your system.
    I've one important left to say, if you are using for closed group of users , you should use request->approve system for each user for first time app registration by using their unique ID and easily deny access from the others.

    0 讨论(0)
  • 2021-01-31 19:17

    secure the APIs in order to be accessed by a 3rd party is a vast domain to be discussed. Most used mechanism to secure APIs is token based access control. It can be implemented in many ways.

    1st you need understand how it works. Refer this link and this link.

    Then try to look at how to implement it.

    Working example of implementing 'Token Based Authentication' using 'JSON Web Token (i.e. JWT)' in PHP and MySQL?

    If you need more in-depth example try this link as well.

    If you need more information let me know.

    0 讨论(0)
  • 2021-01-31 19:19
    I will simply say, 
    
    If you already using PHP OPP, then no need to go with any framework. It will be time consuming for you. 
    Solution is: "Use remember_token system as like laravel and yii. Authenticate to each method before filter."
    
    If you are using Core PHP. Try same code and include it before filter each method in your api
    
    0 讨论(0)
提交回复
热议问题