在 Laravel HTTP 基本身份验证实现中,它使用电子邮件作为用户名,我们如何将列更改为用户名?
in Laravel HTTP Basic Authentication implementation it uses email for username , how do we change the column to be username?
在laravel
中我们可以使用Http Basic Authentication
来方便用户login
。在没有登录页面的情况下,通过将 middleware->('auth.basic')
附加到 web.php
中 Route
的末尾,但默认情况下它会收到电子邮件作为用户名,我该如何更改它?
谢谢。
Route::get('/somePage','Controller@controllerFunction')
->middleware('auth.basic');
图片:
https://drive.google.com/file/d/1gbzE0azW8TfZsvQN7k7ZoG2PIRlo14i6/view?usp=sharing
来自docs:
Once the middleware has been attached to the route, you will
automatically be prompted for credentials when accessing the route in
your browser. By default, the auth.basic middleware will use the email
column on the user record as the "username".
默认中间件中有一个handle()
方法。
您必须创建自己的中间件并覆盖 handle()
:
namespace app\Http\Middleware;
use Closure;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
class YourBasicAuthMiddleware extends AuthenticateWithBasicAuth
{
public function handle($request, Closure $next, $guard = null, $field = null)
{
$this->auth->guard($guard)->basic($field ?: 'YOUR_FIELD'); //place here the name of your field
return $next($request);
}
}
比更新你的 App\Http\Kernel
:
'auth.basic' => \app\Http\Middleware\YourBasicAuthMiddleware::class,
在laravel
中我们可以使用Http Basic Authentication
来方便用户login
。在没有登录页面的情况下,通过将 middleware->('auth.basic')
附加到 web.php
中 Route
的末尾,但默认情况下它会收到电子邮件作为用户名,我该如何更改它?
谢谢。
Route::get('/somePage','Controller@controllerFunction')
->middleware('auth.basic');
图片: https://drive.google.com/file/d/1gbzE0azW8TfZsvQN7k7ZoG2PIRlo14i6/view?usp=sharing
来自docs:
Once the middleware has been attached to the route, you will automatically be prompted for credentials when accessing the route in your browser. By default, the auth.basic middleware will use the email column on the user record as the "username".
默认中间件中有一个handle()
方法。
您必须创建自己的中间件并覆盖 handle()
:
namespace app\Http\Middleware;
use Closure;
use Illuminate\Auth\Middleware\AuthenticateWithBasicAuth;
class YourBasicAuthMiddleware extends AuthenticateWithBasicAuth
{
public function handle($request, Closure $next, $guard = null, $field = null)
{
$this->auth->guard($guard)->basic($field ?: 'YOUR_FIELD'); //place here the name of your field
return $next($request);
}
}
比更新你的 App\Http\Kernel
:
'auth.basic' => \app\Http\Middleware\YourBasicAuthMiddleware::class,