在iOS 15.5/Xcode上使用授权头时出现预检错误- Cordova-ios@6.1.1

osh3o9ms  于 2022-11-15  发布在  iOS
关注(0)|答案(1)|浏览(187)

我在iOS 15.5上的cordova项目遇到了问题。现在我正在尝试验证一个启用了标准身份验证的用户在IIS上的登录。

<?php

// Prüfen, ob Benutzeranmeldung über IIS erfolgt ist
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header("HTTP/1.1 401 Unauthorized");
    exit;
}

// OK zurückgeben
header("Content-Type: application/json");
echo '{"login":"ok"}';
?>

下面是来自iPhone的javascript请求:

function benutzeranmeldungPruefen() {

    var loginString = eingabeBenutzername + ":" + eingabeKennwort;
    var loginStringBase64 = "Basic " + btoa(loginString); // String für Header in Base64 codieren

    
    // Login-Header definieren
    $.ajaxSetup({
        headers: { "Authorization": loginStringBase64 },
            timeout: 10000
    });
    
    var servername = localStorage.getItem("Server");
    var port = localStorage.getItem("Port");
    var https = localStorage.getItem("Https");
 
    // HTTP-GET-Request erstellen
    var get = $.get( https+"://"+servername+":"+port+"/logincheck.php");

    get.done(function( data ) {
        
        // Prüfen, ob Anmeldung erfolgreich war
        if (data["login"] == "ok") {
            
            localStorage.setItem("Benutzer", eingabeBenutzername);
            localStorage.setItem("Kennwort", eingabeKennwort);
            
        }
        else {
            // Grafische Anpassungen und Protokoll
            document.getElementById("statusLogin").innerHTML += "<br />Authentifizierung fehlgeschlagen.";
            document.getElementById("buttonLogin").setAttribute("style", "display:block;");
            
        }
        
        // Grafische Anpassungen
        document.getElementById("loadingLogin").setAttribute("style", "visibility:hidden;");
        //document.getElementById("loadingLogin").setAttribute("style", "display:none;");
        
        
    });

    get.fail(function() {
        // Grafische Anpassungen und Protokoll
        document.getElementById("buttonLogin").setAttribute("style", "display:block;");
        document.getElementById("loadingLogin").setAttribute("style", "visibility:hidden;");
        $("#seiteErsteinrichtung").removeClass("loading");
        alert( "Fehler beim Überprüfen der Zugangsdaten!" );
    })
    
}

如果我构建应用程序并在我的iPhone上运行它,我会在使用Safari进行调试时遇到此错误:

[Error] Preflight response is not successful. Status code: 401
[Error] XMLHttpRequest cannot load https://192.168.178.10/logincheck.php due to access control checks.
[Error] Failed to load resource: Preflight response is not successful. Status code: 401 (logincheck.php, line 0)

然后寻找标题,这就是我得到的:

overview
URL: http://192.168.178.10/logincheck.php
Status: —
Quelle: —
Initiator:
jquery-2.2.2.min.js:4:16032

Request
Accept: */*
Authorization: BasicBasic VGVzdDoxMjM0NTY=
Origin: null
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148

Answer
no Response-Header

我已经设置了所有的标题,我可以找到在线在我的网页配置文件可悲的是没有成功:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.web>
        <customErrors mode="Off" />
    </system.web>
    <system.webServer>
        <handlers>
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <!--<remove name="OPTIONSVerbHandler" /> !-->
            <remove name="TRACEVerbHandler" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*" />
                <add name="Access-Control-Allow-Credentials" value="true"/>
                <add name="Access-Control-Max-Age" value="5000"/>
                <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,OPTIONS" />
                <add name="Access-Control-Allow-Headers" value="Origin, Authorization, Content-Type, Content-Range, Content-Disposition, Content-Description, X-Requested-With, X-ACCESS_TOKEN" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</configuration>

如果我从请求中删除Authorization头,我至少会从服务器得到一个答案:

Overview
URL: https://apptest.itunds.de:17635/logincheck.php
Status: 401 Unauthorized
Quelle: Netzwerk
Adresse: 192.168.252.253:17635
Initiator:
jquery-2.2.2.min.js:4:16032

Request
POST /logincheck.php HTTP/1.1
Accept: */*
Origin: null
Accept-Encoding: gzip, deflate, br
Host: apptest.itunds.de:17635
User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148
Accept-Language: de-DE,de;q=0.9
Content-Length: 0
Connection: keep-alive

Answer
HTTP/1.1 401 Unauthorized
Access-Control-Allow-Credentials: true
Content-Type: text/html
Access-Control-Max-Age: 5000
Access-Control-Allow-Methods: GET,POST,DELETE,OPTIONS
Date: Fri, 27 May 2022 07:42:08 GMT
Access-Control-Allow-Headers: Origin, Authorization, Content-Type, Content-Range, Content-Disposition, Content-Description, X-Requested-With, X-ACCESS_TOKEN
Content-Length: 1344
Access-Control-Allow-Origin: *
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Www-Authenticate: Basic realm="192.168.178.10"

我不知道是什么导致这个问题或如何解决它。我试图解决这个问题没有近3天,我找不到任何东西。我真的需要帮助。

7jmck4yq

7jmck4yq1#

因为WKWebView上没有原点,所以预检会失败。您是否在config.xml中设置了原点和方案?

<preference name="scheme" value="app" />
 <preference name="hostname" value="localhost" />

您也可以尝试https://github.com/globules-io/cordova-plugin-ios-xhr

相关问题