如何通过护照为 AngularJS 和 Laravel 应用程序创建基于令牌的身份验证 (oauth2)
How to create Token-Based Authentication(oauth2) for AngularJS and Laravel Apps via passport
我创建了一个使用 laravel 默认注册 (auth) 的网络应用程序,我已经从 taylor 教程中测试了 passport oauth2 客户端访问令牌。我的网络应用程序使用 angular js 作为 UI 和 laravel 作为后端,所以我需要创建用户,当创建用户请求从 angular 发送时,然后创建全局访问令牌在我对 angular 的响应中给出,然后在所有以后的请求中我用它来验证请求。
实际上我想为我的网络应用程序实现 oauth2 身份验证,但到目前为止我已经搜索了很多但找不到任何有用的分步教程。
谁能帮帮我?
仅供参考:我使用 laravel 5.3 并启用了护照,angular js 1.5 用于前端。
在此处使用基于 JWT 令牌的身份验证您可以了解 jwt https://jwt.io/
我已经解决了这个问题。
我已经为登录和注册定制了 laravel 身份验证,并创建了一个方法,该方法将向服务器发送请求以创建用于注册用户或登录的访问令牌。
我已经设置了护照并像泰勒在他的旅行中所做的那样对其进行了测试。
然后在 AuthenticatesUsers.php
中,我更改了 sendloginResponse
方法响应,例如:
protected function sendLoginResponse(Request $request)
{
isset($request->token) ? $token = $request->token : $token = null;//Check if login request contain token
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
? $this->StatusCode($token,$this->credentials($request),$status=false) : $this->StatusCode($token,$this->credentials($request),$status=true);
}
并且我添加了此方法来请求访问令牌并将其作为 json 响应发送:
public function StatusCode($token,$user,$status){
if( $token != null && $token != ''){
return ($status == true) ? response()->json(GetToken($user),200) : response()->json(['Message' => 'Failed to log in'],403);
}
function GetToken($userobject)
{
$http = new Client;
$response = $http->post('http://localhost/iranAd/public/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => '1',
'client_secret' => 'xKqNbzcXyjySg20nVuVLw5nk5PAMhFQOQwRTeTjd',
'username' => $userobject['email'],
'password' => $userobject['password'],
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
}
function RefreshToken($token,$userobject)
{
$http = new Client;
$response = $http->post('http://localhost/iranAd/public/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => 'refresh_token',
'client_id' => '1',
'client_secret' => 'xKqNbzcXyjySg20nVuVLw5nk5PAMhFQOQwRTeTjd',
'username' => $userobject['email'],
'password' => $userobject['password'],
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
}
return ($status == true) ? response()->json(GetToken($user),200) : response()->json(['Message' => 'Failed to log in'],403);
}
注册用户的步骤相同。
这个 post 的目的不是回答(正如已经回答的那样),而是向最终需要有关主题的更多信息的其他读者提供更多信息。
关于这个问题的教程非常有用Implementing Vue.js 2.0 and Laravel 5.3 with CORS problem solution
检查这个和接下来的 2 个剪辑。
您可以在此处以更短的形式找到其中的一些内容 here
我创建了一个使用 laravel 默认注册 (auth) 的网络应用程序,我已经从 taylor 教程中测试了 passport oauth2 客户端访问令牌。我的网络应用程序使用 angular js 作为 UI 和 laravel 作为后端,所以我需要创建用户,当创建用户请求从 angular 发送时,然后创建全局访问令牌在我对 angular 的响应中给出,然后在所有以后的请求中我用它来验证请求。
实际上我想为我的网络应用程序实现 oauth2 身份验证,但到目前为止我已经搜索了很多但找不到任何有用的分步教程。
谁能帮帮我?
仅供参考:我使用 laravel 5.3 并启用了护照,angular js 1.5 用于前端。
在此处使用基于 JWT 令牌的身份验证您可以了解 jwt https://jwt.io/
我已经解决了这个问题。
我已经为登录和注册定制了 laravel 身份验证,并创建了一个方法,该方法将向服务器发送请求以创建用于注册用户或登录的访问令牌。
我已经设置了护照并像泰勒在他的旅行中所做的那样对其进行了测试。
然后在 AuthenticatesUsers.php
中,我更改了 sendloginResponse
方法响应,例如:
protected function sendLoginResponse(Request $request)
{
isset($request->token) ? $token = $request->token : $token = null;//Check if login request contain token
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return $this->authenticated($request, $this->guard()->user())
? $this->StatusCode($token,$this->credentials($request),$status=false) : $this->StatusCode($token,$this->credentials($request),$status=true);
}
并且我添加了此方法来请求访问令牌并将其作为 json 响应发送:
public function StatusCode($token,$user,$status){
if( $token != null && $token != ''){
return ($status == true) ? response()->json(GetToken($user),200) : response()->json(['Message' => 'Failed to log in'],403);
}
function GetToken($userobject)
{
$http = new Client;
$response = $http->post('http://localhost/iranAd/public/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => '1',
'client_secret' => 'xKqNbzcXyjySg20nVuVLw5nk5PAMhFQOQwRTeTjd',
'username' => $userobject['email'],
'password' => $userobject['password'],
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
}
function RefreshToken($token,$userobject)
{
$http = new Client;
$response = $http->post('http://localhost/iranAd/public/oauth/token', [
'form_params' => [
'grant_type' => 'refresh_token',
'refresh_token' => 'refresh_token',
'client_id' => '1',
'client_secret' => 'xKqNbzcXyjySg20nVuVLw5nk5PAMhFQOQwRTeTjd',
'username' => $userobject['email'],
'password' => $userobject['password'],
'scope' => '',
],
]);
return json_decode((string) $response->getBody(), true);
}
return ($status == true) ? response()->json(GetToken($user),200) : response()->json(['Message' => 'Failed to log in'],403);
}
注册用户的步骤相同。
这个 post 的目的不是回答(正如已经回答的那样),而是向最终需要有关主题的更多信息的其他读者提供更多信息。
关于这个问题的教程非常有用Implementing Vue.js 2.0 and Laravel 5.3 with CORS problem solution 检查这个和接下来的 2 个剪辑。
您可以在此处以更短的形式找到其中的一些内容 here