|
|
@@ -1,89 +1,89 @@
|
|
1
|
|
-package com.ruoyi.novel.service;
|
|
2
|
|
-
|
|
3
|
|
-import com.ruoyi.common.core.domain.model.LoginUser;
|
|
4
|
|
-import io.jsonwebtoken.Claims;
|
|
5
|
|
-import io.jsonwebtoken.Jwts;
|
|
6
|
|
-import io.jsonwebtoken.io.Decoders;
|
|
7
|
|
-import io.jsonwebtoken.security.Keys;
|
|
8
|
|
-import org.springframework.beans.factory.annotation.Value;
|
|
9
|
|
-import org.springframework.security.core.Authentication;
|
|
10
|
|
-import org.springframework.security.core.GrantedAuthority;
|
|
11
|
|
-import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
12
|
|
-import org.springframework.stereotype.Service;
|
|
13
|
|
-
|
|
14
|
|
-import javax.crypto.SecretKey;
|
|
15
|
|
-import java.util.*;
|
|
16
|
|
-import java.util.stream.Collectors;
|
|
17
|
|
-
|
|
18
|
|
-@Service
|
|
19
|
|
-public class TokenService {
|
|
20
|
|
-
|
|
21
|
|
- @Value("${jwt.secret}") // 从配置文件中读取密钥
|
|
22
|
|
- private String secret;
|
|
23
|
|
-
|
|
24
|
|
- @Value("${jwt.expiration}") // JWT有效期
|
|
25
|
|
- private long expiration;
|
|
26
|
|
-
|
|
27
|
|
- // 获取登录用户信息
|
|
28
|
|
- public LoginUser getLoginUser(String token) {
|
|
29
|
|
- Claims claims = parseToken(token);
|
|
30
|
|
- Long userId = claims.get("user_id", Long.class);
|
|
31
|
|
- String username = claims.getSubject();
|
|
32
|
|
-
|
|
33
|
|
- // 获取权限列表
|
|
34
|
|
- List<String> authorities = claims.get("authorities", List.class);
|
|
35
|
|
- Set<GrantedAuthority> grantedAuthorities = authorities.stream()
|
|
36
|
|
- .map(SimpleGrantedAuthority::new)
|
|
37
|
|
- .collect(Collectors.toSet());
|
|
38
|
|
-
|
|
39
|
|
- return new LoginUser(userId, username, "", grantedAuthorities);
|
|
40
|
|
- }
|
|
41
|
|
-
|
|
42
|
|
- // 解析用户ID
|
|
43
|
|
- public Long parseUserId(String token) {
|
|
44
|
|
- Claims claims = parseToken(token);
|
|
45
|
|
- return claims.get("user_id", Long.class);
|
|
46
|
|
- }
|
|
47
|
|
-
|
|
48
|
|
- // 解析JWT令牌
|
|
49
|
|
- private Claims parseToken(String token) {
|
|
50
|
|
- byte[] keyBytes = Decoders.BASE64.decode(secret);
|
|
51
|
|
- SecretKey key = Keys.hmacShaKeyFor(keyBytes);
|
|
52
|
|
-
|
|
53
|
|
- return Jwts.parserBuilder()
|
|
54
|
|
- .setSigningKey(key)
|
|
55
|
|
- .build()
|
|
56
|
|
- .parseClaimsJws(token)
|
|
57
|
|
- .getBody();
|
|
58
|
|
- }
|
|
59
|
|
-
|
|
60
|
|
- // 验证令牌有效期
|
|
61
|
|
- public boolean validateToken(String token) {
|
|
62
|
|
- try {
|
|
63
|
|
- Claims claims = parseToken(token);
|
|
64
|
|
- Date expirationDate = claims.getExpiration();
|
|
65
|
|
- return expirationDate.after(new Date());
|
|
66
|
|
- } catch (Exception e) {
|
|
67
|
|
- return false;
|
|
68
|
|
- }
|
|
69
|
|
- }
|
|
70
|
|
-
|
|
71
|
|
- // 创建JWT令牌(如果需要)
|
|
72
|
|
- public String createToken(Authentication authentication) {
|
|
73
|
|
- LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
74
|
|
-
|
|
75
|
|
- byte[] keyBytes = Decoders.BASE64.decode(secret);
|
|
76
|
|
- SecretKey key = Keys.hmacShaKeyFor(keyBytes);
|
|
77
|
|
-
|
|
78
|
|
- return Jwts.builder()
|
|
79
|
|
- .setSubject(loginUser.getUsername())
|
|
80
|
|
- .claim("user_id", loginUser.getUserId())
|
|
81
|
|
- .claim("authorities", loginUser.getAuthorities().stream()
|
|
82
|
|
- .map(GrantedAuthority::getAuthority)
|
|
83
|
|
- .collect(Collectors.toList()))
|
|
84
|
|
- .setIssuedAt(new Date())
|
|
85
|
|
- .setExpiration(new Date(System.currentTimeMillis() + expiration))
|
|
86
|
|
- .signWith(key)
|
|
87
|
|
- .compact();
|
|
88
|
|
- }
|
|
89
|
|
-}
|
|
|
1
|
+//package com.ruoyi.novel.service;
|
|
|
2
|
+//
|
|
|
3
|
+//import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
4
|
+//import io.jsonwebtoken.Claims;
|
|
|
5
|
+//import io.jsonwebtoken.Jwts;
|
|
|
6
|
+//import io.jsonwebtoken.io.Decoders;
|
|
|
7
|
+//import io.jsonwebtoken.security.Keys;
|
|
|
8
|
+//import org.springframework.beans.factory.annotation.Value;
|
|
|
9
|
+//import org.springframework.security.core.Authentication;
|
|
|
10
|
+//import org.springframework.security.core.GrantedAuthority;
|
|
|
11
|
+//import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
|
12
|
+//import org.springframework.stereotype.Service;
|
|
|
13
|
+//
|
|
|
14
|
+//import javax.crypto.SecretKey;
|
|
|
15
|
+//import java.util.*;
|
|
|
16
|
+//import java.util.stream.Collectors;
|
|
|
17
|
+//
|
|
|
18
|
+//@Service
|
|
|
19
|
+//public class TokenService {
|
|
|
20
|
+//
|
|
|
21
|
+// @Value("${jwt.secret}") // 从配置文件中读取密钥
|
|
|
22
|
+// private String secret;
|
|
|
23
|
+//
|
|
|
24
|
+// @Value("${jwt.expiration}") // JWT有效期
|
|
|
25
|
+// private long expiration;
|
|
|
26
|
+//
|
|
|
27
|
+// // 获取登录用户信息
|
|
|
28
|
+// public LoginUser getLoginUser(String token) {
|
|
|
29
|
+// Claims claims = parseToken(token);
|
|
|
30
|
+// Long userId = claims.get("user_id", Long.class);
|
|
|
31
|
+// String username = claims.getSubject();
|
|
|
32
|
+//
|
|
|
33
|
+// // 获取权限列表
|
|
|
34
|
+// List<String> authorities = claims.get("authorities", List.class);
|
|
|
35
|
+// Set<GrantedAuthority> grantedAuthorities = authorities.stream()
|
|
|
36
|
+// .map(SimpleGrantedAuthority::new)
|
|
|
37
|
+// .collect(Collectors.toSet());
|
|
|
38
|
+//
|
|
|
39
|
+// return new LoginUser(userId, username, "", grantedAuthorities);
|
|
|
40
|
+// }
|
|
|
41
|
+//
|
|
|
42
|
+// // 解析用户ID
|
|
|
43
|
+// public Long parseUserId(String token) {
|
|
|
44
|
+// Claims claims = parseToken(token);
|
|
|
45
|
+// return claims.get("user_id", Long.class);
|
|
|
46
|
+// }
|
|
|
47
|
+//
|
|
|
48
|
+// // 解析JWT令牌
|
|
|
49
|
+// private Claims parseToken(String token) {
|
|
|
50
|
+// byte[] keyBytes = Decoders.BASE64.decode(secret);
|
|
|
51
|
+// SecretKey key = Keys.hmacShaKeyFor(keyBytes);
|
|
|
52
|
+//
|
|
|
53
|
+// return Jwts.parserBuilder()
|
|
|
54
|
+// .setSigningKey(key)
|
|
|
55
|
+// .build()
|
|
|
56
|
+// .parseClaimsJws(token)
|
|
|
57
|
+// .getBody();
|
|
|
58
|
+// }
|
|
|
59
|
+//
|
|
|
60
|
+// // 验证令牌有效期
|
|
|
61
|
+// public boolean validateToken(String token) {
|
|
|
62
|
+// try {
|
|
|
63
|
+// Claims claims = parseToken(token);
|
|
|
64
|
+// Date expirationDate = claims.getExpiration();
|
|
|
65
|
+// return expirationDate.after(new Date());
|
|
|
66
|
+// } catch (Exception e) {
|
|
|
67
|
+// return false;
|
|
|
68
|
+// }
|
|
|
69
|
+// }
|
|
|
70
|
+//
|
|
|
71
|
+// // 创建JWT令牌(如果需要)
|
|
|
72
|
+// public String createToken(Authentication authentication) {
|
|
|
73
|
+// LoginUser loginUser = (LoginUser) authentication.getPrincipal();
|
|
|
74
|
+//
|
|
|
75
|
+// byte[] keyBytes = Decoders.BASE64.decode(secret);
|
|
|
76
|
+// SecretKey key = Keys.hmacShaKeyFor(keyBytes);
|
|
|
77
|
+//
|
|
|
78
|
+// return Jwts.builder()
|
|
|
79
|
+// .setSubject(loginUser.getUsername())
|
|
|
80
|
+// .claim("user_id", loginUser.getUserId())
|
|
|
81
|
+// .claim("authorities", loginUser.getAuthorities().stream()
|
|
|
82
|
+// .map(GrantedAuthority::getAuthority)
|
|
|
83
|
+// .collect(Collectors.toList()))
|
|
|
84
|
+// .setIssuedAt(new Date())
|
|
|
85
|
+// .setExpiration(new Date(System.currentTimeMillis() + expiration))
|
|
|
86
|
+// .signWith(key)
|
|
|
87
|
+// .compact();
|
|
|
88
|
+// }
|
|
|
89
|
+//}
|