Skip to content

Commit 0570b34

Browse files
author
hong
committed
jwt 无状态登录.
1 parent c6fae0a commit 0570b34

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

chapter10-jwt/README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,39 @@
22

33
## jwt(json web token)
44
- 用户发送按照约定,向服务端发送 Header、Payload 和 Signature,并包含认证信息(密码),验证通过后服务端返回一个token,之后用户使用该token作为登录凭证,适合于移动端和api
5+
// 1. Headers
6+
// 包括类别(typ)、加密算法(alg);
7+
{
8+
"alg": "HS256",
9+
"typ": "JWT"
10+
}
11+
// 2. Claims
12+
// 包括需要传递的用户信息;
13+
{
14+
"sub": "1234567890",
15+
"name": "John Doe",
16+
"admin": true
17+
}
18+
// 3. Signature
19+
// 根据alg算法与私有秘钥进行加密得到的签名字串;
20+
// 这一段是最重要的敏感信息,只能在服务端解密;
21+
HMACSHA256(
22+
base64UrlEncode(header) + "." +
23+
base64UrlEncode(payload),
24+
SECREATE_KEY
25+
)
526

627

728
- 具体流程如下:
829
1.登陆时生成token,并将token 保存到本地(提供两种方案:使用cookie 或者 localStorage)
930
2.在需要权限控制的请求中都带着token
1031
3.验证token是否失效,如果失效,提示实现或者重定向到登陆页面,重新获取token.
32+
33+
34+
1135

12-
![image](https://github.com/t-hong/springboot-examples/tree/master/chapter10-jwt/src/main/resources/static/images/oauth.JPG)
36+
![image]()https://github.com/t-hong/springboot-examples/tree/master/chapter10-jwt/src/main/resources/static/images/oauth.JPG
37+
38+
39+
参考资料:
40+
http://blog.leapoahead.com/2015/09/07/user-authentication-with-jwt/

chapter10-jwt/src/main/java/com/hong/jwt/utils/JwtHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ private String generateAudience(Device device) {
109109
if (device.isNormal()) {
110110
audience = AUDIENCE_WEB;//Pc端
111111
} else if (device.isTablet()) {
112-
audience = AUDIENCE_TABLET;//Pc端
112+
audience = AUDIENCE_TABLET;//平板
113113
} else if (device.isMobile()) {
114-
audience = AUDIENCE_MOBILE;//平板
114+
audience = AUDIENCE_MOBILE;//手机
115115
}
116116
return audience;
117117
}

0 commit comments

Comments
 (0)