Search in sources :

Example 1 with UserVo

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;
}
Also used : User(shu.java.csky.entity.User) UserVo(shu.java.csky.vo.UserVo) CommentVo(shu.java.csky.vo.CommentVo)

Example 2 with UserVo

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);
}
Also used : UserVo(shu.java.csky.vo.UserVo) Algorithm(com.auth0.jwt.algorithms.Algorithm) Date(java.util.Date)

Example 3 with UserVo

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;
}
Also used : JWTVerificationException(com.auth0.jwt.exceptions.JWTVerificationException) User(shu.java.csky.entity.User) UserVo(shu.java.csky.vo.UserVo) JWTDecodeException(com.auth0.jwt.exceptions.JWTDecodeException) PassToken(shu.java.csky.annotation.PassToken) HandlerMethod(org.springframework.web.method.HandlerMethod) Method(java.lang.reflect.Method) UserLoginToken(shu.java.csky.annotation.UserLoginToken) JWTVerifier(com.auth0.jwt.JWTVerifier) HandlerMethod(org.springframework.web.method.HandlerMethod)

Aggregations

UserVo (shu.java.csky.vo.UserVo)3 User (shu.java.csky.entity.User)2 JWTVerifier (com.auth0.jwt.JWTVerifier)1 Algorithm (com.auth0.jwt.algorithms.Algorithm)1 JWTDecodeException (com.auth0.jwt.exceptions.JWTDecodeException)1 JWTVerificationException (com.auth0.jwt.exceptions.JWTVerificationException)1 Method (java.lang.reflect.Method)1 Date (java.util.Date)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1 PassToken (shu.java.csky.annotation.PassToken)1 UserLoginToken (shu.java.csky.annotation.UserLoginToken)1 CommentVo (shu.java.csky.vo.CommentVo)1