1.什么是javaJwt?
JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties. JWT.IO allows you to decode, verify and generate JWT.https://jwt.io/ javaJwt is library of jwt implemented by java
2.javaJwt的类结构
术语解释:
通常在传输数据时,为了使数据传输更可靠,要把原始数据分批传输,并且在每一批数据的头和尾都加上一定的辅助信息,比如这一批数据量的大小,校验位等,这样就相当于给已经分批原始数据加一些外套,这些外套起到标示作用,使得原始数据不易丢失。
一批数据加上它的“外套”,就形成了传输通道中基本的传输单元,叫做数据帧或者数据包(有的地方数据帧和数据包不是同一概念比如网络传输)。这些数据帧中的记录信息的原始数据就是有效载荷数据,即payload data。
而消息体就是外套。即标记着原始数据的大小等的辅助信息。
- The Header class represents the 1st part of the JWT, where the Header value is hold.
- The Payload class represents the 2nd part of the JWT, where the Payload value is hold.
- The Signature class represents the 3rd part of the JWT, where the Signature value is hold.
- The JWTDecoder class holds the decode method to parse a given JWT token into it's JWT representation.
- The JWTVerifier class holds the verify method to assert that a given Token has not only a proper JWT format, but also it's signature matches.
- The JWTCreator class holds the sign method to generate a complete JWT (with Signature) from a given Header and Payload content.
- The Claim class holds the value in a generic way so that it can be recovered in many representations.
根据JWT的标准,这些claims可以分为以下三种类型:
a. Reserved claims(保留),它的含义就像是编程语言的保留字一样,属于JWT标准里面规定的一些claim。JWT标准里面定好的claim有:
- iss(Issuser):代表这个JWT的签发主体;
- sub(Subject):代表这个JWT的主体,即它的所有人;
- aud(Audience):代表这个JWT的接收对象;
- exp(Expiration time):是一个时间戳,代表这个JWT的过期时间;
- nbf(Not Before):是一个时间戳,代表这个JWT生效的开始时间,意味着在这个时间之前验证JWT是会失败的;
- iat(Issued at):是一个时间戳,代表这个JWT的签发时间;
- jti(JWT ID):是JWT的唯一标识。
b. Public claims,略(不重要)
c. Private claims,这个指的就是自定义的claim。比如前面那个结构举例中的admin和name都属于自定的claim。这些claim跟JWT标准规定的claim区别在于:JWT规定的claim,JWT的接收方在拿到JWT之后,都知道怎么对这些标准的claim进行验证;而private claims不会验证,除非明确告诉接收方要对这些claim进行验证以及规则才行。
按照JWT标准的说明:保留的claims都是可选的,在生成payload不强制用上面的那些claim,你可以完全按照自己的想法来定义payload的结构,不过这样搞根本没必要:第一是,如果把JWT用于认证, 那么JWT标准内规定的几个claim就足够用了,甚至只需要其中一两个就可以了,假如想往JWT里多存一些用户业务信息,比如角色和用户名等,这倒是用自定义的claim来添加;第二是,JWT标准里面针对它自己规定的claim都提供了有详细的验证规则描述,每个实现库都会参照这个描述来提供JWT的验证实现,所以如果是自定义的claim名称,那么你用到的实现库就不会主动去验证这些claim。
参考文献:
【1】http://www.cnblogs.com/lyzg/p/6028341.html
,