我正在我的Laravel应用程序中使用aacotroneo/laravel- SAML 2包尝试实现针对Azure AD的SAML身份验证。我配置了IDP设置,microsoft登录页面正确显示。问题是我无法从Azure那里获取用户信息。我按照这个问题的答案中的说明Azure Active Directory SSO with Laravel修改了文件app/Providers/SAML2ServiceProvider.php中的代码,添加了以下几行:
public function boot()
{
Event::listen('Aacotroneo\Saml2\Events\Saml2LogoutEvent', function ($event) {
if (session_status() !== PHP_SESSION_ACTIVE) {
session_start();
}
unset($_SESSION["id"]);
session_destroy();
});
Event::listen('Aacotroneo\Saml2\Events\Saml2LoginEvent', function (Saml2LoginEvent $event) {
$messageId = $event->getSaml2Auth()->getLastMessageId();
// Add your own code preventing reuse of a $messageId to stop replay attacks
if (session_status() == PHP_SESSION_ACTIVE) {
session_start();
}
$user = $event->getSaml2User();
Log::info("COOKIE_SAML ACTIVATED");
$_COOKIE["COOKIE_SAML"] = 1;
$userData = [
'id' => $user->getUserId(),
'attributes' => $user->getAttributes(),
'assertion' => $user->getRawSamlAssertion()
];
Log::info(json_encode($userData));
$inputs = [
'sso_user_id' => self::getValue($user->getUserId()),
// 'username' => $user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name'),
'email' => self::getValue($user->getAttribute('http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name')),
'first_name' => self::getValue($user->getAttribute('http://schemas.microsoft.com/identity/claims/displayname')),
'last_name' => self::getValue($user->getAttribute('http://schemas.microsoft.com/identity/claims/displayname')),
];
$user = UserAdecco::where('sso_user_id', $inputs['sso_user_id'])->where('email', $inputs['email'])->first();
if (!$user) {
$res = UserAdecco::store($inputs);
if ($res['status'] == 'success') {
$user = $res['data'];
$_SESSION["id"] = $user->id;
// Auth::guard('web')->login($user);
} else {
Log::info('SAML USER Error ' . $res['messages']);
}
} else {
$_SESSION["id"] = $user->id;
// Auth::guard('web')->login($user);
}
});
}
但是,这段代码似乎根本没有被执行。结果是,当我登录时,什么都不会发生,用户也不会登录到Laravel应用程序中。我遗漏了什么?
发布于 2022-07-12 21:42:59
我发现了这个问题,服务提供者需要在config/app.php中注册才能工作。
https://stackoverflow.com/questions/72940919
复制相似问题