Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.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 JWT授权laravel/angularjs问题_Php_Angularjs_Authentication_Laravel 5.1 - Fatal编程技术网

Php JWT授权laravel/angularjs问题

Php JWT授权laravel/angularjs问题,php,angularjs,authentication,laravel-5.1,Php,Angularjs,Authentication,Laravel 5.1,因此,我试图创建一个基本的JWT令牌身份验证 我使用Laravel作为我的后端API,使用tymon/jwt auth创建令牌/刷新令牌,并提供中间件来检查令牌 我正在使用AngularJS和Satellizer将返回的令牌存储在本地存储器中,并为所有未来的请求发送所述令牌 我使用的是Laravel 5.1/Jwt auth 0.5.*/AngularJs 1.4.7/Satellizer(最新版本) 我登录并将令牌存储在本地存储器中。然后,我尝试调用authentcate的索引来“获取所有用户

因此,我试图创建一个基本的JWT令牌身份验证

我使用Laravel作为我的后端API,使用tymon/jwt auth创建令牌/刷新令牌,并提供中间件来检查令牌

我正在使用AngularJS和Satellizer将返回的令牌存储在本地存储器中,并为所有未来的请求发送所述令牌

我使用的是Laravel 5.1/Jwt auth 0.5.*/AngularJs 1.4.7/Satellizer(最新版本)

我登录并将令牌存储在本地存储器中。然后,我尝试调用authentcate的索引来“获取所有用户”,授权头与
Bearer
一起存在,但我得到了“token\u not\u provided”的响应,就好像中间件没有在授权头中查找令牌一样。这是我的一些代码。我跟在后面

AuthenticateController.php:

<?php namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;
use App\User;

class AuthenticateController extends Controller
{

    public function __construct()
    {
        // Apply the jwt.auth middleware to all methods in this controller
        // except for the authenticate method. We don't want to prevent
        // the user from retrieving their token if they don't already have it
        $this->middleware('jwt.auth', ['except' => ['authenticate']]);
    }

    public function index()
    {
        // Retrieve all the users in the database and return them
        $users = User::all();
        return $users;
    }

    public function authenticate(Request $request)
    {
        $credentials = $request->only('email', 'password');

        try {
            // 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
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // if no errors are encountered we can return a JWT
        return response()->json(compact('token'));
    }
}

使用Apache时,需要将.htaccess设置为不裁剪授权头

路径/to/your/project/public/.htaccess打开.htaccess,并将其添加到

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
我的是这样的:

<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

RewriteEngine On        

RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]    

选项-多视图
重新启动发动机
RewriteCond%{HTTP:Authorization}^(.*)
重写规则。*-[e=HTTP\U授权:%1]
#如果不是文件夹,则重定向尾部斜杠。。。
重写cond%{REQUEST_FILENAME}-D
重写规则^(.*)/$/$1[L,R=301]
#处理前控制器。。。
重写cond%{REQUEST_FILENAME}-D
重写cond%{REQUEST_FILENAME}-F
重写规则^index.php[L]

我受这个问题的困扰。你找到解决办法了吗?我已经研究了好几个星期了,仍然没有找到解决办法。48个观点,没有解决方案。我找到了一个解决办法,我只是不记得是什么了。让我想想。你使用Apache吗?是的…lamp环境