php开发接口jwt使用,Laravel配合jwt使用的方法实例

测试使用的是Laravel5.5版本。

安装composer require tymon/jwt-auth=1.0.0-rc.5

配置

生成配置php artisan vendor:publish --provider="TymonJWTAuthProvidersLaravelServiceProvider"

php artisan jwt:secret

auth配置<?php

return [

...

'defaults' => [

'guard' => 'web',

'passwords' => 'users',

],

'guards' => [

'web' => [

'driver' => 'session',

'provider' => 'users',

],

// 使用jwt

'api' => [

'driver' => 'jwt',

'provider' => 'apiUser',

],

],

'providers' => [

...

// 指定model

'apiUser' => [

'driver' => 'eloquent',

'model' => AppApiUser::class,

],

],

];

编码

控制器:<?php

namespace AppHttpControllersApi;

use AppApiUser;

use AppHttpControllersController;

use IlluminateHttpRequest;

use TymonJWTAuthFacadesJWTAuth;

class AuthController extends Controller

{

/**

* 中间件去除login和refresh

*

* @return void

*/

public function __construct()

{

$this->middleware('auth:api', ['except' => ['login','refresh']]);

}

/**

* Get a JWT via given credentials.

*

* @return IlluminateHttpJsonResponse

*/

public function login(Request $request)

{

$credentials = $request->only('phone', 'password');

if (count($credentials) < 2) {

return response()->json(['error' => 'Unauthorized'], 401);

}

$user = ApiUser::where('phone', $credentials['phone'])

->where('password', md5($credentials['password']))

->first();

if (empty($user) || !$token = JWTAuth::fromUser($user)) {

return response()->json(['error' => 'Unauthorized'], 401);

}

// dd($token);

return $this->respondWithToken($token);

}

/**

* Get the authenticated User.

*

* @return IlluminateHttpJsonResponse

*/

public function me()

{

return response()->json(auth('api')->user());

}

/**

* Log the user out (Invalidate the token).

*

* @return IlluminateHttpJsonResponse

*/

public function logout()

{

auth()->logout();

return response()->json(['message' => 'Successfully logged out']);

}

/**

* Refresh a token.

*

* @return IlluminateHttpJsonResponse

*/

public function refresh()

{

return $this->respondWithToken(auth('api')->refresh());

}

/**

* Get the token array structure.

*

* @param string $token

*

* @return IlluminateHttpJsonResponse

*/

protected function respondWithToken($token)

{

return response()->json([

'access_token' => $token,

'token_type' => 'bearer',

'expires_in' => auth('api')->factory()->getTTL() * 60

]);

}

}

路由:

此处注意,我为了方便测试,使用了get方法,生产环境不建议使用get。// routes/api.php

Route::middleware('api')->prefix('auth')->namespace('Api')->group(function () {

Route::get('login', 'AuthController@login');

Route::post('logout', 'AuthController@logout');

Route::get('refresh', 'AuthController@refresh');

Route::get('me', 'AuthController@me');

});

测试一下:

a2972a32560798724ea224aed5b6412a.png

d508b5779518e704d3811ea0ffdb74e2.png

076fb58b45e659f25f1e57791e0fa70e.png

unauthenticated处理

这里需要注意下,unauthenticated处理一下比较好,否则会默认跳转login登录页面。<?php

namespace AppExceptions;

use Exception;

use IlluminateFoundationExceptionsHandler as ExceptionHandler;

use IlluminateAuthAuthenticationException;

class Handler extends ExceptionHandler

{

...

protected function unauthenticated($request, AuthenticationException $exception)

{

return response()->json(['message' => 'Unauthenticated.'], 401);

/*非api可以这么处理

return $request->expectsJson()

? response()->json(['message' => 'Unauthenticated.'], 401)

: redirect()->guest(route('login'));

*/

}

}

加入token refresh

加入中间件代码:<?php

namespace AppHttpMiddleware;

use Closure;

use TymonJWTAuthFacadesJWTAuth;

use TymonJWTAuthExceptionsJWTException;

use IlluminateAuthAuthenticationException;

use TymonJWTAuthExceptionsTokenExpiredException;

use IlluminateHttpExceptionsHttpResponseException;

use TymonJWTAuthHttpMiddlewareBaseMiddleware;

class RefreshToken extends BaseMiddleware

{

/**

* Handle an incoming request.

*

* @param IlluminateHttpRequest $request

* @param Closure $next

* @return mixed

*/

public function handle($request, Closure $next)

{

try{

//检查请求中是否带有token 如果没有token值则抛出异常

$this->checkForToken($request);

if ($request->user = JWTAuth::parseToken()->authenticate()) {

return $next($request);

}

throw new AuthenticationException('Unauthorized', []);

}catch (TokenExpiredException $exception){

//返回特殊的code

throw new HttpResponseException(response()->json([

'message' => 'token expired'

]));

} catch (Exception $exception) {

throw new AuthenticationException('Unauthorized', []);

}

}

}

注册:<?php

namespace AppHttp;

use IlluminateFoundationHttpKernel as HttpKernel;

class Kernel extends HttpKernel

{

...

protected $routeMiddleware = [

'token.refresh' => AppHttpMiddlewareRefreshToken::class,

'auth.basic' => IlluminateAuthMiddlewareAuthenticateWithBasicAuth::class,

'bindings' => IlluminateRoutingMiddlewareSubstituteBindings::class,

'can' => IlluminateAuthMiddlewareAuthorize::class,

'guest' => AppHttpMiddlewareRedirectIfAuthenticated::class,

'throttle' => IlluminateRoutingMiddlewareThrottleRequests::class,

];

}

相应的控制器构造函数修改:public function __construct()

{

$this->middleware('token.refresh', ['except' => ['login','refresh']]);

}

把token时间设置成1分钟,测试一下。

99898f83e06e27eef68b59cc3522ce23.png

可以根据api返回,去调用刷新接口。

简单使用就是这样啦。

总结

到此这篇关于Laravel配合jwt使用的文章就介绍到这了,更多相关Laravel配合jwt使用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值