【问题标题】:Protected API for October CMS website status, authenticated users, etc用于十月 CMS 网站状态、经过身份验证的用户等的受保护 API
【发布时间】:2016-12-30 18:50:55
【问题描述】:

我不相信这方面存在任何东西,但如果我错了,请纠正我。我想为 10 月 CMS 创建一个 API,其端点可以获取对网站状态(已在 10 月仪表板中)、当前经过身份验证的用户、日志提要和其他几种可能性等内容的响应。这个想法是有一个外部仪表板来提取这些数据。

问题是它应该受到保护。我为 vanilla Laravel 找到了 this 添加 API 密钥。有没有人尝试在 10 月份创建受保护的 API 端点?

如果有人构建了某些东西,我实际上是在尝试找到一个起点,并且我希望将其变成一个开源项目,以通过某种插件帮助扩展 10 月。

【问题讨论】:

  • 仍在环顾四周,但我也将在GitHub repo 内继续努力。如果您需要帮助,请告诉我,我会不断地为其他人使用奠定基础。
  • 实际上我想构建类似的东西。如果您在创建此插件时需要任何帮助,我想与您一起构建它。
  • 太棒了!我有一个项目要去here。我正在慢慢构建它,因此我还没有太多时间投入其中。

标签: php laravel octobercms


【解决方案1】:

您可以使用中间件自己进行身份验证。其实很简单。

您需要将中间件类应用于相关路由:

Route::group(['prefix' => 'api/v1', 'middleware' => 'Author\Plugin\classes\ApiMiddleware'], function () {
    Route::get('info', ['uses' => 'Author\Plugin\Controllers\Api\v1\Info@index']);
});

在 ApiMiddleware 的 handle() 函数中,您可以根据需要定义身份验证规则(IP 限制、基本身份验证等)

public function handle($request, Closure $next)
{
    // check if the IP is in the whitelist
    if (!in_array($request->ip(), $this->whitelist)) {
        return response("IP address rejected\n" . $request->ip(), 403);
    }

    // check for errors thrown in the other controllers
    $response = $next($request);
    $errors = $request->getSession()->get('errors');

    // no errors - return the response
    if (empty($errors))
        return $response;

    // return the error
    $message = $errors->getBag('default')->first();
    return response()->json(['code' => '400', 'message' => $message]);
}

【讨论】:

    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多