2017-03-05 3 views
4

Ich habe eine Ionic 2 Anwendung, die eine Spring Boot API aufruft, um Push-Benachrichtigungen an andere Geräte zu senden. Die API ist mit HTTPS konfiguriert.HTTPS-Anfrage schlägt nur auf iOS, Ionic 2

Die API POST Anfrage funktioniert auf alles außeriOS.

Mein SSL-Zertifikat auf dem Server ist selbstsigniert (vielleicht ist das das?).

Werke auf:

  • ionische dienen
  • Android
  • Postman
  • curl

Hier ist die Anfrage:

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); 
    }); 
} 

Die Header-Reaktionen sind wie folgt:

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"); 

Und dieser Fehler empfangen wird:

{ 
"_body": 
    {"isTrusted":true}, 
    "status":0,"ok":false, 
    "statusText":"", 
    "headers":{}, 
    "type":3, 
    "url":null 
} 

Frühling 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); 
} 

ich seine iOS Sicherheitslücke gehe davon aus, aber Ich habe keine Ahnung.

+0

Versuchen Sie dies machen - http://uncaughterror.com/programming/ionic3/preflight- Antwort-Problem-mit-ionic3-app-on-ios-build-only-behoben/ –

Antwort

3

Ich denke, Ihre Annahme ist korrekt - ein iOS Sicherheitsproblem. In iOS gibt es etwas namens App-Transport-Sicherheit, das standardmäßig Verbindungen über HTTP und Verbindungen mit selbstsignierten Zertifikaten verbietet.

Sie haben

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

zum Info.plist Ihres Projekt hinzufügen, um Ihren selbst signierten Datenverkehr zu ermöglichen.

Siehe this answer sowie die untenstehenden Links für weitere Informationen.

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 können Sie die Fehlermeldung teilen, die Sie in der Konsole der iOS App erhalten? – Daniel

+0

Entschuldigung, ich meinte die Konsolenausgabe des physischen/Simulator-iOS-Geräts, wenn die App die Anfrage versucht. – Daniel

+0

Wissen Sie, ich habe es wirklich verstanden. Ich musste meine URL ändern, um genau zu meinem unterzeichneten Zertifikat zu passen. Mir ist jedoch klar, dass ich das signierte Zertifikat sowieso nicht benötigt hätte. Danke für die Hilfe. – theblindprophet

0

Da die Java API so viele Probleme verursacht, einschließlich der Schwierigkeit der Frage, habe ich eine sehr einfache Node Express App. So viel einfacher.

"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

Ich hatte ein gleiches Problem auch. Das Entfernen des WkWebView löst das Problem.

ionic cordova plugin remove cordova-plugin-wkwebview-engine 

Weitere Informationen:

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

Nachdem dieses Plugin entfernt Ich kann HTTP-Anfrage über iOS

+0

Wie hat Ihnen das geholfen? War es das gleiche Problem wie in der Frage erwähnt? – zubair1024

+0

@ zubair1024 versuchen Sie dies http://uncaughterror.com/programming/ionic3/preflight-response-issue-with-ionic3-app-on-ios-build-only-resolved/ –

+0

Ebenso musste ich 'ionic cordova plugin ausführen rm cordova-plugin-ionic-webview ', um unsere App wieder mit dem neuesten Ionic CLI zu arbeiten. Sweet Christmas Ionische Entwicklung ist frustrierend. – Luke