英文:
Class \"Tymon\\JWTAuth\\Middleware\\BaseMiddleware\" not found in laravel 9
问题
In Laravel 9,我正在使用tymon\jwt-auth包。在composer.json中,我添加了以下内容:
"tymon/jwt-auth": "2.x"
在config/app.php的providers数组中,我添加了以下内容:
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
以及别名:
'JWTAuth' => Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' => Tymon\JWTAuth\Facades\JWTFactory::class,
在app\Http\Kernel.php中,我定义了自己的中间件Customer.php,并导入了以下内容:
use Tymon\JWTAuth\Middleware\BaseMiddleware;
但是上面的类未找到。我将use语句替换为:
use Tymon\JWTAuth\Http\Middleware\Authenticate;
但是这个类已经被弃用。请问如何解决这个错误?我已经花了三天时间但没有进展。Http\Middleware中的所有类都已被弃用。
(Note: The above response includes translations of the code and text you provided without additional information or answers to specific questions.)
英文:
In laravel 9 I am using tymon\jwt-auth package.
In composer.json"tymon/jwt-auth": "2.x",
In config/app.php in providers array i added
Tymon\JWTAuth\Providers\LaravelServiceProvider::class,
and aliases
'JWTAuth'=>Tymon\JWTAuth\Facades\JWTAuth::class,
'JWTFactory' =>Tymon\JWTAuth\Facades\JWTFactory::class,
app\Http\Kernel.php
protected $routeMiddleware = [
...
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
];
config\auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
]
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
I am writing my own middleware named as Customer.php and importing this
use Tymon\JWTAuth\Middleware\BaseMiddleware;
in my middleware, but the above class is not found. I replaced the use statement as
use Tymon\JWTAuth\Http\Middleware\Authenticate;
But this class is depricated.
Kindly help how can I get rid of this error. I have spent three days but no progress yet. All the classes in Http\Middleware are deprecated.
答案1
得分: 1
以下是您要翻译的内容:
"The classes are marked as deprecated in the repository and the maintainer likely has a reason to mark them as such. I have not found any hints about this middleware in the documentation either.
I suggest using the documented authorization method in a custom middleware without relying on the BaseMiddleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureUserIsAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (! auth()->validate($credentials)) {
// credentials are invalid, redirect user to login?
}
return $next($request);
}
}
and then use that middleware on your routes with the standard middleware configuration in the Kernel.php
protected $middlewareAliases = [
'jwt-authenticated' => \App\Http\Middleware\EnsureUserIsAuthenticated::class,
];
英文:
The classes are marked as deprecated in the repository and the maintainer likely has a reason to mark them as such. I have not found any hints about this middleware in the documentation either.
I suggest using the documented authorization method in a custom middleware without relying on the BaseMiddleware:
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class EnsureUserIsAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (! auth()->validate($credentials)) {
// credentials are invalid, redirect user to login?
}
return $next($request);
}
}
and then use that middleware on your routes with the standard middleware configuration in the Kernel.php
protected $middlewareAliases = [
'jwt-authenticated' => \App\Http\Middleware\EnsureUserIsAuthenticated::class,
];
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论