Search in sources :

Example 1 with User

use of shu.java.csky.entity.User 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 User

use of shu.java.csky.entity.User in project CSKY by SHU-Silence.

the class UserServiceImpl method userRegister.

@Override
public ResultVO userRegister(String name, String pwd, String email) {
    // 1.根据用户查询,这个用户是否已经被注册
    if (isRegister(name)) {
        return new ResultVO(ResStatus.NO, "用户名已经被注册!", null);
    }
    // 2.如果没有被注册则进行保存操作
    String md5Pwd = MD5Utils.md5(pwd);
    User u = new User();
    u.setUsername(name);
    u.setPassword(md5Pwd);
    // 3. 设置随机头像
    List<Avatar> avatarList = avatarMapper.selectList(null);
    u.setUserImg(avatarList.get(new Random().nextInt(avatarList.size())).getSrc());
    u.setUserEmail(email);
    u.setUserRegtime(new Date());
    u.setUserModtime(new Date());
    // 执行保存操作
    int count = userMapper.insert(u);
    if (count == 1) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", u.getUsername()));
        user.setPassword(null);
        return new ResultVO(ResStatus.OK, "注册成功!", user);
    }
    return new ResultVO(ResStatus.NO, "注册失败!", null);
}
Also used : ResultVO(shu.java.csky.vo.ResultVO) User(shu.java.csky.entity.User) Random(java.util.Random) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Avatar(shu.java.csky.entity.Avatar) Date(java.util.Date)

Example 3 with User

use of shu.java.csky.entity.User 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)

Example 4 with User

use of shu.java.csky.entity.User in project CSKY by SHU-Silence.

the class UserServiceImpl method checkLogin.

@Override
public ResultVO checkLogin(String name, String pwd) {
    // 获取该用户名对应的user对象
    QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
    userQueryWrapper.eq("username", name);
    User user = userMapper.selectOne(userQueryWrapper);
    if (user == null) {
        return new ResultVO(ResStatus.NO, "用户名或密码错误!", null);
    }
    // 得到该用户信息后
    String md5Pwd = MD5Utils.md5(pwd);
    if (md5Pwd.equals(user.getPassword())) {
        // 密码正确
        // 如果登录验证成功且已经激活,则需要生成令牌token(token就是按照特定规则生成的字符串)
        HashMap<String, Object> map = new HashMap<>();
        map.put("TOKEN", createToken(user));
        return new ResultVO(ResStatus.OK, "登录成功!", map);
    }
    throw new RuntimeException("用户名或密码错误!");
}
Also used : ResultVO(shu.java.csky.vo.ResultVO) User(shu.java.csky.entity.User) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HashMap(java.util.HashMap)

Aggregations

User (shu.java.csky.entity.User)4 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)2 ResultVO (shu.java.csky.vo.ResultVO)2 UserVo (shu.java.csky.vo.UserVo)2 JWTVerifier (com.auth0.jwt.JWTVerifier)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 HashMap (java.util.HashMap)1 Random (java.util.Random)1 HandlerMethod (org.springframework.web.method.HandlerMethod)1 PassToken (shu.java.csky.annotation.PassToken)1 UserLoginToken (shu.java.csky.annotation.UserLoginToken)1 Avatar (shu.java.csky.entity.Avatar)1 CommentVo (shu.java.csky.vo.CommentVo)1