jwt身份验证错误(laravel)-未提供令牌

nlejzf6q  于 2021-06-15  发布在  Mysql
关注(0)|答案(0)|浏览(248)

我已经和jwt战斗了好几天了!我检查了所有能找到的解决方案,我得到的令牌没有提供。它是tymon/jwt auth版本“dev develop”(发布时最新的非dev版本是1.0.0rc-3),我将它与laravel5.6一起使用。
在本地运行时,它运行良好,在上传到cloudways后,它开始说token\u not\u provided。我已尝试更改公共驱动器中的.htaccess文件,并在route目录中添加了另一个副本,以防万一:

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

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

# Redirect Trailing Slashes If Not A Folder...

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

# Handle Front Controller...

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

还有我的authcontroller:

<?php

namespace App\Http\Controllers;

use Auth;
use JWTAuth;
use App\User;
use App\Http\Requests\RegisterFormRequest;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    public function register(RegisterFormRequest $request)
    {
        $user = new User;
        $user->email = $request->email;
        $user->name = $request->name;
        $user->username = $request->username;
        $user->password = bcrypt($request->password);
        $user->save();

        return response([
            'status' => 'success',
            'data' => $user
        ], 200);
    }

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

        if ( ! $token = JWTAuth::attempt($credentials)) {
            return response([
                'status' => 'error',
                'error' => 'invalid.credentials',
                'msg' => 'Invalid Credentials.'
            ], 400);
        }

        return response([
            'status' => 'success'
        ])
        ->header('Authorization', $token);
    }

    public function user(Request $request)
    {
        $user = User::find(Auth::user()->id)->load('interest');

        return response([
            'status' => 'success',
            'data' => $user
        ]);
    }

    public function edit($id)
    {

        $post = Post::find($id);

        return view('admin.posts.edit', compact('post'));

    }

    public function refresh()
    {
        return response([
            'status' => 'success'
        ]);
    }

    public function logout()
    {
        JWTAuth::invalidate();

        return response([
            'status' => 'success',
            'msg' => 'Logged out Successfully.'
        ], 200);
    }
}

这是我的api.php:

<?php

use Illuminate\Http\Request;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|

* /

Route::post('conv/store', 'ConversationController@store');
Route::post('interest/store', 'InterestController@store');
Route::post('message/store', 'MessageController@store');
Route::post('message/{id}/conv', 'MessageController@show');
Route::get('conv/{id}', 'ConversationController@show');
Route::get('get/users', 'AutoCompleteController@getUsers');
Route::get('subscribed/{pageTitle}/{UserInt}', 
'ConversationController@isSubscribed');
Route::get('categories', 'CategoryController@show');
Route::get('{id}/recommended', 'CategoryController@recommended');
Route::get('interests', 'InterestController@getAll');
Route::post('subscribe/{id}/{int}/add', 'InterestController@subscribe');
Route::post('subscribe/{id}/{int}/remove', 
'InterestController@unsubscribe');
Route::get('interest/profile/{id}', 'InterestController@show');
Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('auth/register', 'AuthController@register');
Route::post('auth/login', 'AuthController@login');
Route::group(['middleware' => 'api'], function(){
Route::get('auth/user', 'AuthController@user');
Route::post('auth/logout', 'AuthController@logout');
});

Route::group(['middleware' => 'jwt.refresh'], function(){
    Route::get('auth/refresh', 'AuthController@refresh');
    Route::get('/{id}/profile', 'AuthController@refresh');
});

这是我的auth.php:

<?php

return [

/*
|--------------------------------------------------------------------------
| Authentication Defaults
|--------------------------------------------------------------------------
|
| This option controls the default authentication "guard" and password
| reset options for your application. You may change these defaults
| as required, but they're a perfect start for most applications.
|

* /

'defaults' => [
    'guard' => 'api',
    'passwords' => 'users',
],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|

* /

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users',
    ],
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|

* /

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|

* /

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
  ],
];

希望你能帮忙!!对不起,我遗漏了什么!如果你还需要我补充什么,请告诉我。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题