Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Aug 11, 2018
1 parent 8f9e841 commit 7862d4f
Show file tree
Hide file tree
Showing 15 changed files with 704 additions and 64 deletions.
530 changes: 523 additions & 7 deletions _ide_helper.php

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Exceptions;

use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
Expand Down Expand Up @@ -46,6 +47,49 @@ public function report(Exception $exception)
*/
public function render($request, Exception $exception)
{
//捕获laravel-permission异常 然后跳转到主页
// if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
// return redirect('/');
// }

if ($exception instanceof \Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException) {

//判断token是否存在
if (! Auth::guard('api')->parser()->setRequest($request)->hasToken()) {
return response()->json([
'code' => 4002,
'message' => 'Token not provided',
]);
}

//判断token是否正常
try {
if (! Auth::guard('api')->parseToken()->authenticate()) {
return response()->json([
'code' => 4003,
'message' => 'jwt-auth: Member not found',
]);
}
} catch (Exception $e) {
return response()->json([
'code' => 4004,
'message' => 'jwt-auth: Token is error',
]);
}
}

//判断token是否被列入黑名单(退出登录)
if ($exception instanceof \Tymon\JWTAuth\Exceptions\TokenBlacklistedException) {
if (! \auth()->guard('api')->user()) {

return response()->json([
'code' => 4001,
'message' => 'The token has been blacklisted',
]);
}
}


return parent::render($request, $exception);
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function login()
$member = $this->memberRepository->skipPresenter()->find(1);
// dd($member);
$token = auth()->guard('api')->login($member);
dd($token);
// dd($token);
return response_json('1001','成功',[
'token' => $token
]);
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/MembersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function __construct(MemberRepository $repository, MemberValidator $valid
}

/**
* http://test.cc/api/member?token=
*
* http://test.cc/member?search=name:ja&searchFields=name:like
* http://test.cc/member?filter=id;name
* Display a listing of the resource.
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,7 @@ class Kernel extends HttpKernel
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'guard' => \App\Http\Middleware\SetAuthGuard::class,
'refresh.toke' => \App\Http\Middleware\RefreshToken::class,
];
}
52 changes: 52 additions & 0 deletions app/Http/Middleware/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Http\Middleware;

use Auth;
use Closure;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Tymon\JWTAuth\Http\Middleware\BaseMiddleware;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;

class RefreshToken extends BaseMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
// 检查此次请求中是否带有 token,如果没有则抛出异常。

$this->checkForToken($request);


// 使用 try 包裹,以捕捉 token 过期所抛出的 TokenExpiredException 异常
try {
// 检测用户的登录状态,如果正常则通过
if ($this->auth->parseToken()->authenticate()) {
return $next($request);
}

throw new UnauthorizedHttpException('jwt-auth', '未登录');
} catch (TokenExpiredException $exception) {
// 此处捕获到了 token 过期所抛出的 TokenExpiredException 异常,我们在这里需要做的是刷新该用户的 token 并将它添加到响应头中
try {
// 刷新用户的 token
$token = $this->auth->refresh();
// 使用一次性登录以保证此次请求的成功
Auth::guard('api')->onceUsingId($this->auth->manager()->getPayloadFactory()->buildClaimsCollection()->toPlainArray()['sub']);
} catch (JWTException $exception) {
// 如果捕获到此异常,即代表 refresh 也过期了,用户无法刷新令牌,需要重新登录。
throw new UnauthorizedHttpException('jwt-auth', $exception->getMessage());
}
}

// 在响应头中返回新的 token
return $this->setAuthenticationHeader($next($request), $token);
}
}
36 changes: 36 additions & 0 deletions app/Http/Middleware/SetAuthGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;

class SetAuthGuard
{
/**
* @var
*/
protected $auth;

/**
* SetAuthGuard constructor.
* @param Auth $auth
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next,$guard)
{
$this->auth->shouldUse($guard);
return $next($request);
}
}
9 changes: 5 additions & 4 deletions app/Models/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
namespace App\Models;


use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Prettus\Repository\Contracts\Transformable;
use Prettus\Repository\Traits\TransformableTrait;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;

class Member extends Authenticatable implements Transformable,JWTSubject
class Member extends Model implements Transformable,JWTSubject,AuthenticatableContract
{
use TransformableTrait;
use Notifiable;
use Authenticatable;
protected $table = 'member';
protected $primaryKey = 'id';
protected $guarded = [];
Expand Down
12 changes: 12 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
use Notifiable;
}
12 changes: 7 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
|
*/

'timezone' => 'UTC',
'timezone' => 'PRC',

/*
|--------------------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
'model' => \App\Models\User::class,
],

// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
'member' => [
'driver' => 'eloquent',
'model' => \App\Models\Member::class,
],
],

/*
Expand Down
34 changes: 0 additions & 34 deletions database/migrations/2018_08_11_135546_create_members_table.php

This file was deleted.

15 changes: 12 additions & 3 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
//Route::middleware('auth:api')->get('/user', function (Request $request) {
//// return $request->user();
////});

Route::middleware(['guard:api','refresh.toke'])
->group(function () {
Route::resource('member','MembersController')->except([
'create','edit'
]);
});

Route::get('/login', 'HomeController@login')->name('login');
6 changes: 2 additions & 4 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
return view('welcome');
});

Route::resource('member','MembersController')->except([
'create','edit'
]);



Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/login', 'HomeController@login')->name('login');

0 comments on commit 7862d4f

Please sign in to comment.