Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于Laravel令牌的用户身份验证?_Php_Laravel_Authentication_Authorization_Laravel 5.1 - Fatal编程技术网

Php 基于Laravel令牌的用户身份验证?

Php 基于Laravel令牌的用户身份验证?,php,laravel,authentication,authorization,laravel-5.1,Php,Laravel,Authentication,Authorization,Laravel 5.1,我已经开发了一个web应用程序,我正在使用它进行用户授权和用户身份验证 现在我想为同一个应用程序restapi,使用基于令牌的无状态用户授权 是否有任何方法可以直接实现这一点,只需对当前代码进行最小的修改,或者对移动应用程序使用相同的授权,但基于令牌,如果没有,那么实现这一点的最快方法是什么 我已经检查过了,但我不认为这对我的情况有用 提前感谢 您可以在Laravel应用程序上使用基于令牌的身份验证。请参阅有关如何使用它的说明。我将此答案添加到上述答案的基础上 首先,我在我的composer.j

我已经开发了一个web应用程序,我正在使用它进行用户授权和用户身份验证

现在我想为同一个应用程序restapi,使用基于令牌的无状态用户授权

是否有任何方法可以直接实现这一点,只需对当前代码进行最小的修改,或者对移动应用程序使用相同的授权,但基于令牌,如果没有,那么实现这一点的最快方法是什么

我已经检查过了,但我不认为这对我的情况有用


提前感谢

您可以在Laravel应用程序上使用基于令牌的身份验证。请参阅有关如何使用它的说明。

我将此答案添加到上述答案的基础上

首先,我在我的
composer.json
文件中添加了
“tymon/jwt-auth”:“0.5.*”
,并运行
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);
        }
    }
然后,我修改了登录名并添加了Rest请求检查和Rest请求返回RESTAPI令牌

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'));


}
rest的一切都在正常工作,没有做太多修改,甚至
Auth::user()
返回包含所有值的用户对象


致敬,很高兴知道。它是一个很棒的库,可以轻松地与现有的laravel应用程序集成。