phpCAS注销失败,原因是不存在CORS“ Access-Control-Allow-Origin”

时间:2019-06-28 07:17:32

标签: php cas phpcas

我在Angular 4上有一个前端,要连接,我使用的是phpCAS,它放在与前端相同位置的文件夹/ backend中。

在我的前面,即来自Angular的index.html所在的位置,有一个index.php文件,该文件首先启动,并且在调用我的身份验证后包含index.html。

然后身份验证会按预期工作,但是每当我要断开连接时,我都会使用以下命令调用后端:

前面(角度)的断开按钮

logoutCerbere() {
    return this._http.get("./backend/logout.php").subscribe(data => {
       console.log("Disconnected")
   })
}

logout.php

require_once 'init.inc.php';
if (phpCAS::isAuthenticated()) {
    phpCAS::logout();
    session_destroy();
    session_unset();
} else {
    header('HTTP/1.0 401 Unauthorized');
    echo 'HTTP/1.0 401 Unauthorized';    
}

init.inc.php

<?php
require_once 'CAS-1.3.6/CAS.php';
$CAS_HOST = '*******/****';
$CAS_CONTEXT = '/cas/public/';

//$cas_server_ca_cert_path = '/path/to/cachain.pem';
//phpCAS::setCasServerCACert($cas_server_ca_cert_path);

phpCAS::client(CAS_VERSION_2_0, $CAS_HOST, 443, $CAS_CONTEXT);
phpCAS::setNoCasServerValidation();
phpCAS::forceAuthentication();
?>

我收到一条CORS错误消息,提示“同一起源策略不允许在...处读取远程资源(原因:缺少CORS标头'Access-Control-Allow-Origin')”

我不明白的是,我是从服务器上调用此命令的(因为我要获取我的php文件,并且我的连接工作方式完全相同),所以不应有CORS请求

我在那里想念什么?

1 个答案:

答案 0 :(得分:0)

我所缺少的是:

  • 在包含之前确实是PHP,因此没有CORS,但是一旦进入包含,它就是html / front域

  • getUser只是从本地存储中检索数据,因此不涉及CORS

  • 当我要提出断开连接请求时,该请求是在前端发出的,这意味着CORS将会介入。

我通过简单地放置一个<a href="backend/logout.php">Disconnect</a>来解决它。

这意味着PHP在服务器端运行,不会涉及CORS。