jwt Token-based authentication

I recently learned about Token-based authentication and shared it with you. Many large websites are also using it, such as Facebook, Twitter, Google+, Github, etc. Compared with traditional authentication methods, Token is more scalable and more secure, and is very suitable for use in web applications or mobile applications. Token is translated into "token" in Chinese. I think it is very good. It means that you can pass some levels with this token.

Traditional Authentication Methods

HTTP is a stateless protocol, that is, it does not know who is accessing the application. Here we regard the user as a client. The client uses the username and password to pass the authentication, but the next time the client sends a request, it has to be verified again.

The solution is that when the user requests to log in, if there is no problem, we will generate a record on the server side. This record can indicate who the logged in user is, and then send the ID number of this record to the client. After receiving it, the client stores the ID number in the cookie. The next time the user sends a request to the server, he can take the cookie with him, so that the server will verify the information in the cookie to see if it can be used in the service. The client finds the corresponding record here. If yes, it means that the user has passed the authentication, and returns the data requested by the user to the client.

The above is the Session. We need to store the Session generated for the logged-in user on the server side. These Sessions may be stored in memory, disk, or database. We may need to periodically clean up expired sessions on the server side.

Token-based authentication method

With the Token-based authentication method, there is no need to store the user's login record on the server. The approximate process is as follows:

  1. Client requests login with username and password
  2. The server receives a request to verify the username and password
  3. After the verification is successful, the server will issue a Token, and then send the Token to the client
  4. After the client receives the Token, it can store it, such as in Cookie or Local Storage
  5. Every time the client requests resources from the server, it needs to bring the Token issued by the server
  6. 服务端收到请求,然后去验证客户端请求里面带着的 Token,如果验证成功,就向客户端返回请求的数据

JWT

实施 Token 验证的方法挺多的,还有一些标准方法,比如 JWT,读作:jot ,表示:JSON Web Tokens 。JWT 标准的 Token 有三个部分:

  • header
  • payload
  • signature

中间用点分隔开,并且都会使用 Base64 编码,所以真正的 Token 看起来像这样:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJuaW5naGFvLm5ldCIsImV4cCI6IjE0Mzg5NTU0NDUiLCJuYW1lIjoid2FuZ2hhbyIsImFkbWluIjp0cnVlfQ.SwyHTEx_RQppr97g4J5lKXtabJecpejuef8AqKYMAJc

Header

header 部分主要是两部分内容,一个是 Token 的类型,另一个是使用的算法,比如下面类型就是 JWT,使用的算法是 HS256。

{
  "typ": "JWT",
  "alg": "HS256"
}

上面的内容要用 Base64 的形式编码一下,所以就变成这样:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9

Payload

Payload 里面是 Token 的具体内容,这些内容里面有一些是标准字段,你也可以添加其它需要的内容。下面是标准字段:

  • iss:Issuer,发行者
  • sub:Subject,主题
  • aud:Audience,观众
  • exp:Expiration time,过期时间
  • nbf:Not before
  • iat:Issued at,发行时间
  • jti:JWT ID

比如下面这个 Payload ,用到了 iss 发行人,还有 exp 过期时间。另外还有两个自定义的字段,一个是 name ,还有一个是 admin 。

{
 "iss": "ninghao.net",
 "exp": "1438955445",
 "name": "wanghao",
 "admin": true
}

使用 Base64 编码以后就变成了这个样子:

eyJpc3MiOiJuaW5naGFvLm5ldCIsImV4cCI6IjE0Mzg5NTU0NDUiLCJuYW1lIjoid2FuZ2hhbyIsImFkbWluIjp0cnVlfQ

Signature

JWT 的最后一部分是 Signature ,这部分内容有三个部分,先是用 Base64 编码的 header.payload ,再用加密算法加密一下,加密的时候要放进去一个 Secret ,这个相当于是一个密码,这个密码秘密地存储在服务端。

  • header
  • payload
  • secret
var encodedString = base64UrlEncode(header) + "." + base64UrlEncode(payload); 
HMACSHA256(encodedString, 'secret');

处理完成以后看起来像这样:

SwyHTEx_RQppr97g4J5lKXtabJecpejuef8AqKYMAJc

最后这个在服务端生成并且要发送给客户端的 Token 看起来像这样:

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJuaW5naGFvLm5ldCIsImV4cCI6IjE0Mzg5NTU0NDUiLCJuYW1lIjoid2FuZ2hhbyIsImFkbWluIjp0cnVlfQ.SwyHTEx_RQppr97g4J5lKXtabJecpejuef8AqKYMAJc

客户端收到这个 Token 以后把它存储下来,下回向服务端发送请求的时候就带着这个 Token 。服务端收到这个 Token ,然后进行验证,通过以后就会返回给客户端想要的资源。

相关链接

 

http://ninghao.net/blog/2834

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326724303&siteId=291194637