【问题标题】:JWT authorization laravel / angularjs issueJWT 授权 laravel / angularjs 问题
【发布时间】:2016-01-03 21:39:30
【问题描述】:

所以我正在尝试创建一个基本的 JWT 令牌身份验证。

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

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

我正在使用 Laravel 5.1 / Jwt-auth 0.5.* / AngularJs 1.4.7 / Satellizer(最新)。

我登录并且令牌存储在我的本地存储中。然后我尝试调用 authentcate 的索引以“获取所有用户”,并且授权标头与 Bearer <token stored in local storage> 一起存在,但我得到“token_not_provided”的响应,几乎就像中间件没有在授权标头中查找令牌一样.这是我的一些代码。我正在关注https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps

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

【问题讨论】:

  • 我遇到了这个问题。你找到解决办法了吗?
  • 我已经研究了几个星期,但仍然没有解决方案。 48 次浏览,无解。嗯嗯
  • 我找到了解决方案,只是不记得是什么了。让我考虑一下。你使用 Apache 吗?
  • yessir....灯环境

标签: php angularjs authentication laravel-5.1


【解决方案1】:

使用 Apache 时,需要设置 .htaccess 不裁剪 Authorization 标头。

path/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]    

【讨论】:

    猜你喜欢
    • 2017-07-02
    • 1970-01-01
    • 2019-03-07
    • 1970-01-01
    • 2018-11-26
    • 1970-01-01
    • 2020-09-13
    • 2021-06-24
    • 2013-07-19
    相关资源
    最近更新 更多