找到你要的答案

Q:Authenticating users in Laravel via Windows Authentication

Q:用户认证通过Windows身份验证laravel

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

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

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.

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?

我有PHP在IIS 7.5上运行一个Windows域。

Windows身份验证是通过IIS,所以用户域\用户名可通过_server美元[ 'auth_user ] PHP。

我刚刚安装了5的一个新的内部laravel网站,并需要一种方法来验证用户在使用单点登录laravel型方式。

最终用户应经在没有签署任何laravel,因为他们已经验证的领域。

我四处搜寻,几乎找不到任何相关的东西!

有什么关于这个的吗?当然,它是一个相当普遍的用例?我应该从哪里开始阅读,或者是否有人可以直接帮助?

answer1: 回答1:

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

}

Now add the middleware to your routes.

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.

假设美元_server [ 'auth_user ]的设置,你可以使用像在你的应用的HTTP /中间件以下认证:

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

}

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

然而,这确实假定您的用户已经存在。如果用户不存在,您可以更改重定向以创建用户,如果您需要。

php  authentication  laravel  iis  laravel-5