在Https上使用Express.js的Node.js上的ATS配置

时间:2018-05-18 07:16:37

标签: javascript ios node.js swift

我有一个 node.js 服务器必须从使用express来管理路由器的nod​​e.js应用程序接收json调用, iOS 应用程序调用路由器传递json值。我还补充一点,如果我在http工作,我没有任何问题!

我的问题是我无法配置苹果ATS。我该怎么办?

下面我输入了代码和与Info.plist相关的信息。

错误:

ATS Default Connection
2018-05-18 08:54:14.070 nscurl[7310:57407] NSURLSession/NSURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9802)
Result : FAIL
---

夫特:

func Login(Username: String, Password: String, completion: @escaping (Int) -> ())
    {
        let semaphore = DispatchSemaphore(value: 1)
        semaphore.wait()
        let db = Database() /* https://172.16.53.247:8989 */
        let json: [String: Any] = ["Username": "" + Username, "Password": "" + Password]
        let jsonData = try? JSONSerialization.data(withJSONObject: json)
        var request = URLRequest(url: URL(string: db.GetServerURL() + "/login")!)
        request.httpMethod = "POST"
        request.httpBody = jsonData
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            guard let data = data, error == nil else {
                print(error?.localizedDescription ?? "No data")
                completion(0)
                return
            }
            let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
            if let responseJSON = responseJSON as? [String: Any] {
                let read = responseJSON["return"]!
                let IdUser=Int(String(describing: read))!
                if (IdUser > 0) {

                    completion(IdUser)
                }
                else {
                    completion(0)
                }
            }

        }
        task.resume()
        semaphore.signal()
    }

Node.js服务器:

const fs = require('fs');
const http = require('http');
const https = require('https');
const express = require('express');

const port = 8989;

const options = {
    key: fs.readFileSync('./Config/server-key.pem'),
    cert: fs.readFileSync('./Config/server-cert.pem'),
};

var app = express();

var server = https.createServer(options, app).listen(port, function(){
  console.log("Express server listening on port " + port);
});

app.get('/', function (req, res) {
    res.writeHead(200);
    res.end("hello world\n");
});


module.exports = app;

ATS配置: enter image description here

1 个答案:

答案 0 :(得分:1)

我一直在寻找问题的解决方案,并发现ATS异常不适用于IP地址的问题。

所以我找到了两种可能的解决方案:

1)将您的IP地址转换为真实的域名。你可以使用xip.io     将本地IP地址“转换”为域名的服务。

2)完全关闭ATS:

<key>NSAppTransportSecurity</key>
   <dict>
       <key>NSAllowsArbitraryLoads</key>
       <true/>
   </dict>

虽然已知此解决方案易受攻击,但这是我在开发期间建议的唯一解决方案。