简体   繁体   English

Laravel 5.3 API

[英]Laravel 5.3 API

When user enter username and password on the the browser and successfully logged in. 当用户在浏览器上输入用户名和密码并成功登录时。

I like to make some API requests after user have logged in. 我喜欢在用户登录后发出一些API请求。

Laravel 5.3 provide api.php in routes folder. Laravel 5.3在routes文件夹中提供api.php。

in api.php I have included: 在api.php我已经包括:

Route::group(['middleware' => ['auth']], function () {
    Route::get('/test', function (Request $request) {
         return response()->json(['name' => 'test']);
    });
});

When requesting domain.com/api/test on the browser, for some reason it is redirecting to /home ? 在浏览器上请求domain.com/api/test时,出于某种原因它会重定向到/home

API token is not needed. 不需要API令牌。

If you are specifying routes in api.php, you will need to use the auth:api middleware. 如果要在api.php中指定路由,则需要使用auth:api中间件。 So using your example it would be: 所以使用你的例子就是:

Route::group(['middleware' => ['auth:api']], function () {
    Route::get('/test', function (Request $request) {
         return response()->json(['name' => 'test']);
    });
});

Notes about Token auth and Laravel 5.3: 关于Token auth和Laravel 5.3的注意事项:

  • If you've setup laravel's default auth system, you will also need to add a column for api_token to the user table. 如果您已设置laravel的默认身份验证系统,则还需要将api_token列添加到用户表。 If you are using DB seeders, you might want to add something like: $table->char('api_token', 60)->nullable(); 如果您正在使用数据库播种器,您可能需要添加以下内容: $table->char('api_token', 60)->nullable(); to your users table seeder. 给你的用户表播种机。 Alternatively just add the column manually and fill that column with a random 60-char key. 或者,只需手动添加列,并使用随机的60-char键填充该列。
  • When making the request, you can add the api_token as a URL/Querystring parameter like so: domain.com/api/test?api_token=[your 60 char key] . 发出请求时,您可以将api_token添加为URL / api_token参数,如下所示: domain.com/api/test?api_token=[your 60 char key] api_token domain.com/api/test?api_token=[your 60 char key] You can also send the key as a header (if using Postman or similar), ie: Header: Authorization , Value: Bearer [your 60 char key] . 您还可以将密钥作为标题发送(如果使用Postman或类似),即:标题: Authorization ,值: Bearer [your 60 char key]
  • I order to get a useful error if the token is incorrect, and not just be redirected to login, also send the following header with all requests: Header: Accept , Value: application/json . 如果令牌不正确,我命令得到一个有用的错误,而不仅仅是重定向到登录,还发送以下标题包含所有请求:标题: Accept ,值: application/json This allows the expectsJson() check in the unauthenticated() function inside App/Exceptions/Handler.php to work correctly. 这允许在App/Exceptions/Handler.php中的unauthenticated()函数中的expectsJson()检查正常工作。

I found it hard to find clear docs from Laravel about using token auth with 5.3, I think it's because there's a drive to make use of Passport , and it supports tokens in a different way. 我发现很难从Laravel找到关于使用带有5.3的令牌认证的明确文档,我认为这是因为有一个驱动器可以使用Passport ,它以不同的方式支持令牌。 Here's the article that probably helped most getting it working: https://gistlog.co/JacobBennett/090369fbab0b31130b51 这篇文章可能有助于大多数工作: https//gistlog.co/JacobBennett/090369fbab0b31130b51

first install the passport as stated here laravel passport installation 首先安装护照,如此处所述的laravel护照安装

while consuming your own api add below line in your config/app.php in middleware section 在中间件部分的config / app.php中使用自己的api添加以下行

'web' => [
// Other middleware...
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],

now change your route to 现在改变您的路线

Route::group(['middleware' => ['auth:api']], function () {
Route::get('/test', function (Request $request) {
     return response()->json(['name' => 'test']);
});
});

now in your config/auth.php change these lines 现在在你的config / auth.php中更改这些行

 'api' => [
    'driver' => 'passport',
    'provider' => 'users',
],

The reason you are being redirected back to home is because the auth middleware checks if a user session is stored in your browser, but since api middleware does not make use of sessions ( see app\\http\\kernel.php ), your request is considered unauthenticated 您被重定向home的原因是auth中间件检查用户会话是否存储在您的浏览器中,但由于api中间件没有使用会话( 请参阅app \\ http \\ kernel.php ),您的请求被认为是unauthenticated

If you would like to perform simple APIs that utilize sessions, feel free to add them in your web routes, and make sure to secure them by grouping them inside an auth middleware. 如果您想执行使用会话的简单API,请随意在web路由中添加它们,并确保通过将它们分组到auth中间件中来保护它们。

The standard behaviour in Laravel 5.5 is to delegate handling of authentication exceptions to app/Handler::unauthenticated(), in your project's application code. Laravel 5.5中的标准行为是在项目的应用程序代码中将认证异常的处理委托给app / Handler :: unauthenticated()。 You'll find the code in there that redirects to the login page, and you can override it or perform further tests and contextualization in there. 您将在那里找到重定向到登录页面的代码,您可以在其中覆盖它或执行进一步的测试和上下文化。 In previous versions of Laravel, 5.3 among them I believe, this exception handling was executed way down within the Laravel library within the vendor folder. 在Laravel的早期版本中,我相信其中的5.3,这个异常处理是在供应商文件夹中的Laravel库中执行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM