【问题标题】:Not able to connect to backend over https from ionic android app无法从 ionic android 应用程序通过 https 连接到后端
【发布时间】:2018-03-08 11:43:27
【问题描述】:

无法在通过 ionic CLI 创建的 Android 应用上通过“https”连接到后端。

它在浏览器上运行良好,在安卓手机上的调试模式下运行良好。只是在发布模式下不起作用。它甚至适用于“http”,但不适用于“https”。

而且我的 SSL 证书不是自签名的。它是正确购买的证书,所有 SSL 检查员都说没问题。

尝试了我在互联网上可以找到的所有解决方案。

  1. 已安装白名单插件。
  2. 重新安装白名单插件。
  3. 为 default-src 添加了带有“*”的 Content-security-policy。

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

  1. 已添加<allow-intent href="*" /> <allow-navigation href="*" />

看起来应用程序本身阻止了请求发出。 似乎没有任何工作。请帮忙。

【问题讨论】:

  • 你能分享一下你遇到了什么错误
  • 如何在手机上调试并查看错误。我什至无法通过 chrome 扩展进行检查,因为它处于发布模式,并且在调试模式下工作正常。
  • @PrateekJain 你解决了这个问题吗,对我来说,我的 API 是 https 和基于证书的身份验证,我收到错误代码 0 问题。
  • @Mrunal 不,我无法解决这个问题。由于时间限制,就把它留在那里。

标签: android ionic-framework hybrid-mobile-app content-security-policy


【解决方案1】:

非常简单的解决方案是,在您的 API 中允许两个端口,例如

3001 用于 SSL / HTTPS 和 3002 用于 HTTP.. 这样您的应用程序就可以正常运行,并且您的网站可以在 HTTPS 中完美运行

我的代码如下,效果很好:

var express = require('express');
var DataController = require('./user/DataController');
var UserController = require('./user/UserController');
var db = require('./database/database-db'); 
var cors = require('cors');
var app = express();

app.use(cors());
app.use(function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});

app.use('/user', UserController);
app.use('/data', DataController);
app.get('/', function(req, res){
res.send("Welcome to the secure mobile and web development world");
});

// This settings are for HTTPS, SSL web applications.

// var https = require("https");
// var fs = require("fs");

// var options = {
//   key: fs.readFileSync("/home/path/ssl/keys/key.key"),
//   cert: fs.readFileSync("/home/path/ssl/certs/crt.crt")
  
//   };

// https.createServer(options,app).listen(3001);
// console.log('Welcome to the security world')


// This settings are only for HTTP sites

// var http = require("http");
// var fs = require("fs");

// http.createServer(app).listen(3001);
// console.log('Welcome to the security')



//This settings are for both HTTPS,HTTP SSL web applications.

var https = require("https");
var http = require("http");
var fs = require("fs");

var options = {
  key: fs.readFileSync("/home/path/ssl/keys/key.key"),
  cert: fs.readFileSync("/home/path/ssl/keys/crt.crt")
  };

https.createServer(options,app).listen(3001);
console.log('Welcome to the security world')
http.createServer(app).listen(3002);
console.log('Welcome to the proxy world')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 2016-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2017-11-30
    相关资源
    最近更新 更多