通过Windows身份验证在Laravel中对用户进行身份验证 [英] Authenticating users in Laravel via Windows Authentication

查看:129
本文介绍了通过Windows身份验证在Laravel中对用户进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的PHP在IIS 7.5的Windows域中运行.

I have PHP running in a Windows domain on IIS 7.5.

Windows身份验证是通过IIS打开的,因此用户DOMAIN \ USERNAME可通过PHP中的$_SERVER['AUTH_USER']访问.

Windows authentication is turned on via IIS, so the users DOMAIN\USERNAME is available via $_SERVER['AUTH_USER'] in PHP.

我刚刚为新的内部网站安装了Laravel 5,并且需要一种使用单点登录类型的方式在Laravel中对用户进行身份验证的方法.

I've just installed Laravel 5 for a new internal website, and need a way to authenticate users in Laravel using a single sign-on type of way.

最终用户应该在Laravel中进行身份验证,而不必在任何地方登录,因为他们已经通过域进行了身份验证.

The end users should be authenticated in Laravel without having to sign in anywhere, as they are already authenticated to the domain..

我已经搜索了,实际上找不到任何相关内容!

I've searched around, and literally can't find anything relevant!

关于这件事有什么记载吗?当然是一个很普通的用例吗?在阅读方面,或者如果有人可以直接提供帮助,我应该从哪里开始?

Is there anything written about this? Surely its quite a common use-case? Where should I start in terms of reading, or if anyone can help directly?

推荐答案

假设已设置$_SERVER['AUTH_USER'],则可以在App\Http\Middleware\Authenticate中使用类似以下内容的内容:

Assuming $_SERVER['AUTH_USER'] is set, you can use something like the following in your App\Http\Middleware\Authenticate:

<?php namespace App\Http\Middleware;

use Closure;
use App\User;
use Illuminate\Contracts\Auth\Guard;

class Authenticate {

    /**
     * The Guard implementation.
     *
     * @var Guard
     */
    protected $auth;

    /**
     * Create a new filter instance.
     *
     * @param  Guard  $auth
     * @return void
     */
    public function __construct(Guard $auth)
    {
        $this->auth = $auth;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->server('AUTH_USER') && ($user = User::where('username', $request->server('AUTH_USER'))->first()))
        {
            $this->auth->login($user);
        }
        else
        {
            return redirect()->guest('auth/login');
        }

        return $next($request);
    }

}

现在将中间件添加到您的路由.

但是,这确实假定您的用户已经存在.您可以更改重定向以动态创建用户(如果该用户不存在)(如果需要).

This however does assume your user already exists. You can change the redirect to create a user on the fly if the user does not exist, if you want that.

这篇关于通过Windows身份验证在Laravel中对用户进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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