Laravel 基于令牌的用户身份验证?
Laravel token based User Authentication?
我开发了一个 Web 应用程序,使用 Laravel 5.1 and I am using Laravel Authentication 进行用户授权和用户身份验证。
现在我想为具有基于令牌的无状态用户授权的同一个应用程序休息API。
有没有什么方法可以在对当前代码进行最少修改的情况下直接执行此操作,或者对移动应用程序使用相同的授权但基于令牌,如果没有,那么实现此操作的最快方法是什么。
我已经检查过oauth2-server-laravel但我不认为它对我有用。
提前致谢!!!
您可以使用JWT-Auth for token based authentication on Laravel applications. Refer to the documentation了解如何使用它。
我添加这个答案只是对上面答案的补充
首先,我已将 "tymon/jwt-auth": "0.5.*"
添加到我的 composer.json
文件中,并添加了 运行 composer update
用于提取 JWT-Auth .
然后,我在我的身份验证中间件中添加了对 Rest 请求的检查,并在身份验证中间件中为请求添加了 JWT 令牌验证
public function handle($request, Closure $next)
{
if($request->has('token')){
$this->auth = JWTAuth::parseToken()->authenticate();
return $next($request);
}
else{
if ($this->auth->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('auth/login');
}
}
return $next($request);
}
}
然后我修改了登录并添加了休息请求检查和返回休息请求的休息API令牌
if(isRestRequest){
// grab credentials from the request
$credentials = Input::only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return Response::json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return Response::json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return Response::json(compact('token'));
}
其余的一切都在没有太多修改的情况下正常工作,甚至 Auth::user()
returns 具有所有值的用户对象。
点赞
我开发了一个 Web 应用程序,使用 Laravel 5.1 and I am using Laravel Authentication 进行用户授权和用户身份验证。
现在我想为具有基于令牌的无状态用户授权的同一个应用程序休息API。
有没有什么方法可以在对当前代码进行最少修改的情况下直接执行此操作,或者对移动应用程序使用相同的授权但基于令牌,如果没有,那么实现此操作的最快方法是什么。
我已经检查过oauth2-server-laravel但我不认为它对我有用。
提前致谢!!!
您可以使用JWT-Auth for token based authentication on Laravel applications. Refer to the documentation了解如何使用它。
我添加这个答案只是对上面答案的补充
首先,我已将 "tymon/jwt-auth": "0.5.*"
添加到我的 composer.json
文件中,并添加了 运行 composer update
用于提取 JWT-Auth .
然后,我在我的身份验证中间件中添加了对 Rest 请求的检查,并在身份验证中间件中为请求添加了 JWT 令牌验证
public function handle($request, Closure $next)
{
if($request->has('token')){
$this->auth = JWTAuth::parseToken()->authenticate();
return $next($request);
}
else{
if ($this->auth->guest()) {
if ($request->ajax()) {
return response('Unauthorized.', 401);
} else {
return redirect()->guest('auth/login');
}
}
return $next($request);
}
}
然后我修改了登录并添加了休息请求检查和返回休息请求的休息API令牌
if(isRestRequest){
// grab credentials from the request
$credentials = Input::only('email', 'password');
try {
// attempt to verify the credentials and create a token for the user
if (! $token = JWTAuth::attempt($credentials)) {
return Response::json(['error' => 'invalid_credentials'], 401);
}
} catch (JWTException $e) {
// something went wrong whilst attempting to encode the token
return Response::json(['error' => 'could_not_create_token'], 500);
}
// all good so return the token
return Response::json(compact('token'));
}
其余的一切都在没有太多修改的情况下正常工作,甚至 Auth::user()
returns 具有所有值的用户对象。