在很多需求我们不希望别人知道用户在我们表中的 user_id ;
但是又想用数据库的自增 id 功能;
一般时候在取出用户后加密 user_id 加密即可;
但是总有那么几个不经意间就可能把我们的 user_id 暴露了;
比如说 laravel 的 passport ;

创建一个项目用于测试;

laravel new passport
Bash

安装 passport;

composer require laravel/passport
php artisan migrate
php artisan passport:install
Bash

现在我们有了用于测试的 Clint;

 

将 Laravel\Passport\HasApiTokens Trait 添加到 App\User 模型中;

<?php

namespace App;

+ use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable
{
+ use HasApiTokens, Notifiable;
// ...
}
Diff

在 AuthServiceProvider 的 boot 方法中增加 Passport::routes() ;

<?php

namespace App\Providers;

+ use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
]; /**
* 注册任何认证/授权服务。
*
* @return void
*/
public function boot()
{
$this->registerPolicies(); + Passport::routes();
}
}
Diff

将 config/auth.php 中的 driver 改为 passport;

'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
], 'api' => [
- 'driver' => token',
+ 'driver' => 'passport',
'provider' => 'users',
],
],
Diff

创建测试用户

php artisan make:seeder UsersTableSeeder
Bash
<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\User::class, 5)->create();
}
}
PHP
php artisan db:seed --class=UsersTableSeeder
Bash

测试用户也有了;

 

获取下 access_token ;

 

我们拿着 access_token 去 jwt.io 解开看下;

 

可以看到 PAYLOAD 中的 sub 就是我们的未加密的用户id;
下面就是将 user_id 加密的过程了;

既然是加密id;
那还需要安装一个扩展包;
示例中我们使用laravel-hashids

composer require vinkla/hashids
php artisan vendor:publish --provider='Vinkla\Hashids\HashidsServiceProvider'
Bash

随便配置下;
/config/hashids.php

'main' => [
- 'salt' => 'your-salt-string',
- 'length' => 'your-length-integer',
+ 'salt' => 'alsd2987vnvczx&^$%Tpweqfhkjn',
+ 'length' => 20,
],
Diff

加密用户id;
这里主要用到了 laravel 留的一个钩子;
vendor/laravel/passport/src/Bridge/UserRepository.php

 

在 getUserEntityByUserCredentials 中会判断 User 模型是否有 findForPassport 方法;
我们可以在此处加密;
app/User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Passport\HasApiTokens;
use Hashids; class User extends Authenticatable
{
use HasApiTokens, Notifiable; /**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
]; /**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
]; /**
* 此处定义 getIdAttribute 是为了跳过模型把主键 id 转成数字的操作
*
* @see \Illuminate\Database\Eloquent\Concerns\HasAttributes::getAttributeValue()
*
* @param $value
* @return mixed
*/
public function getIdAttribute($value)
{
return $value;
} /**
* 当获取用户的时候加密 user_id
*
* 下面这些方法是为了加密和解密 jwt 中的 user_id
* @see \App\Extensions\Illuminate\Auth\ExtendedUserProvider::retrieveById()
* @see \App\Models\User::findForPassport()
* @see \App\Models\OauthAccessToken::setUserIdAttribute()
*
* @param string $email
*
* @return User
*/
public function findForPassport($email): self
{
$user = $this->where('email', $email)->first();
$user->id = Hashids::encode($user->id); return $user;
}
}
PHP

因为上面把 user_id 加密了;
而 oauth_access_tokens 表中的 user_id 是 int 类型;
所以我们需要在向 oauth_access_tokens 存储数据的时候自动解密 user_id ;

php artisan make:model OauthAccessToken
Bash
<?php

namespace App;

use Laravel\Passport\Token;
use Vinkla\Hashids\Facades\Hashids; class OauthAccessToken extends Token
{
/**
* 当向 oauth_access_tokens 表中存储数据的时候解密 user_id
*
* 下面这些方法是为了加密和解密 jwt 中的 user_id
* @see \App\Extensions\Illuminate\Auth\ExtendedUserProvider::retrieveById()
* @see \App\Models\User::findForPassport()
* @see \App\Models\OauthAccessToken::setUserIdAttribute()
*
* @param int|string $value
*/
public function setUserIdAttribute($value): void
{
if (is_numeric($value)) {
$this->attributes['user_id'] = $value;
} else {
$this->attributes['user_id'] = current(Hashids::decode($value));
}
}
}
PHP

