繁体   English   中英

Laravel:目标 [Lcobucci\JWT\Parser] 不可实例化

[英]Laravel: Target [Lcobucci\JWT\Parser] is not instantiable

嘿,我在我的产品网站上尝试使用 Laravel 护照登录时遇到问题。 它说我的 Lcobucci JWT 解析器不可实例化。 它在本地适用于我,但不适用于我的遥控器。

我该如何解决这个问题?

错误:

exception: "Illuminate\Contracts\Container\BindingResolutionException"
file: "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php"
line: 1038
message: "Target [Lcobucci\JWT\Parser] is not instantiable while building [Laravel\Passport\PersonalAccessTokenFactory]."
trace: [,…]

登录 Controller 方法:

public function login(Request $request) {

        $login = $request->validate([
            'email' => 'required:string',
            'password' => 'required:string'
        ]);

        if(filter_var($request->email, FILTER_VALIDATE_EMAIL)) {
            //user sent their email 
            Auth::attempt(['email' => $request->email, 'password' => $request->password]);
        } else {
            //they sent their username instead 
            Auth::attempt(['username' => $request->email, 'password' => $request->password]);
        }

        if(!Auth::check()) {
            return response([
                'status' => 'fail',
                'message' => 'Invalid credentials'
            ]);
        }

        $accessToken = Auth::user()
            ->createToken('authToken')
            ->accessToken;
        
        return response([
            'status' => 'success',
            'user' => new User_Resource(Auth::user()),
            'access_token' => $accessToken 
        ]);
    }

我也遇到了同样的问题,在你的项目 composer.json 添加 "lcobucci/jwt": "3.3.3" 并执行 composer update。 我在以下位置找到了这个解决方案: https://github.com/laravel/passport/issues/1381

Laravel:“8”和 Lcobucci\JWT:“^3.4”

解决方案:

use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Token\Parser;
.
.
...
public function GetTokenId(Request $request)
{
  // Get the Access_Token from the request
  $Token = $request->bearerToken();
  // Parse the Access_Token to get the claims from them the jti(Json Token Id)
  $TokenId = (new Parser(new JoseEncoder()))->parse($token)->claims()
   ->all()['jti'];
  return $tokenId;
}

如果有人仍然收到此错误,那是因为$verifiedIdToken->getClaim('sub')已在 3.4 中弃用并在 4.0 中删除。 使用$verifiedIdToken->claims()->get('sub')这个 insted。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM