- 去JWT的官网https://jwt.io/#libraries-io 下载PHP的JWT包
- 这里我使用的是laravel框架 在命令行里执行
- composer require lcobucci/jwt - --- 这是官方提供的代码,在你下载JWT包的时候就可以看到
- 运行成功后就会在你的项目vendor\下生成lcobucci\jwt文件
- 接下来就是代码编写
- 我们自己在app/Commend/Auth/下封装一个JwtAuth.php类
JwtAuth.php类
<?php
namespace App\Common\Auth;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\ValidationData;
class JwtAuth{
private static $instance;
private $token;
private $uid;
private $secrect='*&%$@#@#!#!^&^%*^';
private $decodeToken;
public static function getInstance(){
if(is_null(self::$instance)){
self::$instance = new self();
}
return self::$instance;
}
private function __construct()
{
}
private function __clone()
{
}
public function encode(){
$time=time();
$this->token=(new \Lcobucci\JWT\Builder())
->setHeader('alg','HS256')
->setIssuer('www.laravel.com')
->setAudience('zjw')
->setIssuedAt($time)
->setExpiration($time+3600)
->set('uid',$this->uid)
->sign(new \Lcobucci\JWT\Signer\Hmac\Sha256(),$this->secrect)
->getToken();
return $this;
}
public function getToken(){
return (string)$this->token;
}
public function setToken($token){
$this->token=$token;
return $this;
}
public function setUid($uid){
$this->uid=$uid;
return $this;
}
public function decode(){
if(!$this->decodeToken){
$this->decodeToken=(new Parser())->parse((string)$this->token);
$this->uid = $this->decodeToken->getClaim('uid');
}
return $this->decodeToken;
}
public function validate(){
$data= new ValidationData();
$data->setIssuer('www.laravel.com');
$data->setAudience($this->uid);
return $this->decode()->validate($data);
}
public function verify(){
$result=$this->decode()->verify(new \Lcobucci\JWT\Signer\Hmac\Sha256(),$this->secrect);
return $result;
}
}
public function author(){
return $this->jsonSuccessData([
'id'=>1,
'name'=>'zhaojiawei'
]);
}
定义好方法后我们需要用到laravel的中间件
- laravel的中间件用法,详情看LARAVEL书院
- 执行 php artisan make:middleware JwtAuthMiddleware
<?php
namespace App\Http\Middleware;
use App\Common\Auth\JwtAuth;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Model\permession\PermessionModel;
use Closure;
class JwtAuthMiddleware
{
use ResponseJson;
public function handle($request, Closure $next)
{
$token=$request->input('token');
if(!empty($token)){
$jwtauth=JwtAuth::getInstance();
$jwtauth->setToken($token);
if($jwtauth->validate() && $jwtauth->verify()){
return $next($request);
}else{
return $this->jsonData('1','登录过期');
}
}else{
return $this->jsonData('2','参数错误');
}
}
}
- 定义好中间件后,记得在Kernel.php中添加定义的中间件,添加成功后在web.php中声名一个路由,路由的middleware指向Kernel.php定义的中间件名称
JWT
Route::group(["prefix"=>"crontab","middleware"=>"jwt_auth"],function (){
//获取token
Route::get("index","crontab\CrontabController@index")->name("crontab.index");
});
下面这个是我自己封装的一个转化json格式类,建议放在app\http\Response\下
<?php
namespace App\Http\Response;
trait ResponseJson{
public function jsonResponse($code,$message,$data){
$content=[
'code'=>$code,
'msg'=>$message,
'data'=>$data
];
return json_encode($content,JSON_FORCE_OBJECT);
}
public function jsonSuccessData($data=[]){
return $this->jsonResponse(0,'Success',$data);
}
public function jsonData($code,$message,$data=[]){
return $this->jsonResponse($code,$message,[]);
}
}
$jwtauth=JwtAuth::getInstance();
$token=$jwtauth->setUid(1)->encode()->getToken();
}