如何在 laravel 中的中间件 class 中检查 sanctum 中间件
how to check santcum middleware in middleware class in laravel
我有两种 api 身份验证方式,如果我的第一种方式失败,我想检查 santcum laravel auth 。
我制作了 midlleware class 但我不知道如何检查其中的 santcum
<?php
namespace App\Http\Middleware;
class UserApiAuthenticated
{
/**
* User authenticator container
* @var UserAuthenticatorServiceInterface
*/
protected UserAuthenticatorServiceInterface $user_authenticator_service;
public function __construct(UserAuthenticatorServiceInterface$user_authenticator_service)
{
$this->user_authenticator_service = $user_authenticator_service;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request):
(\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if ($this->user_authenticator_service->isUserAuthenticated($request)) {
return $next($request);
} elseif () {
}
我会做类似的事情:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param string|null ...$guards
*
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard)
{
if (Auth::guard($guard)->check())
{
return $next($request);
}
}
abort(403, 'Unauthenticated')
}
它一一检查所有守卫,
当你使用中间件时,你设置了守卫:
->middleware('mymiddleware:web,sanctum')
或使用路由组:
Route::group(['middleware' => ['mymiddleware:web,sanctum',
如果你有共同的路由,你可以同时使用两种守卫,对于特定于一种身份验证方法的路由,你可以设置你需要的正确守卫
但最后我会检查来自 laravel 的 auth 中间件,我确定它可以满足您的需要 (Illuminate\Auth\Middleware\Authenticate)
我有两种 api 身份验证方式,如果我的第一种方式失败,我想检查 santcum laravel auth 。 我制作了 midlleware class 但我不知道如何检查其中的 santcum
<?php
namespace App\Http\Middleware;
class UserApiAuthenticated
{
/**
* User authenticator container
* @var UserAuthenticatorServiceInterface
*/
protected UserAuthenticatorServiceInterface $user_authenticator_service;
public function __construct(UserAuthenticatorServiceInterface$user_authenticator_service)
{
$this->user_authenticator_service = $user_authenticator_service;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request):
(\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
if ($this->user_authenticator_service->isUserAuthenticated($request)) {
return $next($request);
} elseif () {
}
我会做类似的事情:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @param string|null ...$guards
*
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard)
{
if (Auth::guard($guard)->check())
{
return $next($request);
}
}
abort(403, 'Unauthenticated')
}
它一一检查所有守卫, 当你使用中间件时,你设置了守卫:
->middleware('mymiddleware:web,sanctum')
或使用路由组:
Route::group(['middleware' => ['mymiddleware:web,sanctum',
如果你有共同的路由,你可以同时使用两种守卫,对于特定于一种身份验证方法的路由,你可以设置你需要的正确守卫
但最后我会检查来自 laravel 的 auth 中间件,我确定它可以满足您的需要 (Illuminate\Auth\Middleware\Authenticate)