Zak*_*ziz 5 authentication rest oauth-2.0 angularjs laravel-5
我正在使用Laravel和Angular编写一个Web应用程序.
在前端,Laravel用于创建基本模板,但由Angular控制.在后端laravel用于创建一个宁静的API.
我有几条这样的路线:
Route::group(['domain' => 'domain.com'], function() {
Route::get('/', ['as' => 'home', function () {
return view('homepage');
}]);
Route::get('/login', ['as' => 'login', function () {
return view('login');
}]);
//users should be authenticated before accessing this page
Route::get('/dashboard', ['as' => 'dashboard', function () {
return view('dashboard');
}]);
});
Route::group(['domain' => 'api.domain.com', 'middleware' => ['oauth']], function() {
Route::post('/post/create', ['uses' => 'PostController@store']);
Route::get('/post/{id}', ['uses' => 'PostController@show']);
//other API endpoints
// ...
});
Run Code Online (Sandbox Code Playgroud)
我想确保我的domain.com/dashboardURL只能由经过身份验证的用户访问.
在我的后端,我为我的API路由实现了OAuth,这确保访问这些路由的用户是真实的.Auth::once()OAuth库使用Laravel 来确保用户凭据正确然后生成access_token.由于Auth::once()是"无状态"功能,因此不会使用会话或cookie,我无法Auth::check()确保在呈现仪表板页面之前对用户进行身份验证.
我应该如何检查用户是否尝试访问domain.com/dashboard是否经过身份验证?我应该发access_token在头部,当我从转发用户/login来/dashboard?或者我应该实现Laravel基于会话/ cookie的身份验证?
编辑:根据这个:在Angular应用程序中将http标头添加到window.location.href我无法将用户转发到带有Authorization标题的仪表板页面.
为了重用我的移动应用程序的API,我强烈希望使用某种基于令牌的身份验证.
@Pierre Cordier @Mr_Antivius 谢谢你们的回答,它帮助我深入了解了问题,并让我能够修改 JWT,但最终没有为我提供解决方案。
为了仅允许经过身份验证的用户访问,domain.com/dashboard我必须实施混合会话和 OAuth 身份验证系统。我决定使用Sentinel(而不是 Laravel 开箱即用的身份验证系统),因为它有一个我在应用程序的其他地方需要的用户权限系统。我将此库用于 OAuth 服务器。
这是我在控制器中执行的操作:
POST domain.com/auth/authenticate:
public function processLogin(Request $request)
{
$credentials = [
'email' => $request->input('username'),
'password' => $request->input('password'),
];
try
{
$sentinel = Sentinel::authenticate($credentials);
}
catch (\Cartalyst\Sentinel\Checkpoints\ThrottlingException $e)
{
$response = ['error' => [$e->getMessage()]];
$httpStatus = 429;
return response()->json($response, $httpStatus);
}
catch (\Cartalyst\Sentinel\Checkpoints\NotActivatedException $e)
{
$response = ['error' => [$e->getMessage()]];
$httpStatus = 401;
return response()->json($response, $httpStatus);
}
if ($sentinel) //user credentials correct
{
//get oauth token
$oauthToken = Authorizer::issueAccessToken();
$response = ['success' => true, 'user' => ['id' => $sentinel->id, 'email' => $sentinel->email]] + $oauthToken;
$httpStatus = 200;
}
else
{
$response = ['success' => false, 'error' => ['Incorrect credentials']];
$httpStatus = 401;
}
return response()->json($response, $httpStatus);
}
Run Code Online (Sandbox Code Playgroud)
以下是 OAuth 库用于验证用户身份的方法:
public function verifyAuth($email, $password)
{
$credentials = [
'email' => $email,
'password' => $password,
];
if ($user = Sentinel::stateless($credentials))
{
return $user->id;
}
else
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这将创建如下响应:
{
"success": true,
"user": {
"id": 1,
"email": "email@domain.tld"
},
"access_token": "6a204bd89f3c8348afd5c77c717a097a",
"token_type": "Bearer",
"expires_in": 28800,
"refresh_token": "092a8e1a7025f700af39e38a638e199b"
}
Run Code Online (Sandbox Code Playgroud)
希望这对那里的人有帮助
旁注:我正在POST向 发送请求domain.com/auth/authenticate,而不是api.domain.com/auth/authenticate因为如果我发布到 ,我无法domain.com/dashboard识别哨兵的 cookie api.domain.com。我已经尝试过改变domain,config/session.php但.domain.com仍然一无所获。也许我做错了什么?
| 归档时间: |
|
| 查看次数: |
1030 次 |
| 最近记录: |