在Restler 3 API上启用CORS的正确方法

在Restler 3 API上启用CORS的正确方法,cors,restler,Cors,Restler,Restler3真是难以置信 我们有一个“公共”API,一个具有基于API_密钥的访问控制的“管理”API,我们希望另一个API受CORS保护(无API_密钥) 新的CORS保护API将用于保护我们所有的javascript ajax调用。我们希望将所有ajax服务器端代码集中到一个具有一致入口和出口点的API中 我们已经设置了以下Restler默认值 Defaults::$crossOriginResourceSharing = true; Defaults::$accessControlAl

Restler3真是难以置信

我们有一个“公共”API,一个具有基于API_密钥的访问控制的“管理”API,我们希望另一个API受CORS保护(无API_密钥)

新的CORS保护API将用于保护我们所有的javascript ajax调用。我们希望将所有ajax服务器端代码集中到一个具有一致入口和出口点的API中

我们已经设置了以下Restler默认值

Defaults::$crossOriginResourceSharing = true;
Defaults::$accessControlAllowOrigin = 'https://www.mydomain.com';
这是正确的方法吗?

我们如何确认安全性工作正常?

作为参考,这里是我们为这个新API创建的index.php

// get the document root from apache and make sure that there is a trailing slash
$document_root = rtrim($_SERVER['DOCUMENT_ROOT'], '/') . '/';

// autoload Restler
// note: this code was provided by Arul to address issues with autoloading Swift and Aws
$loader = require_once $document_root . 'vendor/autoload.php';
$loader->setUseIncludePath(true);
class_alias('Luracast\\Restler\\Restler', 'Restler');

// import namespaces
use Luracast\Restler\Defaults;
use Luracast\Restler\Restler;

// setup versioning
Defaults::$useUrlBasedVersioning = true;

// setup CORS on this API
Defaults::$crossOriginResourceSharing = true;
Defaults::$accessControlAllowOrigin = 'https://www.mydomain.com';

// instantiate restler
$r = new Restler();

// support both Json and Xml formats
$r->setSupportedFormats('JsonFormat', 'XmlFormat');

// api version
$r->setAPIVersion(1);

// create resources.json at API Root for use by API Explorer
$r->addAPIClass('Luracast\\Restler\\Resources');

// autoload the Diagnostics class in the v1 folder
$r->addAPIClass('Diagnostics');

// start
$r->handle();
响应标题

Date: Tue, 15 Oct 2013 17:50:12 GMT
X-Powered-By: PHP/5.3.27
Connection: Keep-Alive
Content-Length: 50
Server: Apache
Content-Type: text/html
Keep-Alive: timeout=5, max=94

是的,你为CORS做得很好

为了测试它,请尝试通过javascript调用api方法。如果它适用于未启用的域,则表明它不工作


类似地,如果它不适用于已启用的域,那么这也是错误的

是的,您对CORS的操作是正确的

为了测试它,请尝试通过javascript调用api方法。如果它适用于未启用的域,则表明它不工作

同样,如果它不适用于已启用的域,那也是错误的