覆盖 passport 的 OauthAccessToken ;
app/Providers/AuthServiceProvider.php

<?php

namespace App\Providers;

use App\OauthAccessToken;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport; class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
]; /**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies(); Passport::routes(); + Passport::useTokenModel(OauthAccessToken::class);
}
}
Diff

再获取下 access_token ;

 

 

如我们所愿;
user_id 已经被加密了;
接着还需要处理的是当我们拿着这个token去访问应用的时候能成功验证;
并且可以正确的获取到用户;
定义ExtendedUserProvider 用于覆盖 retrieveById 方法;
app/Extensions/Illuminate/Auth/ExtendedUserProvider.php

<?php

namespace App\Extensions\Illuminate\Auth;

use App\User;
use Illuminate\Auth\AuthenticationException;
use Illuminate\Auth\EloquentUserProvider;
use Vinkla\Hashids\Facades\Hashids; class ExtendedUserProvider extends EloquentUserProvider
{
/**
* 当获取用户的时候解密 user_id
*
* 下面这些方法是为了加密和解密 jwt 中的 user_id
* @see \App\Extensions\Illuminate\Auth\ExtendedUserProvider::retrieveById()
* @see \App\User::findForPassport()
* @see \App\OauthAccessToken::setUserIdAttribute()
*
* @param int|string $identifier
* @return User
* @throws AuthenticationException
*/
public function retrieveById($identifier)
{
$model = $this->createModel(); /**
* If Id is a string, then we need to decrypt $identifier.
*
* @see \App\Models\User::findForPassport()
*/
if (!is_numeric($identifier)) {
$identifier = current(Hashids::decode($identifier));
} return $model->newQuery()
->where($model->getAuthIdentifierName(), $identifier)
->first();
}
}
PHP

修改配置项把 auth.providers.users.driver 改成 
config/auth.php

'providers' => [
'users' => [
- 'driver' => 'extended',
+ 'model' => App\User::class,
]
],
Diff

app/Providers/AuthServiceProvider.php

<?php

namespace App\Providers;

use App\Extensions\Illuminate\Auth\ExtendedUserProvider;
use App\OauthAccessToken;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport;
use Illuminate\Foundation\Application;
use Auth; class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
]; /**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies(); Passport::routes();
Passport::useTokenModel(OauthAccessToken::class); /**
* Register @see \App\Extensions\Illuminate\Auth\ExtendedUserProvider
*/
Auth::provider('extended', function ($app, $config) {
$model = $config['model'];
return new ExtendedUserProvider($app['hash'], $model);
});
}
}
PHP

使用 jwt 请求 /api/user 正确的通过的验证

 

至此通过password模式的加密和解密已经完成了;
然鹅坑爹的是如果使用authorization code模式;
因为id是从 code 获取的;
而不是从模型中获取的;
因此我们需要在获取 code 的步骤中加密 user_id ;
先来覆盖原本的 AuthorizationController 中的approve方法;

php artisan make:controller Oauth/AuthorizationController
Bash
<?php

namespace App\Http\Controllers\Oauth;

use Illuminate\Http\Request;
use Laravel\Passport\Http\Controllers\ApproveAuthorizationController as Controller;
use Zend\Diactoros\Response as Psr7Response;
use Hashids; class ApproveAuthorizationController extends Controller
{
public function approve(Request $request)
{
return $this->withErrorHandling(function () use ($request) {
$authRequest = $this->getAuthRequestFromSession($request); /**
* Encrypt user_id
*
* @see \App\OauthAuthCode::setRawAttributes()
*/
$user = $authRequest->getUser();
$user->setIdentifier(Hashids::encode($user->getIdentifier())); return $this->convertResponse(
$this->server->completeAuthorizationRequest($authRequest, new Psr7Response)
);
});
}
}
PHP

