api – HTTPS请求仅在iOS,Ionic 2上失败

我有一个Ionic 2应用程序,它调用 Spring Boot API将推送通知发送到其他设备. API使用HTTPS配置.

API POST请求适用于除iOS之外的所有内容.

我在服务器上的SSL证书是自签名的(可能就是这样吗?).

适用于:

>离子服务
> Android
>邮差
>卷曲

这是请求:

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中,有一种叫做App Transport Security的东西,默认情况下不允许通过HTTP连接和使用自签名证书的连接.

你必须添加

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

到项目的Info.plist以允许自签名流量.

有关详细信息,请参阅this answer以及以下链接.

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

以上是来客网为你收集整理的api – HTTPS请求仅在iOS,Ionic 2上失败全部内容,希望文章能够帮你解决api – HTTPS请求仅在iOS,Ionic 2上失败所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。