【问题标题】:Laravel 5.3 RESTFul API without authentication没有身份验证的 Laravel 5.3 RESTFul API
【发布时间】:2017-02-23 17:27:15
【问题描述】:

我想用 Laravel 5.3 创建一个 API,但我不需要任何类型的身份验证。有可能摆脱它吗?我不想要任何令牌或任何类型的身份验证。

【问题讨论】:

  • 是的,有可能

标签: api laravel authentication restful-architecture


【解决方案1】:

是的,有可能 通常在你的

路由/api.php

你会有类似的东西

Route::middleware('auth:api')->get('/user', function (Request $request) {
   return $request->user();
});

您只需要删除引用身份验证的中间件部分。 所以上面看起来像:

Route::middleware('api')->get('/user', function (Request $request) {
  return $request->user();
  //middleware('api') URI prefix. which would become '/api/user'
});

Route::apiResource('user', 'UserController');
//same as above but includes crud methods excluding 'create and edit'

【讨论】:

    【解决方案2】:

    为了帮助在我的情况下到达这里的任何人:请注意 api.php 中的任何路由都以“api/”为前缀。 它设置在/app/Providers/RouteServiceProvider.php

    所以:

    Route::get('/delegates', "APIController@delegate");

    可以从

    访问

    http://www.yourdomain.com/api/delegates

    对不起,如果有点离题,但希望它可以帮助某人。

    【讨论】:

      【解决方案3】:

      当然你可以摆脱它。只需将您的路线设置为不使用任何中间件。

      routes/api.php 文件上创建您的 API 路由,然后修改 app/Http/Kernel.php 文件以正确设置您的中间件:

      删除(或添加)api 中间件组中不需要的中间件。

      默认情况下,L5.3 在api 组上带有两个中间件:

      'api' => [
                  'throttle:60,1',
                  'bindings',
              ],
      

      第一个为您的 API 提供速率限制(60 个请求/分钟), 第二个替换您的模型绑定。

      【讨论】:

        【解决方案4】:

        允许您的路线在没有身份验证的情况下运行

        Http\Middleware\VerifyCsrfToken
        public function handle($request, Closure $next)
        {
          if (!$request->is('api/*'))
          {
            return parent::handle($request, $next);
          }
        
          return $next($request);
        }
        

        这样设置路线

        'api' => 'APIController'
        

        这是 APIController ('/api/data') 中的方法

        public function getData(Request $request)
        {
          return "Hello";
        }
        

        【讨论】:

        • VerifyCsrfToken 中间件不是对用户进行身份验证的人。您的回答不能解决身份验证问题。
        【解决方案5】:

        有可能,只需创建到您的控制器的路由并返回数据(无需任何身份验证中间件)。

        【讨论】:

          猜你喜欢
          • 2018-06-30
          • 2011-11-28
          • 1970-01-01
          • 2017-05-25
          • 2017-05-13
          • 2018-06-10
          • 1970-01-01
          • 2013-08-21
          • 2017-06-18
          相关资源
          最近更新 更多