Laravel 5-如何用用户名代替电子邮件使用基本身份验证?
大家好 所以在拉威尔4我们可以Laravel 5-如何用用户名代替电子邮件使用基本身份验证?,laravel,laravel-5,basic-authentication,laravel-middleware,Laravel,Laravel 5,Basic Authentication,Laravel Middleware,大家好 所以在拉威尔4我们可以 Route::filter('auth.basic', function() { return Auth::basic('username'); }); 但是现在这不可能了,医生也没有给出一个关于怎么做的线索。有人能帮忙吗 谢谢 使用与默认中间件相同的代码创建新的自定义中间件: 并覆盖默认的“电子邮件”字段,如: return $this->auth->basic('username') ?: $next($request); 这就是我正
Route::filter('auth.basic', function()
{
return Auth::basic('username');
});
但是现在这不可能了,医生也没有给出一个关于怎么做的线索。有人能帮忙吗
谢谢 使用与默认中间件相同的代码创建新的自定义中间件: 并覆盖默认的“电子邮件”字段,如:
return $this->auth->basic('username') ?: $next($request);
这就是我正在使用的
class AuthenticateOnceWithBasicAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
return Auth::onceBasic('username') ?: $next($request);
}
}
使用Laravel 5.7,handle方法如下所示:
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @param string|null $field
* @return mixed
*/
public function handle($request, Closure $next, $guard = null, $field = null)
{
return $this->auth->guard($guard)->basic($field ?: 'email') ?: $next($request);
}
如果查看函数定义,可以指定$字段
值
您可以提供中间件参数:
定义路由时,可以通过将中间件名称和参数分隔为:。多个参数应以逗号分隔:
使用以下命令,我可以指定要在基本身份验证中使用的字段:
Route::middleware('auth.basic:,username')->get('/<route>', 'MyController@action');
您会注意到,
$next
之后有两个参数$guard
默认为null
,我希望它保持null/空,因此我省略了该值并提供了一个空字符串。下一个参数(用逗号分隔,如文档所示)是我想用于基本身份验证的$字段。是的,我就是这么想的。。。所以没有比这更好的了呃。。。谢谢:)谢谢。真的,我在文档中的任何地方都找不到它
public function handle($request, Closure $next, $guard = null, $field = null)