Laravel认证和授权与REMOTE_USER



我已经有以下设置:

Apache Server 2.4.x使用Kerberos进行SSOLaravel 5.5PHP 7.xMSSQL数据库12具有现有数据库结构,可以自定义使用Usertable

在我的laravel应用程序中,用户只有在他/她已经通过了apache登录时才能访问该应用程序,这意味着我可以使用

从服务器检索远程_user变量
$user = $_SERVER['REMOTE_USER'];

我打算做的是在其余的应用程序中使用Laravel身份验证和授权。我研究了很多例子,但没有发现真正适合的。

在访问应用程序以获得凭据时,有人可以提示如何验证laravel中的$用户吗?

homecontroller还不够

public function __construct()
{
    $this->middleware('logincustom');
}

的中间件
class CustomLogin
{
    public function handle($request, Closure $next)
    {
        $user = $_SERVER['REMOTE_USER']; // get remoteusername
        if (Auth::user() != null) { // if user not null
            if ($user ==  Auth::user()->login) { // check if remoteuser is equal to currently login user
                    if (Auth::check()) {
                        return $next($request);
                    }
            }
        }
        $user = DB::table('settings.user')
            ->where('login',"=",$user)
            ->first();
         $authenticated = Auth::attempt(['login' => $user->login, 'password' => "xxx"]);
        if ($authenticated == null ) {
            abort(403, 'Unauthorized action.');
        }

       return $next($request);
    }
}

因此,这解决了我的问题,以基于外部的自动登录,已经经过身份验证的用户!

几个小时后我设法使它起作用:

i用户homecontroller和

public function index()

在函数索引((中,我使用以下代码:


$user = $_SERVER['REMOTE_USER']; // user from apache $env variable 
                                 // authenticated via kerberos
$user = DB::table('users')
    ->where('users.name',"=",$user)
    ->first();
    
Auth::attempt(['name' => $user->name, 'password' => "xxx"]);
    
return view('start');

也许对某人很有用!

相关内容

最新更新