我正在使用 laravel 的“HTTP 基本身份验证”功能。如果输入的凭据错误,我想自定义从 laravel 生成的错误消息。
是否可以捕获 HTTP Auth 失败时生成的 401 错误?
希望您能够帮助我。
问候
我正在使用 laravel 的“HTTP 基本身份验证”功能。如果输入的凭据错误,我想自定义从 laravel 生成的错误消息。
是否可以捕获 HTTP Auth 失败时生成的 401 错误?
希望您能够帮助我。
问候
尝试捕获401错误并返回自定义视图?!
App::error(function($exception, $code)
{
switch ($code)
{
case 401:
return Response::view('errors.403', array(), 401);
case 403:
return Response::view('errors.403', array(), 403);
case 404:
return Response::view('errors.404', array(), 404);
case 500:
return Response::view('errors.500', array(), 500);
default:
return Response::view('errors.default', array(), $code);
}
});
我认为,代码非常简单并且可以自我解释。请注意,$errors变量是类型MessageBag的,即使您没有明确设置它也可以在视图中使用!太棒了!:)
我使用了简单的路由,将其放入您的控制器中
app/routes.php
Route::get('auth', function()
{
$creds = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
);
if ( ! Auth::attempt($creds))
{
$errors = new MessageBag;
$errors->add('login', trans("Username and/or password invalid."));
return Redirect::to('/')->withErrors($errors);
}
return Redirect::to('/protected/area');
});
Route::get('/', function(){
return View::make('hello');
});
// app/views/hello.php
@if($errors->has('login'))
{{ $errors->first('login') }}
@endif
我是这样做的:
Route::filter('auth.basic', function()
{
$message = [
"error" => [
"code" => 401,
"message" => "Invalid Credentials"
]
];
$headers = ['WWW-Authenticate' => 'Basic'];
$response = Auth::basic();
if (!is_null($response)) {
return Response::json($message, 401, $headers);
}
});
如果您查看 Illuminate\Auth\Guard,您会发现 Auth::basic() 调用的基本方法。它通过 getBasicResponse 方法返回 null 或 Response 对象。
/**
* Attempt to authenticate using HTTP Basic Auth.
*
* @param string $field
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response|null
*/
public function basic($field = 'email', Request $request = null)
{
if ($this->check()) return;
$request = $request ?: $this->getRequest();
// If a username is set on the HTTP basic request, we will return out without
// interrupting the request lifecycle. Otherwise, we'll need to generate a
// request indicating that the given credentials were invalid for login.
if ($this->attemptBasic($request, $field)) return;
return $this->getBasicResponse();
}
这是getBasicResponse:
/**
* Get the response for basic authentication.
*
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function getBasicResponse()
{
$headers = array('WWW-Authenticate' => 'Basic');
return new Response('Invalid credentials.', 401, $headers);
}
在这里,我们终于有了“无效凭据”。我们希望更改的文本。我们看到它只是返回一个带有 401 状态代码和 Basic Auth 标头的 Symphony 响应实例,并且在所有其他情况下为 null。因此,我们将只检查一个非空结果,如果我们得到一个,则返回我们的新响应,如上所示。
此外,如果您希望它实际上是无状态的,您应该使用:
Auth::onceBasic()
我不知道这种方法的未来证明,但它适用于 Laravel 4.1。
最终结果再次:
Route::filter('auth.basic', function()
{
$message = [
"error" => [
"code" => 401,
"message" => "Invalid Credentials"
]
];
$headers = ['WWW-Authenticate' => 'Basic'];
$response = Auth::onceBasic();
if (!is_null($response)) {
return Response::json($message, 401, $headers);
}
});