Php 如何将此代码用于laravel中的ntlm sso?
您需要配置一个用于验证NTLM网络上的请求的 在我的公司中,我们有一个带有字段“matricula”的Employee表,该字段的值与在windows上登录时使用的值相同 所以,我将App\Employee的App\User模型替换为App\Employee,并将“matricula”字段作为ID放在我的示例中,但您可以使用ID为的App\User 步骤1:配置路由中间件 文件app\Http\Kernel.php在数组$routeMiddleware值处添加Php 如何将此代码用于laravel中的ntlm sso?,php,laravel,Php,Laravel,您需要配置一个用于验证NTLM网络上的请求的 在我的公司中,我们有一个带有字段“matricula”的Employee表,该字段的值与在windows上登录时使用的值相同 所以,我将App\Employee的App\User模型替换为App\Employee,并将“matricula”字段作为ID放在我的示例中,但您可以使用ID为的App\User 步骤1:配置路由中间件 文件app\Http\Kernel.php在数组$routeMiddleware值处添加 <?php $headers
<?php
$headers = apache_request_headers();
if (!isset($headers['Authorization'])){
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: NTLM');
exit;
}
$auth = $headers['Authorization'];
if (substr($auth,0,5) == 'NTLM ') {
$msg = base64_decode(substr($auth, 5));
if (substr($msg, 0, 8) != "NTLMSSP\x00")
die('error header not recognised');
if ($msg[8] == "\x01") {
$msg2 = "NTLMSSP\x00\x02\x00\x00\x00".
"\x00\x00\x00\x00". // target name len/alloc
"\x00\x00\x00\x00". // target name offset
"\x01\x02\x81\x00". // flags
"\x00\x00\x00\x00\x00\x00\x00\x00". // challenge
"\x00\x00\x00\x00\x00\x00\x00\x00". // context
"\x00\x00\x00\x00\x00\x00\x00\x00"; // target info len/alloc/offset
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: NTLM '.trim(base64_encode($msg2)));
exit;
}
else if ($msg[8] == "\x03") {
function get_msg_str($msg, $start, $unicode = true) {
$len = (ord($msg[$start+1]) * 256) + ord($msg[$start]);
$off = (ord($msg[$start+5]) * 256) + ord($msg[$start+4]);
if ($unicode)
return str_replace("\0", '', substr($msg, $off, $len));
else
return substr($msg, $off, $len);
}
$user = get_msg_str($msg, 36);
$domain = get_msg_str($msg, 28);
$workstation = get_msg_str($msg, 44);
// print "You are $user from $domain/$workstation";
print "$user";
}
}
步骤2:创建一个中间件文件
在“app\Http\Middleware\NTLMAuth”处创建中间件文件。(拉威尔5.7)
配置后,您可以在控制器上使用
$employee = \App\Model\Employee::where('matricula', $user)->first();
Auth::loginUsingId( $employee->matricula );
//app\Http\Middleware\NTLMAuth.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class NTLMAuth
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$auth = $request->header("Authorization");
$user = ' ';
if ($auth == null || strlen($auth) < 4 ){
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: NTLM');
exit;
}
if (substr($auth,0,5) == 'NTLM ') {
$msg = base64_decode(substr($auth, 5));
if (substr($msg, 0, 8) != "NTLMSSP\x00"){
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: NTLM');
exit;
}
if ($msg[8] == "\x01") {
$msg2 = "NTLMSSP\x00\x02\x00\x00\x00".
"\x00\x00\x00\x00". // target name len/alloc
"\x00\x00\x00\x00". // target name offset
"\x01\x02\x81\x00". // flags
"\x00\x00\x00\x00\x00\x00\x00\x00". // challenge
"\x00\x00\x00\x00\x00\x00\x00\x00". // context
"\x00\x00\x00\x00\x00\x00\x00\x00"; // target info len/alloc/offset
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: NTLM '.trim(base64_encode($msg2)));
exit;
}else if ($msg[8] == "\x03") {
function get_msg_str($msg, $start, $unicode = true) {
$len = (ord($msg[$start+1]) * 256) + ord($msg[$start]);
$off = (ord($msg[$start+5]) * 256) + ord($msg[$start+4]);
if ($unicode)
return str_replace("\0", '', substr($msg, $off, $len));
else
return substr($msg, $off, $len);
}
$user = get_msg_str($msg, 36);
$domain = get_msg_str($msg, 28);
$workstation = get_msg_str($msg, 44);
}
$employee = \App\Model\Employee::where('matricula', $user)->first();
if( $employee != null ){
Auth::loginUsingId( $employee->matricula );
}
$user = ( Auth::check() )? Auth::user()->nome : 'Not Found';
$request->attributes->set('user', $user);
return $next($request);
}
}
}
$employee = \App\Model\Employee::where('matricula', $user)->first();
Auth::loginUsingId( $employee->matricula );
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Employee;
use Auth;
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('ntlm');
}
public function index()
{
return view('home');
}
public function welcome()
{
$employees = Employee::take(5)->get();
return view('welcome', compact('employees') );
}
}