简体   繁体   English

在 Laravel 中登录 auth 而不使用数据库

[英]Login auth in Laravel without using database

Im somewhat new to laravel, i need to set an user/password login system, but i cannot use the default database.我对 laravel 有点陌生,我需要设置用户/密码登录系统,但我不能使用默认数据库。 Is possible to store the information credentials other than in a database?是否可以将信息凭证存储在数据库之外? What would an example look like?一个例子会是什么样子?

Laravel's Authentication System relies on Eloquent and therefore on a database. Laravel 的身份验证系统依赖于 Eloquent,因此依赖于数据库。 With Sushi you can use an Array as your database driver.使用Sushi ,您可以使用 Array 作为数据库驱动程序。 Maybe this would be an approach which could work for you.也许这将是一种适合您的方法。

Another idea could be to use Basic Authentication .另一个想法可能是使用Basic Authentication You can set this up in your webserver (Apache, Nginx, etc.) or in Laravel with a Middleware.您可以在您的网络服务器(Apache、Nginx 等)或 Laravel 中使用中间件进行设置。

Here's how such a middleware could look like.这是这样一个中间件的样子。

namespace App\Http\Middleware;

use Closure;

class BasicAuthMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $authenticationHasPassed = false;

        if ($request->header('PHP_AUTH_USER', null) && $request->header('PHP_AUTH_PW', null)) {
            $username = $request->header('PHP_AUTH_USER');
            $password = $request->header('PHP_AUTH_PW');

            if ($username === config('auth.basic_auth.username') && $password === config('auth.basic_auth.password')) {
                $authenticationHasPassed = true;
            }
        }

        if ($authenticationHasPassed === false) {
            return response()->make('Invalid credentials.', 401, ['WWW-Authenticate' => 'Basic']);
        }

        return $next($request);
    }
}

(This code example is from a blog post of mine, where I needed an authentication feature without using a database). (这个代码示例来自我的一篇博客文章,我需要一个身份验证功能而不使用数据库)。

I recommend reading the documentation about Middleware and Configuration if you want to implement this.如果您想实现这一点,我建议您阅读有关中间件配置的文档。

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

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