Laravel 4 Basic Auth自定义错误 [英] Laravel 4 Basic Auth custom error

查看:81
本文介绍了Laravel 4 Basic Auth自定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用laravel的"HTTP基本身份验证"功能.如果输入的凭据错误,我想自定义从laravel生成的错误消息.

I'm using the 'HTTP Basic Authentication' feature of laravel. I want to customize the error message which is generated from laravel if the entered credentials are wrong.

是否可以捕获HTTP身份验证失败时生成的401错误?

Is it possible to catch the 401 Error which is generated when HTTP Auth fails?

希望你能帮助我.

致谢

推荐答案

这是我的操作方式:

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对象.

If you look in Illuminate\Auth\Guard you'll find the basic method that's called by Auth::basic(). It either returns null or a Response object via the getBasicResponse method.

/**
 * 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:

Here's 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.因此,我们将仅检查非空结果,如果得到一个非空结果,则返回如上所示的新响应.

Here we finally have our 'Invalid credentials.' text that we're looking to change. We see it's just returning an instance of a Symphony response with a 401 status code and the Basic Auth header and null in all other occasions. So, we'll just check for a non-null result and if we get one, return our new response as shown above.

此外,如果您希望它实际上是无状态的,则应使用:

Also, if you want it to actually be stateless you should use:

Auth::onceBasic()

我不知道这种方法在未来的应用前景如何,但是从Laravel 4.1开始就可以使用.

I don't know how future proof this method is, but it works as of 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);
    }
});

这篇关于Laravel 4 Basic Auth自定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