HTTPS请求仅在iOS Ionic 2上失败
我有一个Ionic 2
应用程序,该应用程序调用Spring Boot
API来向其他设备发送推送通知。该API已配置为HTTPS。
该API POST
请求适用于 所有内容iOS
。
我在服务器上的SSL证书是自签名的(也许就是这样)。
适用于:
- ionic serve
- Android
- Postman
- curl
这是请求:
public sendNotificationRequest(title: string, action: string, name: string, tokens: any, notifications: boolean) { // Check if user turned off notifications
if(!notifications) {
return;
}
let headers = new Headers({'Content-Type': 'application/json'});
headers.append('Authorization', 'Basic ' + btoa(this.username_decrypted + ':' + this.password_decrypted));
let body = this.formObj(tokens, title, action, name);
console.log(body);
this.http.post("https://<some-url>",
body, { headers: headers }
).subscribe((response) => {
console.log("HTTPS RESPONSE");
console.log(response);
}, function(error) {
console.log("HTTPS ERROR");
console.log(error);
});
}
标头响应如下:
response.setHeader("Access-Control-Allow-Origin", "*");response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
并且收到此错误:
{ "_body":
{"isTrusted":true},
"status":0,"ok":false,
"statusText":"",
"headers":{},
"type":3,
"url":null
}
Spring Boot API:
@CrossOrigin@RequestMapping(value="/notifications", method=RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE, produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<NotificationParent> sendNotifications(@RequestBody NotificationParent objs) {
...
return new ResponseEntity<NotificationParent>(objs, HttpStatus.OK);
}
我假设它是一个iOS安全问题,但我不知道。
回答:
我认为您的假设是正确的-
iOS安全问题。在iOS中,有一种称为“应用程序传输安全性”的应用程序,默认情况下不允许通过HTTP进行连接以及使用自签名证书进行连接。
您必须添加
<key>NSAppTransportSecurity</key><dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
到Info.plist
您项目的,以允许您的自签名流量。
http://blog.ionic.io/preparing-for-ios-9/
https://gist.github.com/mlynch/284699d676fe9ed0abfa
https://developer.apple.com/library/prerelease/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW33
以上是 HTTPS请求仅在iOS Ionic 2上失败 的全部内容, 来源链接: utcz.com/qa/397747.html