jybtx / token-auth
此包已被废弃且不再维护。未建议替代包。
为laravel提供的简单JSON Web Token验证
v2.0.15
2021-07-14 06:15 UTC
Requires
- php: >=7.0.0
- lcobucci/jwt: 3.3
README
这是一个token认证的扩展包,使用此扩展包前请确保已安装Redis扩展,如不需要缓存Secret key,请在配置文件中关闭 'cache_open' => false
,这样就不会将生成的Secret key存储到Redis中。^-^
安装
Composer
执行以下命令以获取包的最新版本
composer require jybtx/token-auth
Laravel
>= laravel5.5
ServiceProvider将自动附加
其他
在你的 config/app.php
中,将 Jybtx\TokenAuth\Provider\TokenAuthServiceProvider::class
添加到 providers
数组的末尾
'providers' => [ ... Jybtx\TokenAuth\Providers\TokenAuthServiceProvider::class, ], 'aliases' => [ ... "TokenAuth" => Jybtx\TokenAuth\Facades\TokenAuthFace::class, ]
发布配置
运行以下命令以发布包配置文件
php artisan vendor:publish --provider "Jybtx\TokenAuth\Provider\TokenAuthServiceProvider"
创建token Secret key
php artisan token:generate
这将更新你的 .env 文件,例如 JWT_SECRET_KEY=base64:foobar
用法
创建TestController
<?php use TokenAuth; class TestController extends Controller { public function refresh() { $token = TokenAuth::getRefreshToken(); return $this->respondWithToken($token); } public function getToken() { $data = [ 'name' => 'joe', 'age' => 18, 'sex' => 'girl', 'like' => 'sport' ]; $flag = 'user-name'; // 用户的唯一标识 $token = TokenAuth::getCreateAccessToken( $data, $flag ); return $this->respondWithToken($token); } protected function respondWithToken($token) { return response()->json([ 'access_token' => $token, 'token_type' => 'bearer', 'expires_in' => gettl() ]); } public function me() { return authUser(); } public function logout() { TokenAuth::TokenAddBlacklist(); return response()->json(['status'=>'success','message' => 'Successfully logged out']); } }
中间件
<?php namespace Jybtx\TokenAuth\Http\Middleware; use Jybtx\TokenAuth\JwtAuthToken; use Illuminate\Support\Facades\Redis; use Jybtx\TokenAuth\Support\CreateToken; use Jybtx\TokenAuth\Support\TokenValidator; use Jybtx\TokenAuth\Support\TokenBlackList; use Jybtx\TokenAuth\Support\AuthenticationHeader; abstract class BaseMiddleware { use TokenValidator,AuthenticationHeader,TokenBlackList,CreateToken; /** * [checkTokenRefreshTimeForRestApi description] * @author jybtx * @date 2020-05-06 * @return [type] [description] */ public function checkTokenRefreshTimeForRestApi() { return $this->verifyRefresh( getoken() ); } /** * [Check token value of user REST API] * @author jybtx * @date 2019-12-16 * @return [type] [description] */ public function checkTokenForRestApi() { return $this->getVerifyToken( getoken() ); } /** * Set the authentication header. * * @param \Illuminate\Http\Response|\Illuminate\Http\JsonResponse $response * @param string|null $token * * @return \Illuminate\Http\Response|\Illuminate\Http\JsonResponse */ public function setAuthenticationHeaders($response, $token = null) { return $this->getSetAuthenticationHeader($response, $token); } /** * [Check the blacklist token value of the user REST API] * @author jybtx * @date 2019-12-16 * @return [type] [description] */ public function checkTokenIsInBlacklistForApi() { return Redis::exists( md5( getoken() ) ); } }
<?php use Jybtx\TokenAuth\Http\Middleware\BaseMiddleware; class xxxxMiddleware extends BaseMiddleware { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { /** * 验证token是否在黑名单中 */ if ( $this->checkTokenIsInBlacklistForApi() ) return response()->json(['status'=>100,'message'=>"token 无效请重新登录!"]); /** * 检查token是否有效 * token在有效期内重新更新token值 * 设置响应头 */ if ( !$this->checkTokenForRestApi() ) { if ( $this->checkTokenRefreshTimeForRestApi() ) { return $this->setAuthenticationHeaders($next($request)); } else { return response()->json(['status'=>100,'message'=>"token 无效请重新登录!"]); } } return $next($request); } }
帮助函数
获取用户所有信息
authUser()
获取用户token
getoken()
获取配置的TTL时间
gettl()
用户自己获取token
get_token_data( $string )
许可
MIT许可证 (MIT)