覆盖路由;
routes/web.php

/*
|--------------------------------------------------------------------------
| oauth
|--------------------------------------------------------------------------
*/
Route::prefix('oauth')->namespace('Oauth')->group(function () {
Route::post('authorize', 'ApproveAuthorizationController@approve')->name('passport.authorizations.approve');
});
PHP

此处有一个坑爹的地方;
passport 在向 oauth_auth_code 表中存储数据的时候使用了 setRawAttributes ;

 

以至于我们不能使用 setUserIdAttribute 来解密;
因此需要覆盖 OauthAuthCode 模型的 setRawAttributes 方法用于解密;

php artisan make:model OauthAuthCode
Bash

app/OauthAuthCode.php

<?php

namespace App;

use Hashids;
use Laravel\Passport\AuthCode; class OauthAuthCode extends AuthCode
{
/**
* 因为 laravel passport 在 @see \Laravel\Passport\Bridge\AuthCodeRepository@persistNewAuthCode() 中使用的 setRawAttributes ; 而 setRawAttributes 不能触发 setUserIdAttribute ;所以不能使用 setUserIdAttribute 解密;需要 覆盖 setRawAttributes 方法;
*
* @param array $attributes
* @param bool $sync
*
* @return $this
*/
public function setRawAttributes($attributes, $sync = false)
{
/**
* Decrypt user_id
*
* Encrypt user_id in @see \App\Http\Controllers\Oauth\AuthorizationController::authorize()
*/
if (isset($attributes['user_id'])) {
$attributes['user_id'] = current(Hashids::decode($attributes['user_id']));
} return parent::setRawAttributes($attributes, $sync);
}
}
PHP

app/Providers/AuthServiceProvider.php

<?php

namespace App\Providers;

use App\Extensions\Illuminate\Auth\ExtendedUserProvider;
use App\OauthAccessToken;
+ use App\OauthAuthCode;
use Auth;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Laravel\Passport\Passport; class AuthServiceProvider extends ServiceProvider
{
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
]; /**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies(); Passport::routes();
Passport::useTokenModel(OauthAccessToken::class);
+ Passport::useAuthCodeModel(OauthAuthCode::class);
/**
* Register @see \App\Extensions\Illuminate\Auth\ExtendedUserProvider
*/
Auth::provider('extended', function ($app, $config) {
$model = $config['model'];
return new ExtendedUserProvider($app['hash'], $model);
});
}
}
Diff

authorization code模式需要网页登录;

php artisan make:auth
Bash

访问 /login 登录;

 

生成client;

php artisan passport:client
Bash

 

手动组一个获取 code 的 url 并访问;
http://passport.test/oauth/authorize?client_id=3&redirect_uri=http://passport.test/auth/callback&response_type=code&scope=

 

在回调地址 http://passport.test/auth/callback 页面地址栏获取 code ;
拿着 code 去换取 token ;

 

检查 user_id 是否被加密;

 

检测 token 是否可用;

 

一切按着剧本走的没有什么问题;
我把示例代码上传到 github 上了;
如果自己按文章测试的过程中出现问题;
可用参考 https://github.com/baijunyao/laravel-passport-encrypt-user-id-demo

