loginUsingId 的 Laravel 问题(手动身份验证)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39360314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel issue with loginUsingId (Manual Authentication)
提问by omer Farooq
I am trying to implement a single signon on multiple domains. The concept is pretty simple i.e to send unique user tokens and then verify these tokens to find the user and then log him in.
我正在尝试在多个域上实现单点登录。这个概念非常简单,即发送唯一的用户令牌,然后验证这些令牌以找到用户,然后让他登录。
Now after verifying the token and then grabbing the user, i do something like this
现在在验证令牌然后抓取用户之后,我做这样的事情
$loggedInUser = Auth::loginUsingId($user->id, true);
Now i have a custom middleware where it first checks for a logged in user, i.e
现在我有一个自定义中间件,它首先检查登录用户,即
Auth::Check()
The above works fine for the first time. But on refresh Auth::check() is not validated. I have also tried using all different session drivers but still doesn't work.
以上第一次工作正常。但刷新时 Auth::check() 未验证。我也尝试过使用所有不同的会话驱动程序,但仍然无法正常工作。
I used a similar code on laravel 5.2, and it did work. But on laravel 5.3 its not validating on persistent requests.
我在 laravel 5.2 上使用了类似的代码,它确实有效。但是在 laravel 5.3 上,它不会对持久请求进行验证。
Edit: Let me show you my CodeI have not modified AuthServiceProvider or any other guard. I do have the user model inside a directory but i have modified the path in auth.php.
编辑:让我向您展示我的代码我没有修改 AuthServiceProvider 或任何其他守卫。我确实在目录中有用户模型,但我修改了 auth.php 中的路径。
Here is the route that domain1 points to:
以下是 domain1 指向的路由:
http://domain2.com/{{$role}}/{{$route}}/singlesignon/{{$token}}
http://domain2.com/{{$role}}/{{$route}}/singlesignon/{{$token}}
This is then picked up by verifySingleSignOn method inside the loginController which takes in the role, route that the user came in from other domain and the token. The user is then redirected to the same routes, but on domain2. Here i can successfully recieve the user id before manually logging in.
然后由 loginController 中的 verifySingleSignOn 方法获取,该方法承担角色、用户从其他域进入的路由和令牌。然后用户被重定向到相同的路由,但在域 2 上。在这里,我可以在手动登录之前成功收到用户 ID。
public function verifySingleSignOn($role, $route, $token)
{
// Fetch Single Signon
$userRepository = new UserRepository();
$user = $userRepository->checkForSingleSignOnToken($token, ['id']);
// Check if Token Exists
if (isset($user->id) && is_int($user->id) && $user->id != 0) {
// Manually Logging a user (Here is successfully recieve the user id)
$loggedInUser = Auth::loginUsingId($user->id);
if (!$loggedInUser) {
// If User not logged in, then Throw exception
throw new Exception('Single SignOn: User Cannot be Signed In');
}
$redirectTo = $role . '/' . $route;
return redirect($redirectTo);
} else {
return Auth::logout();
}
}
Then i have this GlobalAdminAuth middleware
然后我有这个 GlobalAdminAuth 中间件
// Check if logged in
if( Auth::Check() ){
$user = Auth::User();
// Check if user is active and is a globaladmin
if( !$user->isGlobalAdmin() || !$user->isActive() ){
return redirect()->guest('login');
}
}else{
return redirect()->guest('login');
}
return $next($request);
Now the first time everything works fine and the user moves through the middleware successfully . but the second time the else statement is triggered.
现在第一次一切正常,用户成功地通过中间件。但是第二次触发 else 语句。
Edit: Code for checkForSingleSignOnToken
编辑: checkForSingleSignOnToken 的代码
public function checkForSingleSignOnToken($token, $columns = array('*'))
{
return User::where('single_signon', $token)->first($columns);
}
回答by Pankaj Kachhwaye
try
尝试
Auth::login($user);
Auth::login($user);
instead of
代替
Auth::loginUsingId($user->id, true);
回答by Curos
Cookies are restricted domain-wise. Your application on domain1.com
wont be able to grab cookies set by domain2.com
.
Cookie 是受域限制的。您的应用程序domain1.com
将无法获取由domain2.com
.
You should be customizing the guard to use some other mechanism than cookies. Maybe use a token in the query parameters.
您应该自定义守卫以使用 cookie 以外的其他机制。也许在查询参数中使用令牌。
回答by Ghost Worker
add this to your protected $middleware
array in app\Http\Kernel.php
将此添加到您的protected $middleware
数组中app\Http\Kernel.php
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class
I think it has to do with an update in the framework
我认为这与框架中的更新有关
回答by Tran Hoang Hiep
no using auth:check in middleware
不使用身份验证:签入中间件
using request->user() or auth::user()
使用 request->user() 或 auth::user()
回答by Hirendrasinh S. Rathod
Please review bellow code structure, i had made manual authentication in laravel 5.0.
请查看以下代码结构,我在 laravel 5.0 中进行了手动身份验证。
routes.php
路由文件
Route::get('login_user_by_id/{id?}', ['as' => 'login_user_by_id', 'uses' => 'UsersController@login_user_by_id']);
Route::post('user_login_post_for_admin',['as'=>'user_login_post_for_admin','uses'=>'LoginController@user_login_post_for_admin']);
Route::get('user_logout', ['as' => 'user_logout', 'uses' => 'UsersController@user_logout']);
LoginController.php
登录控制器.php
public function user_login_post_for_admin(){
$this->set_email($_POST['email']);
$this->set_password($_POST['password']);
$this->set_login_requested_role(['Admin','Moderator']);
return $this->user_login_post();
}
public function user_login_post(){
$User = new User();
if(isset($this->email) && !empty($this->email)){
$User->set_email(trim($this->email));
$User->set_password(Hash::make(trim($this->password)));
$user_login_data = $User->check_email_password_for_login();
if(isset($user_login_data) && !empty($user_login_data)){
if (Hash::check(trim($this->password), $user_login_data[0]->password)) {
$response['user_id']=$user_login_data[0]->id;
$response['name']=$user_login_data[0]->name;
$response['surname']=$user_login_data[0]->surname;
$response['profile_picture']=$user_login_data[0]->profile_picture;
$response['SUCCESS']='True';
$response['MESSAGE']='Login Success.';
return Redirect::route('login_user_by_id',[$user_login_data[0]->id]);
}else{
Session::put('SUCCESS','FALSE');
Session::put('MESSAGE', 'Invalid Credential.');
return redirect()->back();
}
}else{
Session::put('SUCCESS','FALSE');
Session::put('MESSAGE', 'Invalid Credential.');
return redirect()->back();
}
}else{
Session::put('SUCCESS','FALSE');
Session::put('MESSAGE', 'Invalid Credential.');
return redirect()->back();
}
}
UsersController.php
用户控制器.php
public function login_user_by_id($id=''){
if(isset($_GET['id'])&&!empty($_GET['id'])){
$id = $_GET['id'];
}
$User = new User();
$Log=new Log();
$user_for_auth = $User->find($id);
Auth::login($user_for_auth, true);
$User->id=AUTH::user()->id;
$auth_user_role=$User->auth_user_role();
$rl_title=$auth_user_role[0]->rl_title;
return Redirect::route('admin_home');
}
public function user_logout(User $user){
$User=new User();
$login_user_id = AUTH::user()->id;
$User->id=AUTH::user()->id;
$auth_user_role=$User->auth_user_role();
$login_user_role=$auth_user_role[0]->rl_title;
$response['user_id']=$login_user_id;
$response['SUCCESS']='TRUE';
$response['MESSAGE']='Successfully Logout.';
Auth::logout();
return Redirect::route('admin_login');
}