【问题标题】:Proper way to enable CORS on Restler 3 API在 Restler 3 API 上启用 CORS 的正确方法
【发布时间】:2013-10-15 17:53:13
【问题描述】:

Restler3 真是不可思议!

我们有一个“公共”API,一个带有基于 API_KEY 的访问控制的“管理”API,我们想要另一个受 CORS 保护的 API(无 API_KEY)。

新的 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 restler


    【解决方案1】:

    是的,你为 CORS 做的很好

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-13
      • 2014-12-04
      • 1970-01-01
      • 2015-08-31
      • 2013-12-23
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多