How add android ssl certificate for https request in ionic?

痴心易碎 提交于 2020-01-13 10:13:22

问题


In ionic simulator all http and https working fine but in real device release version its stop working.

Many people adviced to add SSL certificate for release version but I dont know how to add this ?

I have tries all this to make https request ?

<access origin="*"/>
<access origin="*"/>
<allow-navigation href="*"/>
<allow-intent href="*" />

Also added whitelist plugin but not working.

Also tried this but not working

<meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">

回答1:


Yes, this is the most common issue raised by all the developers.. Finally I found the answer and here it is..

SSL certificate is enough for a website to connect via HTTPS but coming to hybrid Apps like App build on ionic, SSL itself is not enough and you need a code signing certificate which costs few hundred bucks..

Other alternative is using a self signed certificate which you ca create FREE, here also you can able to connect to ionic web version via HTTPS but again after compiling, the apk file get halted to connect to DB server..

Here is the trick where you can add the HTTPS port or HTTP api link in the services using self signed certification..

Exactly follow the commands:

  • ionic serve
  • ionic build
  • ionic build --prod
  • cordova platform add android

After this, stop compiling and relax .. Now you will find a folder named platform as the path is as follows project/platforms/android/CordovaLib/src/org/apache/cordova/CordovaWebViewClient.java

DO THE FOLLOWING MODIFICATION AS IT IS

public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
  final String packageName = this.cordova.getActivity().getPackageName();
  final PackageManager pm = this.cordova.getActivity().getPackageManager();

  ApplicationInfo appInfo;
  try {
    appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
    if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
      // debug = true
      handler.proceed();
      return;
    } else {
      // debug = false
      // THIS IS WHAT YOU NEED TO CHANGE:
      // 1. COMMENT THIS LINE
      // super.onReceivedSslError(view, handler, error);
      // 2. ADD THESE TWO LINES
      // ---->
      handler.proceed();
      return;
      // <----
    }
  } catch (NameNotFoundException e) {
    // When it doubt, lock it out!
    super.onReceivedSslError(view, handler, error);
  }
}

THEN FINALLY

  • cordova build --release android

After this, the usual commands jarsiner and zipalign

you apk should run in HTTPS port in SSL withh your self signed certificate..



来源:https://stackoverflow.com/questions/40147383/how-add-android-ssl-certificate-for-https-request-in-ionic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!