我有一个Ionic 2应用程序,该应用程序调用Spring BootAPI来向其他设备发送推送通知。该API已配置为HTTPS。
Ionic 2
Spring Boot
该API POST请求适用于 除以外的 所有内容iOS。
POST
iOS
我在服务器上的SSL证书是自签名的(也许就是这样)。
适用于:
这是请求:
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您项目的,以允许您的自签名流量。
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