laravel passport加密jwt格式的access_token中的sub(user_id)字段的更多相关文章

  1. Laravel Passport认证-多表、多字段解决方案

    Laravel Passport认证-多表.多字段解决方案 2018年08月19日 09:31:01 醉卧码场君莫笑 阅读数:1632   1. 概述 API 通常使用令牌(token)进行认证并且在 ...

  2. Laravel Passport API 认证使用小结

    Laravel Passport API 认证使用小结 八月 4, 2017 发布在 Laravel 看到Laravel-China 社区常有人问 Laravel Passport 用于密码验证方式来 ...

  3. laravel Passport - 创建 REST API 用户认证以及Dingo/Api v2.0+Passport实现api认证

    第一部分: 安装passport 使⽤ Composer 依赖包管理器安装 Passport : composer require laravel/passport 接下来,将 Passport 的服 ...

  4. Jwt在Java项目中的简单实际应用

    1.什么是jwt 双方之间传递安全信息的简洁的.URL安全的表述性声明规范.JWT作为一个开放的标准(RFC 7519),定义了一种简洁的,自包含的方法用于通信双方之间以Json对象的形式安全的传递信 ...

  5. laravel Passport - Dingo/Api v2.0+Passport 实现 api 认证

    第一部分: 安装passport 使⽤ Composer 依赖包管理器安装 Passport : composer require laravel/passport 接下来,将 Passport 的服 ...

  6. laravel passport client_credentials

    我是使用 Laravel 5.4 + Dingo Api + passport/jwt 两个验证方式 目前需要用到 passport 的 client_credentials 获取 token成功之后 ...

  7. APFS 宗卷 • APFS(加密)磁盘格式怎么去掉?Mac磁盘加密怎么解除?

    相信很多朋友都因为APFS 宗卷 • APFS(加密)磁盘格式而困扰,这种磁盘加密,导致很多破解版软件都不能安装.那么磁盘加密怎么解除?小编翻阅了一些教程,为您带来APFS 宗卷 • APFS(加密) ...

  8. (译)利用ASP.NET加密和解密Web.config中连接字符串

    介绍 这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串 背景描述 在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Server, A ...

  9. 利用ASP.NET加密和解密Web.config中连接字符串

    摘自:博客园 介绍 这篇文章我将介绍如何利用ASP.NET来加密和解密Web.config中连接字符串 背景描述 在以前的博客中,我写了许多关于介绍 Asp.net, Gridview, SQL Se ...

随机推荐

  1. python 常用库

    Numpy, Pandas 数据处理 Scipy  科学计算 Matplotlib 可视化 Scikit Learn  机器学习 Keras 深度学习模型 jieba 分词 Gensim  主题(包含 ...

  2. Django学习笔记之验证和授权

    验证和授权概述 Django有一个内置的授权系统.他用来处理用户.分组.权限以及基于cookie的会话系统.Django的授权系统包括验证和授权两个部分.验证是验证这个用户是否是他声称的人(比如用户名 ...

  3. Django-Form组件之字段

    Form类 创建Form类时,主要涉及到 [字段] 和 [插件],字段用于对用户请求数据的验证,插件用于自动生成HTML; 1.Django内置字段如下: 1 2 3 4 5 6 7 8 9 10 1 ...

  4. 使用velodyne16线激光雷达跑loam-velodyne

    一.velodyne-VLP16使用教程 推荐网址: http://blog.csdn.net/littlethunder/article/details/51920681 https://www.c ...

  5. 纵观 jBPM:从 jBPM3 到 jBPM5 以及 Activiti5

    https://www.infoq.cn/article/rh-jbpm5-activiti5# 对jBPM来说,今年最大的事件莫过于 jBPM 的创建者Tom Baeyens离开 JBoss 了.T ...

  6. LCD LED OLED区别 以及RGB、YUV和HSV颜色空间模型

    led 液晶本身不发光,而是有背光作为灯源,白色是由红绿蓝三色组成,黑色是,液晶挡住了led灯光穿过显示器. lcd比led更薄. oled:显示黑色时,灯是灭的,所以显示黑色更深,效果更好. 这就不 ...

  7. Java Socket NIO

    服务端: public class NIOServer { private static final String HOST = "localhost"; private stat ...

  8. 猜测的rpc负载均衡原理,基于dubbo的架构

    集群层(Cluster):封装多个提供者的路由及负载均衡,并桥接注册中心,以Invoker为中心,扩展接口为Cluster.Directory.Router和LoadBalance.将多个服务提供方组 ...

  9. Web高级 Eventloop和事件执行顺序

    1. EventLoop 1.1 调用栈 当一个方法执行时内部调用另外的方法,则会形成调用栈,如图: 1.2 任务队列 JavaScript有一个主线程执行当前任务,主线程的代码同步执行,并把遇到的事 ...

  10. Kong(v1.0.2)认证

    介绍 上游服务(api或微服务)的流量通常由各种Kong的authentication plugins的应用程序和配置控制.由于Kong的服务实体表示您自己的上游服务的一对一映射,所以最简单的场景是在 ...