在Laravel HTTP基本身份验证实现中,它使用电子邮件作为用户名,我们如何将列更改为用户名?



>laravel我们可以使用Http Basic Authentication来方便用户login。 没有登录页面,通过附加middleware->('auth.basic'),到web.phpRoute的末尾,但默认情况下它以电子邮件作为用户名,我该如何更改它? 谢谢。

Route::get('/somePage','Controller@controllerFunction')
->middleware('auth.basic');

图片: https://drive.google.com/file/d/1gbzE0azW8TfZsvQN7k7ZoG2PIRlo14i6/view?usp=sharing

从文档中:

将中间件附加到路由后,您将 在访问路由时自动提示输入凭据 您的浏览器。默认情况下,auth.basic 中间件将使用电子邮件 列作为"用户名"。

默认中间件中有一个handle()方法。

您必须创建自己的中间件并覆盖handle()

namespace appHttpMiddleware;
use Closure;
use IlluminateAuthMiddlewareAuthenticateWithBasicAuth;
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);
}
}

比更新您的AppHttpKernel

'auth.basic' => appHttpMiddlewareYourBasicAuthMiddleware::class,

相关内容

最新更新