如何在ionic中为https请求添加android ssl证书? [英] How add android ssl certificate for https request in ionic?

查看:268
本文介绍了如何在ionic中为https请求添加android ssl证书?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ionic模拟器中,所有http和https都可以正常工作,但是在实际设备发行版中,它会停止工作.

许多人建议为发行版本添加SSL证书,但我不知道如何添加它?

我已经尝试了所有这些以发出https请求?

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

还添加了白名单插件,但不起作用.

也尝试过此方法,但不起作用

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

解决方案

是的,这是所有开发人员提出的最普遍的问题.

SSL证书足以使网站通过HTTPS进行连接,但要进入混合应用程序(如基于离子的应用程序构建的应用程序),SSL本身还不够,您需要花费几百美元的代码签名证书.

其他选择是使用您可以免费创建的自签名证书,在这里您还可以通过HTTPS连接到离子网络版本,但是再次编译后,apk文件被暂停以连接到DB服务器.

这是技巧,您可以使用自签名证书在服务中添加HTTPS端口或HTTP api链接.

完全遵循以下命令:

  • 离子服务
  • 离子生成
  • ionic build --prod
  • cordova平台添加android

此后,停止编译并放松..现在您将找到一个名为platform的文件夹,路径如下所示 项目/平台/android/CordovaLib/src/org/apache/cordova/CordovaWebViewClient.java

按原样进行以下修改

 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);
  }
} 

最后一刻

  • cordova build --release android

此后,通常的命令jarsiner和zipalign

您的apk应该使用您的自签名证书在SSL的HTTPS端口中运行.

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'">

解决方案

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..

这篇关于如何在ionic中为https请求添加android ssl证书?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