您的位置:首页 > 编程语言 > Go语言

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

2017-05-26 18:46 771 查看
 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环境下执行:

[plain] view
plain copy

composer require tymon/jwt-auth  

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

2、在"bootstrap/app.PHP"
文件中,找到Register Se
1fa48
rvice Providers一节,添加:

[plain] view
plain copy

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

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

内容如下:

[php] view
plain copy

<?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文件。添加:

[javascript] view
plain copy

"autoload": {  

    ...  

    "files": [  

      "app/helpers.php"  

    ]  

  },  

执行:

[plain] view
plain copy

composer dump-autoload  

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

最简单的办法是从/vendor/tymon/jwt-auth/src/config/config.php 复制一份到config/jwt.php。
因为lumen没有vendor:publish命令,所以,可以装basicit/lumen-vendor-publish来解决。

[plain] view
plain copy

composer require basicit/lumen-vendor-publish  

然后执行:

[plain] view
plain copy

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。

[plain] view
plain copy

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"的注释,修改成下面这样:

[php] view
plain copy

$app->routeMiddleware([  

    'jwt.auth'    => Tymon\JWTAuth\Middleware\GetUserFromToken::class,  

    'jwt.refresh' => Tymon\JWTAuth\Middleware\RefreshToken::class,  

]);  

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

[php] view
plain copy

<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] view
plain copy

<?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.

[javascript] view
plain copy

{  

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

}  

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

也可以另外指定。具体请看jwt.php中"User Model namespace" 的设置。
也就是,创建"User" model,生成数据库表users。再插入几条用户记录,到此项目完成。

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

[php] view
plain copy

<?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有问题。

[plain] view
plain copy

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。这个工具非常非常棒。

--完--
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: