Authored by fangyeqing

MOD:Token key:long to string

... ... @@ -12,7 +12,7 @@ public interface ITokenManager {
* @param userId 指定用户的id
* @return 生成的token
*/
public TokenModel createToken(long userId);
public TokenModel createToken(String userId);
/**
* 检查token是否有效
... ... @@ -32,6 +32,6 @@ public interface ITokenManager {
* 清除token
* @param userId 登录用户的id
*/
public void deleteToken(long userId);
public void deleteToken(String userId);
}
... ...
... ... @@ -18,7 +18,7 @@ import java.util.concurrent.TimeUnit;
@Component
public class RedisTokenManager implements ITokenManager {
private RedisTemplate<Long, String> redis;
private RedisTemplate<String, String> redis;
@Autowired
public void setRedis(RedisTemplate redis) {
... ... @@ -27,7 +27,7 @@ public class RedisTokenManager implements ITokenManager {
redis.setKeySerializer(new JdkSerializationRedisSerializer());
}
public TokenModel createToken(long userId) {
public TokenModel createToken(String userId) {
//使用uuid作为源token
String token = UUID.randomUUID().toString().replace("-", "");
TokenModel model = new TokenModel(userId, token);
... ... @@ -45,7 +45,7 @@ public class RedisTokenManager implements ITokenManager {
return null;
}
//使用userId和源token简单拼接成的token,可以增加加密措施
long userId = Long.parseLong(param[0]);
String userId = param[0];
String token = param[1];
return new TokenModel(userId, token);
}
... ... @@ -63,7 +63,7 @@ public class RedisTokenManager implements ITokenManager {
return true;
}
public void deleteToken(long userId) {
public void deleteToken(String userId) {
redis.delete(userId);
}
}
... ...
package com.xkl.authorization.model;
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* Token的Model类,可以增加字段提高安全性,例如时间戳、url签名
*/
@Data
@AllArgsConstructor
public class TokenModel {
//用户id
private long userId;
private String userId;
//随机生成的uuid
private String token;
public TokenModel(long userId, String token) {
this.userId = userId;
this.token = token;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}
... ...
... ... @@ -37,10 +37,10 @@ public class CurrentUserMethodArgumentResolver implements HandlerMethodArgumentR
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
//取出鉴权时存入的登录用户Id
Long currentUserId = (Long) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST);
String currentUserId = (String) webRequest.getAttribute(Constants.CURRENT_USER_ID, RequestAttributes.SCOPE_REQUEST);
if (currentUserId != null) {
//从数据库中查询并返回
return userRepository.findOne(currentUserId);
return userRepository.findOne(Long.parseLong(currentUserId));
}
throw new MissingServletRequestPartException(Constants.CURRENT_USER_ID);
}
... ...
... ... @@ -52,7 +52,7 @@ public class TokenController {
return new ResponseEntity<>(ResultModel.error(ResultStatus.USERNAME_OR_PASSWORD_ERROR), HttpStatus.NOT_FOUND);
}
//生成一个token,保存用户登录状态
TokenModel model = tokenManager.createToken(user.getId());
TokenModel model = tokenManager.createToken(String.valueOf(user.getId()));
return new ResponseEntity<>(ResultModel.ok(model), HttpStatus.OK);
}
... ... @@ -63,7 +63,7 @@ public class TokenController {
@ApiImplicitParam(name = "authorization", value = "请输入登录返回信息:userId_tokens", required = true, dataType = "string", paramType = "header"),
})
public ResponseEntity<ResultModel> logout(@CurrentUser User user) {
tokenManager.deleteToken(user.getId());
tokenManager.deleteToken(String.valueOf(user.getId()));
return new ResponseEntity<>(ResultModel.ok(), HttpStatus.OK);
}
... ...
... ... @@ -70,7 +70,7 @@ public class UserInfoController {
user.setPassword(pass);
user.setSalt(salt);
userRepository.save(user);
tokenManager.deleteToken(user.getId());//退出登录
tokenManager.deleteToken(String.valueOf(user.getId()));//退出登录
return new ResponseEntity<>(ResultModel.ok(ResultStatus.USER_LOGOUT), HttpStatus.OK);
}
... ...