Lumen上使用Dingo/Api做API开发时用JWT-Auth做认证的实现

 Lumen轻量级的框架,用来做API很合适,起码本人这么想。

做API,可以配合Dingo/Api来搞。

Dingo/Api可以使用的用户认证方式有:

  • HTTP Basic (Dingo\Api\Auth\Provider\Basic)
  • JSON Web Tokens (Dingo\Api\Auth\Provider\JWT)
  • OAuth 2.0 (Dingo\Api\Auth\Provider\OAuth2)
这里选了JWT来实现。

需要用的工具有:

  • Lumen
  • Dingo/Api
  • Tymon/JWT-Auth

API可以简单的分三个部分:

  • 登录
  • 验证
  • 具体的API行为

所以,先从登录开始。


这里的API服务基于Lumen实现。Laravel的实现还没搞过,以后再说。


一、Tymon/JWT-Auth安装


1、Lumen环境下执行:

composer require tymon/jwt-auth
然后需要添加“JWTAuthServiceProvider”到 Provider List。在Laravel环境中配置放在config/app.php中,但在Lumen环境有些不同。


2、在"bootstrap/app.php" 文件中,找到Register Service Providers一节,添加:

$app->register('Tymon\JWTAuth\Providers\JWTAuthServiceProvider');

3 、在“app”目录下创建“helpers.php”文件。

内容如下:

<?php

if ( ! function_exists('config_path'))  
{
    /**
     * Get the configuration path.
     *
     * @param  string $path
     * @return string
     */
    function config_path($path = '')
    {
        return app()->basePath() . '/config' . ($path ? '/' . $path : $path);
    }
}

修改composer.json文件。添加:

"autoload": {
    ...
    "files": [
      "app/helpers.php"
    ]
  },

执行:

composer dump-autoload

4、生成jwt-auth的配置文件

最简单的办法是从/vendor/tymon/jwt-auth/src/config/config.php 复制一份到config/jwt.php。

因为lumen没有vendor:publish命令,所以,可以装basicit/lumen-vendor-publish来解决。

composer require basicit/lumen-vendor-publish

然后执行:

php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\JWTAuthServiceProvider" 

会以config目录下生成jwt.php文件。这就是jwt-auth的配置文件。

执行“php artisan jwt:generate ”生成secret。


5、Adding facades

a)bootstrap/app.php文件中去掉"$app->withFacades();"前的注释。

b)在这行下面添加“$app->configure('jwt');”,不然调用不到jwt的配置文件。

c)紧接着是facades。

class_alias('Tymon\JWTAuth\Facades\JWTAuth', 'JWTAuth');  
/** This gives you finer control over the payloads you create if you require it.
 *  Source: https://github.com/tymondesigns/jwt-auth/wiki/Installation
 */
class_alias('Tymon\JWTAuth\Facades\JWTFactory', 'JWTFactory'); // Optional  

6、jwt的配置文件里保持默认就可以。想知道具体含义可以参考它的文档。secret是必须设的。前面已经设过了。

7、在bootstrap/app.php文件中,查找Register Middleware 小节。去掉"routeMiddleware"的注释,修改成下面这样:

$app->routeMiddleware([
    'jwt.auth'    => Tymon\JWTAuth\Middleware\GetUserFromToken::class,
    'jwt.refresh' => Tymon\JWTAuth\Middleware\RefreshToken::class,
]);


8、此时,可以使用路由中间件了。下面举个例子。

<pre name="code" class="php">// store, update, destory这些需要权限的操作全部需要经过认证。
$app->group(['prefix' => 'projects', 'middleware' => 'jwt.auth'], function($app) { $app->post('/', 'App\Http\Controllers\ProjectsController@store'); $app->put('/{projectId}', 'App\Http\Controllers\ProjectsController@update'); $app->delete('/{projectId}', 'App\Http\Controllers\ProjectsController@destroy');}); // index, show这些则不需要$app->group(['prefix' => 'projects'], function ($app){ $app->get('/', 'App\Http\Controllers\ProjectsController@index'); $app->get('/{projectId}', 'App\Http\Controllers\ProjectsController@show');});

 

9、登录。下面要做的就是写一个登录的程序,因为lumen没有,要自己写。

建一个路由“$app->post('auth/login', 'App\Http\Controllers\Auth\AuthController@postLogin');”

在“app/Http/Controllers/Auth/AuthController.php”中创建我们的登录代码。

示例如下:

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Exception\HttpResponseException;  
use JWTAuth;  
use Tymon\JWTAuth\Exceptions\JWTException;  
use App\Http\Controllers\Controller;  
use Illuminate\Http\Request;  
use Illuminate\Http\Response as IlluminateResponse;

class AuthController extends Controller {

    /**
     * Handle a login request to the application.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function postLogin(Request $request)
    {
        try
        {
            $this->validate($request, [
                'email' => 'required|email|max:255', 'password' => 'required',
            ]);
        }
        catch (HttpResponseException $e)
        {
            return response()->json([
                'error' => [
                    'message'     => 'Invalid auth',
                    'status_code' => IlluminateResponse::HTTP_BAD_REQUEST
                ]],
                IlluminateResponse::HTTP_BAD_REQUEST,
                $headers = []
            );
        }

        $credentials = $this->getCredentials($request);

        try
        {
            // attempt to verify the credentials and create a token for the user
            if ( ! $token = JWTAuth::attempt($credentials))
            {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        }
        catch (JWTException $e)
        {
            // something went wrong whilst attempting to encode the token
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
        return response()->json(compact('token'));
    }

    /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function getCredentials(Request $request)
    {
        return $request->only('email', 'password');
    }
}
此时,用post方式提交一个登录请求会得到json格式的返回值,里面就是登录后获得的token.

{
  "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImlzcyI6Imh0dHA6XC9cLzEyNy4wLjAuMTo4NFwvYXV0aFwvbG9naW4iLCJpYXQiOiIxNDQ2OTE4MjM3IiwiZXhwIjoiMTQ0NjkyMTgzNyIsIm5iZiI6IjE0NDY5MTgyMzciLCJqdGkiOiI1ZGY1Njk5OGYxMTc3MzlhMjQ4ZjgzNzUyZmQ2MTA1MiJ9.1FP8yXwlw_KHrx9NAFqQWPaq2c2LLq_vyuJgwI_EX9k"
}

10、还要提一下,jwt-auth默认使用Users表做为登录认证的表,这个表跟laravel是一样的。所以可以直接从laravel复制过来。

也可以另外指定。具体请看jwt.php中"User Model namespace" 的设置。

也就是,创建"User" model,生成数据库表users。再插入几条用户记录,到此项目完成。

但是!!!这里有个问题,User Model有些内容必须要有。

如下:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    //use EntrustUserTrait; // add this trait to your user model

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];
}
请跟自己的User Model对比一下。

出现如下错误一般都是User Model有问题。

Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\User given, called in C:\xampp\htdocs\APIs\vendor\illuminate\auth\Guard.php on line 390 


另外,有条件的可以装个Postman。这个工具非常非常棒。



--完--




  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值