Authored by fangyeqing

MOD:Token key:long to string

@@ -12,7 +12,7 @@ public interface ITokenManager { @@ -12,7 +12,7 @@ public interface ITokenManager {
12 * @param userId 指定用户的id 12 * @param userId 指定用户的id
13 * @return 生成的token 13 * @return 生成的token
14 */ 14 */
15 - public TokenModel createToken(long userId); 15 + public TokenModel createToken(String userId);
16 16
17 /** 17 /**
18 * 检查token是否有效 18 * 检查token是否有效
@@ -32,6 +32,6 @@ public interface ITokenManager { @@ -32,6 +32,6 @@ public interface ITokenManager {
32 * 清除token 32 * 清除token
33 * @param userId 登录用户的id 33 * @param userId 登录用户的id
34 */ 34 */
35 - public void deleteToken(long userId); 35 + public void deleteToken(String userId);
36 36
37 } 37 }
@@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit; @@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit;
18 @Component 18 @Component
19 public class RedisTokenManager implements ITokenManager { 19 public class RedisTokenManager implements ITokenManager {
20 20
21 - private RedisTemplate<Long, String> redis; 21 + private RedisTemplate<String, String> redis;
22 22
23 @Autowired 23 @Autowired
24 public void setRedis(RedisTemplate redis) { 24 public void setRedis(RedisTemplate redis) {
@@ -27,7 +27,7 @@ public class RedisTokenManager implements ITokenManager { @@ -27,7 +27,7 @@ public class RedisTokenManager implements ITokenManager {
27 redis.setKeySerializer(new JdkSerializationRedisSerializer()); 27 redis.setKeySerializer(new JdkSerializationRedisSerializer());
28 } 28 }
29 29
30 - public TokenModel createToken(long userId) { 30 + public TokenModel createToken(String userId) {
31 //使用uuid作为源token 31 //使用uuid作为源token
32 String token = UUID.randomUUID().toString().replace("-", ""); 32 String token = UUID.randomUUID().toString().replace("-", "");
33 TokenModel model = new TokenModel(userId, token); 33 TokenModel model = new TokenModel(userId, token);
@@ -45,7 +45,7 @@ public class RedisTokenManager implements ITokenManager { @@ -45,7 +45,7 @@ public class RedisTokenManager implements ITokenManager {
45 return null; 45 return null;
46 } 46 }
47 //使用userId和源token简单拼接成的token,可以增加加密措施 47 //使用userId和源token简单拼接成的token,可以增加加密措施
48 - long userId = Long.parseLong(param[0]); 48 + String userId = param[0];
49 String token = param[1]; 49 String token = param[1];
50 return new TokenModel(userId, token); 50 return new TokenModel(userId, token);
51 } 51 }
@@ -63,7 +63,7 @@ public class RedisTokenManager implements ITokenManager { @@ -63,7 +63,7 @@ public class RedisTokenManager implements ITokenManager {
63 return true; 63 return true;
64 } 64 }
65 65
66 - public void deleteToken(long userId) { 66 + public void deleteToken(String userId) {
67 redis.delete(userId); 67 redis.delete(userId);
68 } 68 }
69 } 69 }
1 package com.xkl.authorization.model; 1 package com.xkl.authorization.model;
2 2
  3 +import lombok.AllArgsConstructor;
  4 +import lombok.Data;
  5 +
3 /** 6 /**
4 * Token的Model类,可以增加字段提高安全性,例如时间戳、url签名 7 * Token的Model类,可以增加字段提高安全性,例如时间戳、url签名
5 */ 8 */
  9 +@Data
  10 +@AllArgsConstructor
6 public class TokenModel { 11 public class TokenModel {
7 12
8 //用户id 13 //用户id
9 - private long userId; 14 + private String userId;
10 15
11 //随机生成的uuid 16 //随机生成的uuid
12 private String token; 17 private String token;
13 -  
14 - public TokenModel(long userId, String token) {  
15 - this.userId = userId;  
16 - this.token = token;  
17 - }  
18 -  
19 - public long getUserId() {  
20 - return userId;  
21 - }  
22 -  
23 - public void setUserId(long userId) {  
24 - this.userId = userId;  
25 - }  
26 -  
27 - public String getToken() {  
28 - return token;  
29 - }  
30 -  
31 - public void setToken(String token) {  
32 - this.token = token;  
33 - }  
34 } 18 }
@@ -37,10 +37,10 @@ public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentR @@ -37,10 +37,10 @@ public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentR
37 @Override 37 @Override
38 public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { 38 public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
39 //取出鉴权时存入的登录用户Id 39 //取出鉴权时存入的登录用户Id
40 - Long currentUserId = (Long) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST); 40 + String currentUserId = (String) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST);
41 if (currentUserId != null) { 41 if (currentUserId != null) {
42 //从数据库中查询并返回 42 //从数据库中查询并返回
43 - return userRepository.findOne(currentUserId); 43 + return userRepository.findOne(Long.parseLong(currentUserId));
44 } 44 }
45 throw new MissingServletRequestPartException(Constants.CURRENT_USER_ID); 45 throw new MissingServletRequestPartException(Constants.CURRENT_USER_ID);
46 } 46 }
@@ -52,7 +52,7 @@ public class TokenController { @@ -52,7 +52,7 @@ public class TokenController {
52 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND); 52 return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
53 } 53 }
54 //生成一个token,保存用户登录状态 54 //生成一个token,保存用户登录状态
55 - TokenModel model = tokenManager.createToken(user.getId()); 55 + TokenModel model = tokenManager.createToken(String.valueOf(user.getId()));
56 return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK); 56 return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
57 } 57 }
58 58
@@ -63,7 +63,7 @@ public class TokenController { @@ -63,7 +63,7 @@ public class TokenController {
63 @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"), 63 @ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
64 }) 64 })
65 public ResponseEntity<ResultModel> logout(@CurrentUser User user) { 65 public ResponseEntity<ResultModel> logout(@CurrentUser User user) {
66 - tokenManager.deleteToken(user.getId()); 66 + tokenManager.deleteToken(String.valueOf(user.getId()));
67 return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK); 67 return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
68 } 68 }
69 69
@@ -70,7 +70,7 @@ public class UserInfoController { @@ -70,7 +70,7 @@ public class UserInfoController {
70 user.setPassword(pass); 70 user.setPassword(pass);
71 user.setSalt(salt); 71 user.setSalt(salt);
72 userRepository.save(user); 72 userRepository.save(user);
73 - tokenManager.deleteToken(user.getId());//退出登录 73 + tokenManager.deleteToken(String.valueOf(user.getId()));//退出登录
74 return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_LOGOUT), HttpStatus.OK); 74 return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_LOGOUT), HttpStatus.OK);
75 } 75 }
76 76