File tree Expand file tree Collapse file tree 2 files changed +31
-3
lines changed
src/main/java/com/hong/jwt/utils Expand file tree Collapse file tree 2 files changed +31
-3
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## jwt(json web token)
4
4
- 用户发送按照约定,向服务端发送 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
+ )
5
26
6
27
7
28
- 具体流程如下:
8
29
1.登陆时生成token,并将token 保存到本地(提供两种方案:使用cookie 或者 localStorage)
9
30
2.在需要权限控制的请求中都带着token
10
31
3.验证token是否失效,如果失效,提示实现或者重定向到登陆页面,重新获取token.
32
+
33
+
34
+
11
35
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/
Original file line number Diff line number Diff line change @@ -109,9 +109,9 @@ private String generateAudience(Device device) {
109
109
if (device .isNormal ()) {
110
110
audience = AUDIENCE_WEB ;//Pc端
111
111
} else if (device .isTablet ()) {
112
- audience = AUDIENCE_TABLET ;//Pc端
112
+ audience = AUDIENCE_TABLET ;//平板
113
113
} else if (device .isMobile ()) {
114
- audience = AUDIENCE_MOBILE ;//平板
114
+ audience = AUDIENCE_MOBILE ;//手机
115
115
}
116
116
return audience ;
117
117
}
You can’t perform that action at this time.
0 commit comments