【问题标题】:Laravel authentication & authorisation with REMOTE_USERLaravel 使用 REMOTE_USER 进行身份验证和授权
【发布时间】:2018-02-28 18:25:37
【问题描述】:

我有以下设置:

使用 Kerberos 进行 SSO 的 Apache Server 2.4.x 拉拉维尔 5.5 PHP 7.x MSSQL Database 12 现有数据库结构,可自定义Usertable

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

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

我打算对应用程序的其余部分使用 Laravel 身份验证和授权。我查看了很多示例,但没有找到真正适合的示例。

在访问应用程序以获取凭据时,任何人都可以提示如何在 Laravel 中对 $user 进行身份验证吗?

【问题讨论】:

  • 我想这可能与 Auth::attempt(['email' => $user, 'password' => $password])) - 将在这里发布解决方案!

标签: php authentication laravel-5


【解决方案1】:

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);
    }
}

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

【讨论】:

    【解决方案2】:

    几个小时后我设法让它工作:

    我使用 Homecontroller 和

    public function index()
    

    在函数 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');
    

    也许它对某人有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-29
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 2017-05-10
      • 2012-12-12
      • 2021-06-05
      相关资源
      最近更新 更多