我有一台 node.js 服务器,该服务器必须从使用express管理路由器的node.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
---
swift :
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配置:
最佳答案
我一直在寻找解决您问题的方法,但发现一个问题是ATS异常不适用于IP地址。
因此,我找到了两种可能的解决方案:
1)将您的IP地址转换为真实域名。您可以使用xip.io
服务将本地IP地址“转换”为域名。
2)完全关闭ATS:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
虽然此解决方案被称为易受攻击的,但它是我在开发期间推荐的唯一解决方案。
关于javascript - Node.js上的ATS配置以及Https上的Express.js,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50405806/