use of shu.java.csky.vo.UserVo in project CSKY by SHU-Silence.
the class CommentServiceImpl method copy.
private CommentVo copy(Comment comment, Integer uid) {
// 父评论
CommentVo commentVo = new CommentVo();
BeanUtils.copyProperties(comment, commentVo);
Integer userId = comment.getUserId();
User user = userMapper.selectById(userId);
UserVo userVo = new UserVo();
BeanUtils.copyProperties(user, userVo);
commentVo.setAuthor(userVo);
Integer id = comment.getId();
// 处理父评论的点赞状态
Object dataFather = getConditionByAIdAndUId(comment.getId(), uid).getData();
commentVo.setIsLiked((Boolean) dataFather);
// 评论的评论
List<CommentVo> commentVoList = findCommentByParentId(id, uid);
commentVo.setChildren(commentVoList);
if (comment.getLevel() > 1) {
Integer toUid = comment.getToUid();
User toUser = userMapper.selectById(toUid);
UserVo toUserVo = new UserVo();
BeanUtils.copyProperties(toUser, toUserVo);
commentVo.setToUser(toUserVo);
Object dataChildren = getConditionByAIdAndUId(comment.getId(), uid).getData();
commentVo.setIsLiked((Boolean) dataChildren);
}
return commentVo;
}
use of shu.java.csky.vo.UserVo in project CSKY by SHU-Silence.
the class UserServiceImpl method createToken.
private String createToken(User user) {
UserVo userVo = new UserVo();
BeanUtils.copyProperties(user, userVo);
Algorithm algorithm = Algorithm.HMAC256(user.getPassword());
Date nowDate = new Date();
Date expireDate = new Date(System.currentTimeMillis() + 2 * 60 * 60 * 1000);
return JWT.create().withIssuedAt(// 设置 载荷 生成签名的时间
nowDate).withExpiresAt(// 设置 载荷 签名过期的时间
expireDate).withAudience(JSON.toJSONString(userVo), user.getUsername()).sign(// 签名 Signature
algorithm);
}
use of shu.java.csky.vo.UserVo in project CSKY by SHU-Silence.
the class UserTokenInterceptor method preHandle.
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
// 从 http 请求头中取出 token
String token = httpServletRequest.getHeader("TOKEN");
// 如果不是映射到方法直接通过
if (!(object instanceof HandlerMethod)) {
return true;
}
HandlerMethod handlerMethod = (HandlerMethod) object;
Method method = handlerMethod.getMethod();
// 检查是否有PassToken注释,有则跳过认证
if (method.isAnnotationPresent(PassToken.class)) {
PassToken passToken = method.getAnnotation(PassToken.class);
if (passToken.required()) {
return true;
}
}
// 检查有没有需要用户权限的注解
if (method.isAnnotationPresent(UserLoginToken.class)) {
UserLoginToken userLoginToken = method.getAnnotation(UserLoginToken.class);
if (userLoginToken.required()) {
// 执行认证
if (token == null) {
throw new RuntimeException("无token,请重新登录!");
}
// 获取 token 中的 user id
UserVo userVo = null;
try {
userVo = JSON.parseObject(JWT.decode(token).getAudience().get(0), UserVo.class);
} catch (JWTDecodeException j) {
throw new RuntimeException("TOKEN验证失败!");
}
User user = userService.getUserById(userVo.getUserId());
if (user == null) {
throw new RuntimeException("用户不存在,请重新登录!");
}
// 验证 token
JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPassword())).build();
try {
jwtVerifier.verify(token);
} catch (JWTVerificationException e) {
throw new RuntimeException("TOKEN验证失败!");
}
return true;
}
}
return true;
}
Aggregations