记录一次 Authorization 异常的插曲:
API.PHP 有如下配置:
Route::middleware('auth:api')->post('/userfollowOrUnFollowCelebrity', '[email protected]');
FollowController 构造函数及对应方法如下:
- public function __construct()
- {
- $this->middleware('auth')->except(['getFollowersData', 'followOrUnFollowCelebrity', 'getUserFollowedUsers']);
- $this->middleware('auth:api')->only(['getFollowersData', 'followOrUnFollowCelebrity', 'getUserFollowedUsers']);
- }
- public function followOrUnFollowCelebrity(Request $request)
- {
- $celebrity_id = $request->get('celebrity');
- if (is_null($celebrity_id)) {
- return response()->JSON(json_encode(['no available celebrity id']));
- }
- $celebrity = User::query()->where('id', $celebrity_id)->firstOrFail();
- if (Auth::guard('api')->check()) {
- } else {
- }
auth.PHP 中 guards 如下:
- 'guards' => [
- 'web' => [
- 'driver' => 'session',
- 'provider' => 'users',
- ],
- 'api' => [
- 'driver' => 'token',
- 'provider' => 'users',
- 'hash' => true,
- ],
- ],
数据库中用户已经有 api_token 但是死活验证不成功一直报 message: "Unauthenticated."
查了资料看到:
https://laravel.com/docs/5.8/api-authentication
后 发现 原来用户表里我设置的 api_token 时 直接设置 忘记了 hash 存入;
那么
'hash'=>false
就可以了.
更安全的参照下面:
- In order to make the login function working again, we have to run the hash function once on all the users. This can be done quickly by running the following command in PHP artisan tinker:
- use App\User;
- User::all()->each(function ($user) {
- $user->update(['api_token' => hash('sha256', $user->api_token)]);
- });
有时候不起左右:
执行:
- PHP artisan config:clear
- From now on the user's API token is safely stored away in the database.
注意如果使用 hash 的 api_token, 那么就不要在 RegisterController 中生成 API token, 而应该在登录或者刷新页面的同时, 请求生成一个新的 API Token, 生成后, 网站数据库中存储一个副本然后返回 token 文本值给前端视图, 阅后即焚, 在该页面的 post 或者 Ajax 请求的时候上传比对, 下次页面刷新或者登录的时候, 又下一次生成一次性的 API Token 存入数据库并返回一个 JSON 给前端.
比如 ApiTokenController:
- <?PHP
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- class ApiTokenController extends Controller
- {
- //
- public function update(Request $request)
- {
- $token = str_random(80);
- auth()->user()->forceFill([
- 'api_token' => hash('sha256', $token),
- ])->save();
- return ['token' => $token];
- }
- }
注意这个也是生成一个 hash 结果然后存入数据库, 前端收到的也是这个, 然后如果 API 中
'api' => [ 'driver' => 'token', 'provider' => 'users', 'hash' => true, ], 还是'hash'=>true 那么又成了数据库中的值比对前端传来的值再 hash 一次的比对, 又会 false, 这个逻辑的具体实现, 这里暂时不做, 因为更好的是使用 passport 以及 jwt.
来源: http://www.bubuko.com/infodetail-3492592.html