重定向后Laravel会话丢失

时间:2018-01-11 17:40:16

标签: single-sign-on saml-2.0 laravel-5.5

我正在实施一个使用SAML2和Okta进行SSO的解决方案。在我从IDP收到请求后,我对用户进行身份验证并将用户信息保存到会话中。不知何故,我失去了会话信息。我调用重定向('/ home')后的身份验证用户信息。有什么想法吗?如果需要,我也可以粘贴代码。我很感激帮助。

以下是代码段:     的 Kernel.php:

protected $middlewareGroups = [
   'web' => [
       \App\Http\Middleware\EncryptCookies::class,
       \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
       \Illuminate\Session\Middleware\StartSession::class,
       // \Illuminate\Session\Middleware\AuthenticateSession::class,
       \Illuminate\View\Middleware\ShareErrorsFromSession::class,
       \App\Http\Middleware\VerifyCsrfToken::class,
       \Illuminate\Routing\Middleware\SubstituteBindings::class,
   ],

   'api' => [
       'throttle:60,1',
       'bindings',
   ], ];

protected $routeMiddleware = [
   'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
   'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
   'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
   'can' => \Illuminate\Auth\Middleware\Authorize::class,
   'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
   'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
   'saml2' => \Singh\SimpleSaml\Middleware\Saml2Middleware::class,];

这来自 web.php:

Route::get('/home', 'HomeController@index')->name('home');

以下是来自 SamlController:

的代码
public function acs(Request $request) {
   $errors = $this->saml2Auth->acs();
   if (!empty($errors)) {
       logger()->error('Saml2 error_detail', ['error' => $this->saml2Auth->getLastErrorReason()]);
       session()->flash('saml2_error_detail', [$this->saml2Auth->getLastErrorReason()]);
       logger()->error('Saml2 error', $errors);
       session()->flash('saml2_error', $errors);
       return redirect(config('saml2_settings.errorRoute'));
   }
   $user = $this->saml2Auth->getSaml2User();
   event(new Saml2LoginEvent($user, $this->saml2Auth));

   $redirectUrl = $user->getIntendedUrl();

   if ($redirectUrl !== null) {
       return redirect($redirectUrl);
   } else {
       return redirect(config('saml2_settings.loginRoute'));
   } }

这是来自收听者:

的代码
public function handle(Saml2LoginEvent $event)
{
   if (!$event->getSaml2Auth()->isAuthenticated()) {
       Log::info('The user is not authenticated');
       return redirect(config('saml2_settings.logoutRoute'));
   }

   $samlUser = $event->getSaml2User();

   $attributes = $samlUser->getAttributes();

   //check if email already exists and fetch user
   $user = \App\User::where('email', $attributes['email'][0])->first();

   //if email doesn't exist, create new user
    if ($user === null)
    {        
        $user = new \App\User;
       $user->email = $attributes['email'][0];
       $user->shortname = $attributes['shortname'][0];
       $user->firstname = $attributes['firstname'][0];
       $user->lastname = $attributes['lastname'][0];
        $user->save();
    }

   if (count($attributes) >= 4) {
       session()->put('email', $attributes['email'][0]);
       session()->put('shortname', $attributes['shortname'][0]);
       session()->put('firstname', $attributes['firstname'][0]);
       session()->put('lastname', $attributes['lastname'][0]);
   }

   session()->save();

   Auth::login($user, true);  }

0 个答案:

没有答案