laravel5.4+dingoapi+jwt构建restfulapi

添加jwt的认证

在composer.json的reqiure添加如下:

1
"tymon/jwt-auth": "1.0.*@dev",

运行composer update将jwt装上去

proc_open(): fork failed - Cannot allocate memory

1
2
3
4
5
free -m
/bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024
/sbin/mkswap /var/swap.1
/sbin/swapon /var/swap.1
composer update

在config/api.php添加内容

1
2
3
'auth' => [
'jwt' => Dingo\Api\Auth\Provider\JWT::class
]

在config/app.php

1
2
3
4
5
6
7
8
9
'providers' => [
// 前面很多
Tymon\JWTAuth\Providers\LaravelServiceProvider::class
],

'aliases' => [
// 前面很多
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class
]

在终端运行:

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

会生成config/jwt.php 这是jwt的配置文件
生成jwt的key到.env文件运行:

1
php artisan jwt:secret

RegisterApiController

修改User.php

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
use Notifiable;

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

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];

/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey();
}

/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}

添加 /app/Http/Controllers/Api/V1/Auth/RegisterApiController

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/**
* Created by PhpStorm.
* User: clyhs
* Date: 2017/10/1
* Time: 18:11
*/
namespace App\Http\Controllers\Api\V1\Auth;

use App\User;
use App\Http\Controllers\Api\V1\BaseController;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Dingo\Api\Exception\StoreResourceFailedException;
use Tymon\JWTAuth\Facades\JWTAuth;


class RegisterApiController extends BaseController{

use RegistersUsers;

public function register(Request $request)
{
$valid=$this->valid($request->all()); //验证表单
if($valid->fails()){
$this->sendFailResponse($valid->errors());
}
else{
$user=User::create([
'name'=>$request->name,
'email'=>$request->email,
'password'=>bcrypt($request->password)
]);
if($user){
$token=JWTAuth::fromuser($user); //获取token
return $this->response->array([
"token" => $token,
"message" => "Registration Success",
"status_code" => 201
]);
}
else{
$this->sendFailResponse("Register Error");
}
}
}
public function valid($data)
{
return Validator::make($data,[
'name'=>'required|unique:users|max:10',
'email'=>'required|unique:users|email',
'password'=>'required|min:6']);
}
public function sendFailResponse($message)
{
return $this->response->error($message,400);
}

}

routes/api.php中添加路由

1
2
3
4
5
6
7
8
9
10
$api->version('v1', ['namespace' => 'App\Http\Controllers\Api\V1'], function ($api) {
$api->get('user/{id}', 'UserController@show');
$api->get('user', 'UserController@index');
$api->get('user2/{id}', 'UserApiController@getUserInfo');
$api->get('user2/show/{id}', 'UserApiController@show');
$api->get('user2', 'UserApiController@index');
$api->get('user2forpage', 'UserApiController@page');

$api->post('register', 'Auth\RegisterApiController@register');
});

POSTMAN测试
img

LoginApiController

添加 /app/Http/Controllers/Api/V1/Auth/LoginApiController

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
33
34
35
36
37
38
39
40
41
42
<?php
/**
* Created by PhpStorm.
* User: clyhs
* Date: 2017/10/1
* Time: 18:40
*/
namespace App\Http\Controllers\Api\V1\Auth;

use App\User;
use App\Http\Controllers\Api\V1\BaseController;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Facades\JWTAuth;
use Illuminate\Support\Facades\Hash;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginApiController extends BaseController{

use AuthenticatesUsers;

public function login(Request $request)
{
$user=User::where('name',$request->email)->orwhere('email',$request->email)->firstOrFail();
if($user && Hash::check($request->password,$user->password)){
$token=JWTAuth::fromuser($user); //获取token
$this->clearLoginAttempts($request); //清除登录次数
return $this->response->array([
'token'=>$token,
'message'=>"Login Success",
'status_code'=>200
]);
}
else{
throw new UnauthorizedHttpException("Login Failed");
}
}
public function logout(){
JWTAuth::invalidate(JWTAuth::getToken()); //token加入黑名单(注销)
$this->guard()->logout();
}
}

routes/api.php中添加路由

1
$api->post('login', 'Auth\LoginApiController@login');

POSTMAN测试
img

分享