laravel 5.4 JWT 多用户认证

  1. 在 composer.json 文件的 require 中添加如下行来引入 JWT-Auth
1
2
3
4
5
"require": {
...
"tymon/jwt-auth":"1.0.0-beta.3"
...
}

执行 composer update

  1. comfig/app.php 配置

providers 下添加

1
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,

aliases 下添加

1
2
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
  1. 生成 jwt.php 文件

在 cmd 窗口执行命令

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

会在 config 文件夹下生成 jwt.php 配置文件

接着生成 jwt 密钥

1
php artisan jwt:secret
  1. config/auth.php 设置相关参数

我这里需要添加一个微信用户表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
'guards' => [
...
'api' => [
'driver' => 'jwt',
'provider' => 'we_users',
],
],
'providers' => [
...
'we_users' => [
'driver' => 'eloquent',
'model' => App\WeUser::class,
],
],
  1. 创建 WeUser 模型
1
php artisan make:model WeUser -m

编辑 WeUser.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
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Tymon\JWTAuth\Contracts\JWTSubject as AuthenticatableUserContract;
class WeUser extends Authenticatable implements AuthenticatableUserContract
{
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',
];
/**
* @return mixed
*/
public function getJWTIdentifier()
{
return $this->getKey(); // Eloquent model method
}
/**
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}
  1. route.php 中添加登录路由
1
Route::post('/login','[email protected]');
  1. 创建 WeUser 控制器
1
php artisan make:controller WeUserController
  1. 编辑微信用户控制器 WeUserController.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
/**
* 微信用户自动登陆
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function login(Request $request)
{
$hasUser = WeUser::Where('email', $request->email)->first();
if(!$hasUser){
WeUser::create([
'name' => $request->name,
'email' => $request->email,
'password' => bcrypt($request->password),
]);
}
$credentials=[
'email' => $request->email,
'password' => $request->password,
];
try {
if (! $token = Auth::guard($this->guard)->attempt($credentials)) {
return response()->json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
return response()->json(['error' => 'could_not_create_token'], 500);
}
return response()->json(compact('token'));
}