hyperf-ext / jwt

The Hyperf JWT package.
MIT License
52 stars 23 forks source link

$this->auth->guard('api')->checkOrFail();不起作用 #10

Closed taobali32 closed 3 years ago

taobali32 commented 3 years ago

问题:

$this->auth->guard('api')->checkOrFail(); 不能拦截,并且后续打印当前用户为空

流程:

中间件代码


public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$this->auth->guard('api')->checkOrFail();
    }catch (TokenExpiredException $e){

        var_dump(2231);
        if ($e->getMessage() == 'Token has expired') {

            return $this->response->json(
                [
                    'code' => 401,
                    'data' => [],
                    'msg'  => '登陆失效'
                ]
            );

// throw new \Exception($e->getMessage()); }

    }catch (TokenInvalidException $exception){

        return $this->response->json(
            [
                'code' => 401,
                'data' => [],
                'msg'  => 'token错误'
            ]
        );

// throw new \Exception($exception->getMessage());

    }catch (\Throwable $throwable){

        $msg = $throwable->getMessage() == 'A token is required' ? '没有TOKEN' : '请登录';
        return $this->response->json(
            [
                'code' => 401,
                'data' => [],
                'msg'  => $msg
            ]
        );

    }

    return $handler->handle($request);
}

> 控制器
```php
    public function ti(TiRequest $request): array
    {
        $post = $request->validated();

        $user = $this->auth->guard('api')->user();

        var_dump($user ); 
    }

版本

"hyperf-ext/auth": "^2.1",
"hyperf-ext/jwt": "^2.1",

jwt.php配置


<?php

declare(strict_types=1); /**


> 代码中间件

![image](https://user-images.githubusercontent.com/30717763/111108236-679d9780-8593-11eb-94e2-5ec1adf2a4c8.png)

# 出错token

bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJodHRwOlwvXC9hcGkucm9hLmppYWJhb2xlc2hvcC5jb21cL2FwaVwvYXV0aFwvbG9naW4iLCJpYXQiOjE2MTU1MzcyMzgsImV4cCI6MTYxNjUzNzIzOCwibmJmIjoxNjE1NTM3MjM4LCJqdGkiOiJuSWR2UWNmMEhweUxxWmp5Iiwic3ViIjoxMDAwNTU1LCJwcnYiOiJmNmI3MTU0OWRiOGMyYzQyYjc1ODI3YWE0NGYwMmI3ZWU1MjlkMjRkIn0.GFCOnwmiq-YzD2F8877yr0IIyzyY_OIXIAQELRG6Ceu_FYPNd2PfdCWqm7Zw-oWGkFCNOPB2g43_XYh_uFfEug


# 运行结果
![image](https://user-images.githubusercontent.com/30717763/111108026-fa8a0200-8592-11eb-952d-e46e661bedc5.png)

# 结果非常致命, 现在我的临时解决
```php
   $user = $this->auth->guard('api')->user();

            if (empty($user)){
                //  more...
            }

问题发现

这个token应该是前n天生成的,今天偶然测试,发现checkOrFail不能拦截了

其他

我在 链接 中补充了常用使用方式,然后 $this->auth->guard('api')->checkOrFail(); 是大佬帮我补充的

我的.env

JWT_SECRET=MBe71G4hdUdUxROnamk4dFYkVRVY5c2BekIdvDt2ZqJuXORaQLliE8SnIPPPdRwwDLlCLTnfb4k2FdIwru/ELg==
JWT_PRIVATE_KEY="-----BEGIN ENCRYPTED PRIVATE KEY-----\nMIIJnDBOBgkqhkiG9w0BBQ0wQTApBgkqhkiG9w0BBQwwHAQILsX69csRlUMCAggA\nMAwGCCqGSIb3DQIJBQAwFAYIKoZIhvcNAwcECLUjDd9rb6OtBIIJSBXf6Daui41H\nxMUQ8+9T3eRBE0M1QWEUeKzEau5tBJ/oErSKaqO89RUMwFj9cOjN3TDwdLadUC25\nDEIGlY9JKxVKQ38Vzd+jLcZq6Q7HfFYLbLy+WXHLRk32pVgnLXsCSn8bS4s7ZFKE\nOpR2UCOW/WxIiUoLaD7IT3Mfre4sJDcnR9EcFvb8X3/X1Tjran2eGGv8qM5DOKD9\n2FWqrCoJne47Y2ZS54bFebFZSGelN7vsQ88+6e9K3st6VkPjrXpjKAMyEB1JxJpL\n+UeJEHoRGjpkuPxfoTYw8+Aoi52E+BFZnUoqshSDbwlHCWlmMLecPSDR7DCUoSyb\nd51VYS66prdFXnaRcoMRzZK/GnoS3ctudVZbjjRuBNGqg27O0mBDU5bU3N5nicSn\n5p7v7PQUbhx39XE1tJEDWdbxl9BAgBUVMmlihielgZCW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b\nZVjeqG2HwEIAMCHtqAjtdwPkuRY9Af9nJWqEiIJU/akVsqhjCtkpC1Prfj+WNzb6\n8llC/YVruqbrIQQw8vlthZchzSeAxqYJ4VJrcDE/s92c9njYLzsErucZEdL5Uel+\nuRGwTrECCgaqKKdursGTMA08kk1h6p+FlLBVn42GLwUUM/Gx3Xf2hZxaEhb8tpTQ\nHa7Rn5suCXZxeCPx+YikLk2+sIEyYA/weW8JRohnX5933bZ8sZhSH6RXPGkZn8Yh\ncP1kyczIHJ9FIRmJEcoJA786B4FQpnDRa3y7akPcl81DtfYlG6UdbLHVrmQnKiVS\ndgWYMrGzgssIMIsw8Mn+dVzpvsypDkCK9YBzFtTal7E+ly38deBFCdR29JcCYsHb\ntZ7pWxFF5BTL1H9lwztd/6kCAwEAAQ==\n-----END PUBLIC KEY-----\n"
JWT_PASSPHRASE="i29jy/McO4/RulZTm5U0WQ=="
taobali32 commented 3 years ago

问题已解决, 因为现在测试期间大量生成测试数据,然后登陆,注册操作等, 该token出现在 账号删除了 这个token还能验证成功,进入控制器。。