当前位置:编程学习 > > 正文

laravel 快速开发api技巧(Laravel5.4简单实现app接口Api Token认证方法)

时间:2021-10-30 10:20:19类别:编程学习

laravel 快速开发api技巧

Laravel5.4简单实现app接口Api Token认证方法

我是小白,今天写这篇文章主要是给新手学习看的,大佬就不用看了,有很多不足望大家指出,共同进步。

在开发中许多 api 通常在返回响应之前都需要某种形式的认证,有些时候,一个认证的请求和一个未认证的请求,响应可能不同。

在web项目中,实现认证比较轻松,那么前后端分离的项目中,我们要怎么实现认证,今天这篇文章就以 api token 认证机制,使用token可以解决laravel api的无状态认证。

一、给用户表users增加api_token字段

  • ?
  • 1
  • php artisan make:migration add_api_token_to_users
  • 首先,给用户表中增加 api_token字段,在生成的迁移文件中添加字段:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • <?php
  •  
  • use illuminate\support\facades\schema;
  • use illuminate\database\schema\blueprint;
  • use illuminate\database\migrations\migration;
  •  
  • class addapitokentousers extends migration
  • {
  •  /**
  •   * run the migrations.
  •   *
  •   * @return void
  •   */
  •  public function up()
  •  {
  •   schema::table('users', function (blueprint $table) {
  •    $table->string('api_token', 64)->unique();
  •   });
  •  }
  •  
  •  /**
  •   * reverse the migrations.
  •   *
  •   * @return void
  •   */
  •  public function down()
  •  {
  •   schema::table('users', function (blueprint $table) {
  •    $table->dropcolumn(['api_token']); //新增加的
  •   });
  •  }
  • }
  • 二、然后使用下面的命令将字段添加到表中:

  • ?
  • 1
  • php artisan migrate
  • 三、用户注册:

    在注册的控制器文件的创建用户中添加 api_token 字段:

    我这里的控制器是app\http\controllers\api\registercontroller.php

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • protected function register(request $request)
  •  {
  •   $input = $request->all(); //获取传过来的传数
  •  
  •  //在这里设置生成token后,与账号密码等信息一起存进user表
  •  
  •   $user = user::create($data); //存进数据库
  •  return $token;
  •  //这里面的逻辑自己写 我这里只是简单实现
  • }
  • 最后,不要忘记在 app\user.php用户模型表中的 $fillable 属性当中添加api_token字段:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • /**
  •   * the attributes that are mass assignable.
  •   *
  •   * @var array
  •   */
  •  protected $fillable = [
  •   'name', 'email', 'password','confirmation_token','api_token'
  •  ];
  • 四、修改api driver:

    接下来要在config\auth.php 修改如下内容:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 'guards' => [
  •   'web' => [
  •    'driver' => 'session',
  •    'provider' => 'users',
  •   ],
  •  
  •   'api' => [
  •    'driver' => 'token', //把driver设置为token
  •    'provider' => 'users',
  •   ],
  •  ],
  • 五、如何使用:

    接下来,我们要添加路由,在routes\api.php文件修改:

  • ?
  • 1
  • 2
  • 3
  • route::group(['middleware' => 'token'], function(){
  •  route::post('register', 'api\usercontroller@register');
  • });
  • 怎么访问?我们这里用postman来测试:

    laravel 快速开发api技巧(Laravel5.4简单实现app接口Api Token认证方法)

    到些就大功告成了! 注意,这个只是基础认证,现在开发还是用别人已经开发好的插件好,比如oauth2,basic,jwt,passport等等。

    哦对了,如果想看token的认证原理,我们可以看他的底层源码

    vendor\laravel\framework\src\illuminate\auth\tokenguard.php:

    laravel 快速开发api技巧(Laravel5.4简单实现app接口Api Token认证方法)

    这个我也看不明白,哈!再见!

    以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。

    原文链接:https://blog.csdn.net/qq_20455399/article/details/79260787

    上一篇下一篇

    猜您喜欢

    热门推荐