2017-03-05 55 views
4

我有一個Ionic 2應用程序調用Spring Boot API將推送通知發送到其他設備。該API使用HTTPS進行配置。HTTPS請求僅在iOS上失敗,Ionic 2

該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 
} 

春季啓動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的安全性問題,但我不知道。

+0

試試這個 - http://uncaughterror.com/programming/ionic3/preflight- response-issue-with-ionic3-app-on-ios-build-only-resolved/ –

回答

3

我認爲你的假設是正確的 - 一個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

+0

@theblindprophet你能分享你在iOS應用的控制檯中收到的錯誤消息嗎? – Daniel

+0

對不起,我的意思是應用程序嘗試請求時物理/模擬器iOS設備的控制檯輸出。 – Daniel

+0

你知道,我確實知道了。我不得不改變我的網址,以完全符合我簽署的證書。但是,我意識到我不應該需要簽名證書。謝謝您的幫助。 – theblindprophet

0

因爲Java API會有這麼多問題,包括問題的難度,我創建了一個非常簡單的應用程序Node Express。非常容易。

"use strict"; 
// BASE SETUP 

// call the packages we need 
var express = require('express');  // call express 
var fs   = require('fs'); 
var https  = require('https'); 
var app  = express();     // define our app using express 
var bodyParser = require('body-parser'); 
var basicAuth = require('express-basic-auth'); 
var cors  = require('cors'); 

// configure app to use bodyParser() 
// this will let us get the data from a POST 
app.use(bodyParser.urlencoded({ extended: true })); 
app.use(bodyParser.json()); 

// ROUTES FOR OUR API 
var router = express.Router();    
// get an instance of the express Router 

// all of our routes will be prefixed with /api 
var auth = basicAuth({ 
    users: { 'xxx': 'xxx' }, 
    unauthorizedResponse: getUnauthorizedResponse 
}); 

app.use(cors()) 
app.options('*', cors()); // include before other routes 

// middleware to use for all requests 
router.use(function(req, res, next) { 
    // do logging 
    console.log('Notification being processed.'); 
    res.header("Access-Control-Allow-Origin", "*"); 
    res.header("Access-Control-Allow-Headers", "X-Requested-With"); 
    res.header('Access-Control-Allow-Headers', 'Content-Type'); 
    next(); // make sure we go to the next routes and don't stop here 
}); 

app.use('/xxx/api', auth, router); 

// on routes that end in /notifications 
// ---------------------------------------------------- 
router.route('/notifications') 
// post notifications, :8443/api/notifications 
.post(function(req, res) { 
    ... 
    res.end(); 
}); 

// START THE SERVER 
https.createServer({ 
    key: fs.readFileSync('xxx.key'), 
    cert: fs.readFileSync('xxx.crt'), 
    passphrase: 'xxx' 
}, app).listen(8443); 
console.log('Magic happens on port 8443'); 

function getUnauthorizedResponse(req) { 
    var message = req.auth ? 
     ('Credentials ' + req.auth.user + ':' + req.auth.password + ' rejected') : 
    'No credentials provided'; 
    return {error: message}; 
} 
4

我有同樣的問題了。刪除WkWebView解決了這個問題。

ionic cordova plugin remove cordova-plugin-wkwebview-engine 

更多信息:

https://forum.ionicframework.com/t/ios10-http-requests-blocked/67663/2?u=profitsventure

後,這個插件刪除我可以通過iOS的

使HTTP請求
+0

這對你有幫助嗎?是否與問題中提到的問題相同? – zubair1024

+0

@ zubair1024試試這個http://uncaughterror.com/programming/ionic3/preflight-response-issue-with-ionic3-app-on-ios-build-only-resolved/ –

+0

同樣,我不得不運行'ionic cordova plugin rm cordova-plugin-ionic-webview '讓我們的應用程序再次與最新的Ionic CLI一起工作。甜蜜的聖誕離子的發展令人沮喪。 – Luke