简体   繁体   English

设置Laravel Basic Auth的领域?

[英]Set realm of Laravel Basic Auth?

When you use basic auth filter in Laravel 4.2, I noticed that you can't set the realm (maybe just me?) for the authentication, and therefore when the auth window appears, it looks like this in internet explorer: 当您在Laravel 4.2中使用基本身份验证过滤器时,我注意到您无法设置身份验证realm (也许就是我吗?),因此,当出现身份验证窗口时,它在Internet Explorer中看起来像这样:

在此处输入图片说明

Take a look at the official documentation example : http://php.net/manual/en/features.http-auth.php#example-372 看一下官方文档示例: http : //php.net/manual/zh/features.http-auth.php#example-372

If I had used this, the above login window will say Restricted area rather than null . 如果使用了此功能,则上面的登录窗口将显示Restricted area而不是null

Any idea how to set the realm of basic auth in Laravel? 任何想法如何在Laravel中设置基本身份验证领域?

Also, how or where do you set/style the text to display when the auth fails or user hits the cancel button? 另外,当身份验证失败或用户单击“取消”按钮时,如何设置或设置显示样式的文本?

Found that, but nothing laravelish. 发现了这一点,但没什么可说的。

header('WWW-Authenticate: Basic realm="REALM"');
header('HTTP/1.0 401 Unauthorized');

Use the following route filter : 使用以下路由过滤器:

Route::filter('auth.basic', function() {
    $response = Auth::basic();

    if (!is_null($response)) {
        return $response->header("WWW-Authenticate", 'Basic realm="REALM"');
    }
});

Adapted from this answer which explains how it works in detail; 改编自此答案 ,解释了其详细工作方式; basically Auth::basic() returns either null if the user is already authenticated or a 401 Unauthorized response with a WWW-Authenticate header, if the response isn't null we call the header method on that Response to replace that header with our custom version of WWWi-Authenticate including the realm parameter; 基本上Auth::basic()如果用户已经过身份验证,则返回null或者使用WWW-Authenticate标头返回401 Unauthorized响应;如果响应不为null,则在该Response上调用header方法,以使用自定义替换该标头的版本WWWi-Authenticate包括realm参数; if you look at that method you'll notice that it's set to replace any previous headers by default. 如果您查看该方法,则会注意到该方法默认设置为替换以前的所有标头。

Finally the return value of that method is the response object itself so we can return that instead of explicitly returning $response on a new line. 最后,该方法的返回值是响应对象本身,因此我们可以返回该值,而不是在新行上显式返回$response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM