一个JWT实际上就是一个字符串,它由三部分组成,
头部
、
载荷
Payload
与
签名
。
JWTs can be signed using a secret (with the
HMAC
algorithm) or a public/private key pair using
RSA
.
/*
|--------------------------------------------------------------------------
| Required Claims
|--------------------------------------------------------------------------
|
| Specify the required claims that
must exist in any token.
| A TokenInvalidException will be thrown if any of these claims are not
| present in the payload.
|
*/
'required_claims' => [
'iss',
'iat',
'exp',
'
nbf',
'sub',
'
jti',
],
示例:
{
"iss": "http://laravel_api.app/api/register",
"iat":
1502356728,
"exp": 1502360328,(相差3600s)
"nbf":
1502356728,
"jti": "R0Gd00AvOW259LGo",
"sub": 11,
"prv": "3787fba1618a930526aca6c8bb9b44b83f297726"
}
这里面的前6个字段都是由JWT的
标准所定义的
。
- iss: 该JWT的签发者
- iat(issued at): 在何时签发的token
- exp(expires): token何时过时
- nbf(not before):token在此时间以前不能被接收处理
- jti:JWT ID为web token提供惟一标识
iss是issuer的简写,代表请求的实体,能够是发出请求的用户的信息。
There are three types of claims:
reserved
,
public
, and
private
claims.
像上面的是reserved:
iss
(issuer),
exp
(expiration time),
sub
(subject),
aud
(audience), and others.
An example of payload could be:
{ "sub":
"1234567890"
, "name":
"John Doe"
, "admin":
true
}
{ "iss": "John Wu JWT", "iat": 1441593502, "exp": 1441594722, "aud": "www.example.com", "sub": "jrocket@example.com", "from_user": "B", "target_user": "A" } |
这里面的前五个字段都是由JWT的标准所定义的。
aud
: 接收该JWT的一方
将上面的JSON对象进行[
base64编码
]能够获得下面的字符串。这个字符串咱们将它称做JWT的
Payload
(载荷)。
1 |
eyJpc3MiOiJKb2huIFd1IEpXVCIsImlhdCI6MTQ0MTU5MzUwMiwiZXhwIjoxNDQxNTk0NzIyLCJhdWQiOiJ3d3cuZXhhbXBsZS5jb20iLCJzdWIiOiJqcm9ja2V0QGV4YW1 |
JWT还须要一个头部,头部用于描述关于该JWT的最基本的信息,例如其类型以及签名所用的算法等。这也能够被表示成一个JSON对象。
1 2 3 4 |
{ "typ": "JWT", "alg": "HS256" |
对它也要进行
Base64编码
,以后的字符串就成了JWT的
Header
(头部)。
1 |
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9 |
将上面的两个编码后的字符串用句号
.
链接在一块儿(头部在前),就造成了
1 |
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcm9tX3VzZXIiOiJCIiwidGFyZ2V0X3VzZXIiOiJBIn0 |
最后,咱们将上面拼接完的字符串用HS256算法进行加密。在加密的时候,咱们还须要提供一个密钥(secret)
获得咱们加密后的内容
1 |
rSWamyAYwuHCo7IFAgd1oRpSP7nzL7BF5t7ItqpKViM |
最后将这一部分签名
也拼接在被签名的字符串后面(2个句号)
,咱们就获得了完整的JWT
1 |
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcm9tX3VzZXIiOiJCIiwidGFyZ2V0X3VzZXIiOiJBIn0.rSWamyAYwuHCo7IFAgd1oRpSP7nzL7BF5t7ItqpKViM |
多点登陆”
Set
-Cookie: jwt=lll.zzz.xxx;
HttpOnly
; max-age=980000; domain=.taobao.com
注意
domain
必须设置为一个点加顶级域名,即
.taobao.com
。这样,taobao.com和*.taobao.com就均可以接受到这个Cookie,并获取JWT了。
通常放在HTTP的headers 参数里面的authorization里面,值的前面加
Bearer关键字和空格
。除此以外,也能够在url和request body中传递。
Authorization: Bearer {yourtokenhere}
- OAuth是一个受权的开放网络标准,最终目的是取得token(令牌)
- Token 令牌,视为用户登陆成功,通行的惟一令牌
- JWT是生成token的轻巧规范,能够结合一块儿使用
Unresolvable dependency resolving [Parameter #0 [ $app ]] in class Illuminate\Cache\CacheManager
Dependency Bind from
$app->bind(Illuminate\Cache\CacheManager::class, function ($app) { return new Illuminate\Cache\CacheManager($app); });
Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\Auth\AuthManager
$app->bind(Illuminate\Auth\AuthManager::class, function ($app) { return new Illuminate\Auth\AuthManager($app); });