gpt4 book ai didi

php - 自定义中间件 Laravel 不适用于 api 路由

转载 作者:行者123 更新时间:2023-12-02 03:35:59 24 4
gpt4 key购买 nike

我有中间件调用用户,它是过滤数据库用户表上的角色。这是我的中间件,名为 user

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;
use UsersData;
class User
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if(Auth::check() && Auth::User()->role=='user'){
return $next($request);
}
return redirect()->route('login')->with('danger',"You don't have an access");
}
}

我已经在内核中注册了我的中间件

protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'user' => \App\Http\Middleware\User::class,
'ajax' => \App\Http\Middleware\Ajax::class,
];

以及api.php的路由

Route::middleware('user')->group(function () {
Route::post('province','ApiController@getcity')->name('api.getcity');
Route::post('courier/getcost','ApiController@getCourierCost')->name('api.getcouriercost');
});

更新config/auth.php这里是守卫

<?php

return [

/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|
*/

'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],

'api' => [
'driver' => 'token',
'provider' => 'users',
],
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],

// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],

/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/

'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
],
],

];

web.php 路由上一切正常,但这个 api.php 不起作用?

如果您发表评论,我真的很感激!

最佳答案

在 ajax 请求中,您无法像这样检查身份验证:

Auth::check();

因为在ajax中你没有任何 session 。当每个用户发送第一个登录请求时,您必须为每个用户发送一个随 secret 钥,并且当登录成功时,将其保存在他数据库的 key 字段中,之后当他想要发送请求时,他必须发送 key ,并且您如果可以让他进入,将与数据库中的 key 进行检查。

关于php - 自定义中间件 Laravel 不适用于 api 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50195921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com