1.创建数据库表实例

docker exec -it swoft php bin/swoft entity:create -d swoft user

2.安装jwt扩展

a.进入docker swoft
	docker exec -it swoft sh
b.进入目录
	cd /var/www/swoft
c.利用composer安装
	composer require swoft/uth

3.重启docker swoft容器

4.编写测试类

use \Firebase\JWT\JWT;

/**
     * @RequestMapping()
     */
    function access_token(Request $request){
        $uname=$request->query('uname',false);
        $usec=$request->query('usec',false);
        $getUser= Query::table(User::class)
           ->where('user_pwd',$usec)
            ->where('user_name',$uname)
            ->one()
            ->getResult();
        $key="hboa";
        if ($getUser){
//            定义token
            $token=[
                'iss'=>'www.baidu.com',
                'uname'=>$getUser,
            ];
//            对称加密

            $token=JWT::encode($token,$key);
            return ['token'=>$token];
        }else{
            return ['token'=>''];
        }

    }

5.编写测试解密类

 /**
     * @RequestMapping(route="verify",method={RequestMethod::GET})
     * 测试解密
     */
    function verify(Request $request){
       $getToken=$request->query("token",false);
        $key="hboa";
        $userInfo=JWT::decode($getToken,$key,["HS256"]);
       return array($userInfo);
    }

6.针对异常处理

exception下创建一个处理类基础主类

swoft速学~token认证1-LMLPHP

swoft速学~token认证1-LMLPHP

swoft速学~token认证1-LMLPHP

7.使用中间件处理异常

  1. 编写中间件文件 放test下TokenMiddWare.php
    <?php
    namespace App\Test;
    use App\Models\Entity\Products;
    use Psr\Http\Message\ResponseInterface;
    use Psr\Http\Message\ServerRequestInterface;
    use Psr\Http\Server\RequestHandlerInterface;
    use Swoft\Http\Message\Middleware\MiddlewareInterface;
    use \Swoft\Bean\Annotation\Bean;
    use Swoole\Exception;
    use \Firebase\JWT\JWT;
    /**
     * @Bean("TokenMiddWare")
     */
    class TokenMiddWare implements MiddlewareInterface
    {
    
        public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
        {
    //        获取token参数
            $getToken=$request->query("token",false);
    //        验证是否存在不存在就异常
            if (!$getToken)throw new Exception("no token");
    //        解析token
            $key="hboa";
            $userInfo=(array)JWT::decode($getToken,$key,["HS256"]);
    //        塞入头变量
            $users=$userInfo['uname'];
            foreach($users as $key=>$value)
            {
                $request=  $request->withAddedHeader("token_".$key,$value);
            }
            $response = $handler->handle($request);
            return $response;
        }
    }
    
  2. 页面里操作
    <?php
    namespace App\Controllers\Api;
    
    use App\Models\Entity\UserCoins;
    use App\Models\Entity\Users;
    use Swoft\Db\Query;
    use Swoft\Http\Message\Server\Request;
    use Swoft\Http\Server\Bean\Annotation\Controller;
    use Swoft\Http\Server\Bean\Annotation\RequestMapping;
    use Swoft\Http\Server\Bean\Annotation\RequestMethod;
    use Swoft\Http\Message\Bean\Annotation\Middleware;
    /**
     * @Controller("/users")
     * @Middleware(class=\TokenMiddWare::class)
     */
    class UsersController
    {
    
        /**
         * @RequestMapping(route="test",method={RequestMethod::GET})
         */
        function test(Request $request)
        {
            return $request->getHeader("token_uname");
           //return $request->getHeaders();
        }
    
    
    }
    
  3. 访问方法必须带token进去swoft速学~token认证1-LMLPHP
  4. 加入过期时间  加密前JWT::encode
     $token=[
                    'iss'=>'www.baidu.com',
                    'uname'=>$getUser,
    //                过期时间
                    'exp'=>time()+10000
                ];
01-31 12:31