Search in sources :

Example 1 with User

use of com.fanxb.bookmark.common.entity.po.User in project bookmark by FleyX.

the class UserServiceImpl method getUserInfo.

/**
 * Description: 根据userId获取用户信息
 *
 * @param userId userId
 * @return com.fanxb.bookmark.common.entity.po.User
 * @author fanxb
 * @date 2019/7/30 15:57
 */
public User getUserInfo(int userId) {
    User user = userDao.selectByUserIdOrGithubId(userId, null);
    user.setNoPassword(StrUtil.isEmpty(user.getPassword()));
    return user;
}
Also used : User(com.fanxb.bookmark.common.entity.po.User)

Example 2 with User

use of com.fanxb.bookmark.common.entity.po.User in project bookmark by FleyX.

the class UserServiceImpl method checkPassword.

/**
 * 功能描述: 密码校验,校验成功返回一个actionId,以执行敏感操作
 *
 * @param password password
 * @return java.lang.String
 * @author fanxb
 * @date 2019/11/11 23:41
 */
public String checkPassword(String password) {
    int userId = UserContextHolder.get().getUserId();
    String pass = HashUtil.getPassword(password);
    User user = userDao.selectByUserIdOrGithubId(userId, null);
    if (!user.getPassword().equals(pass)) {
        throw new FormDataException("密码错误,请重试");
    }
    String actionId = UUID.randomUUID().toString().replaceAll("-", "");
    String key = RedisConstant.getPasswordCheckKey(userId, actionId);
    RedisUtil.set(key, "1", 5 * 60 * 1000);
    return actionId;
}
Also used : User(com.fanxb.bookmark.common.entity.po.User) FormDataException(com.fanxb.bookmark.common.exception.FormDataException)

Example 3 with User

use of com.fanxb.bookmark.common.entity.po.User in project bookmark by FleyX.

the class UserServiceImpl method resetPassword.

/**
 * Description: 重置密码
 *
 * @param body 重置密码 由于参数和注册差不多,所以用同一个表单
 * @author fanxb
 * @date 2019/7/9 19:59
 */
public void resetPassword(RegisterBody body) {
    User user = userDao.selectByUsernameOrEmail(body.getEmail(), body.getEmail());
    if (user == null) {
        throw new FormDataException("用户不存在");
    }
    String codeKey = CommonConstant.authCodeKey(body.getEmail());
    String realCode = RedisUtil.get(codeKey, String.class);
    if (StringUtil.isEmpty(realCode) || (!realCode.equals(body.getAuthCode()))) {
        throw new FormDataException("验证码错误");
    }
    RedisUtil.delete(codeKey);
    String newPassword = HashUtil.getPassword(body.getPassword());
    userDao.resetPassword(newPassword, body.getEmail());
}
Also used : User(com.fanxb.bookmark.common.entity.po.User) FormDataException(com.fanxb.bookmark.common.exception.FormDataException)

Example 4 with User

use of com.fanxb.bookmark.common.entity.po.User in project bookmark by FleyX.

the class UserServiceImpl method register.

/**
 * Description: 用户注册
 *
 * @param body 注册表单
 * @author fanxb
 * @date 2019/7/6 11:30
 */
public String register(RegisterBody body) {
    User user = userDao.selectByUsernameOrEmail(body.getUsername(), body.getEmail());
    if (user != null) {
        if (user.getUsername().equals(body.getUsername())) {
            throw new FormDataException("用户名已经被注册");
        }
        if (user.getEmail().equals(body.getEmail())) {
            throw new FormDataException("邮箱已经被注册");
        }
    }
    user = new User();
    user.setUsername(body.getUsername());
    user.setEmail(body.getEmail());
    user.setIcon(DEFAULT_ICON);
    user.setPassword(HashUtil.sha1(HashUtil.md5(body.getPassword())));
    user.setCreateTime(System.currentTimeMillis());
    user.setLastLoginTime(System.currentTimeMillis());
    user.setVersion(0);
    userDao.addOne(user);
    Map<String, String> data = new HashMap<>(1);
    data.put("userId", String.valueOf(user.getUserId()));
    return JwtUtil.encode(data, CommonConstant.jwtSecret, LONG_EXPIRE_TIME);
}
Also used : User(com.fanxb.bookmark.common.entity.po.User) FormDataException(com.fanxb.bookmark.common.exception.FormDataException)

Example 5 with User

use of com.fanxb.bookmark.common.entity.po.User in project bookmark by FleyX.

the class OauthServiceImpl method oAuthCheck.

@Override
@Transactional(rollbackFor = Exception.class)
public String oAuthCheck(OauthBody body) {
    User current, other = new User();
    if (StrUtil.equals(body.getType(), OauthBody.GITHUB)) {
        Map<String, String> header = new HashMap<>(2);
        header.put("accept", "application/json");
        String url = "https://github.com/login/oauth/access_token?client_id=" + githubClientId + "&client_secret=" + githubSecret + "&code=" + body.getCode();
        JSONObject obj = HttpUtil.getObj(url, header, true);
        String accessToken = obj.getString("access_token");
        if (StrUtil.isEmpty(accessToken)) {
            throw new CustomException("github登陆失败,请稍后重试");
        }
        header.put("Authorization", "token " + accessToken);
        JSONObject userInfo = HttpUtil.getObj("https://api.github.com/user", header, true);
        other.setGithubId(userInfo.getLong("id"));
        if (other.getGithubId() == null) {
            log.error("github返回异常:{}", userInfo);
            throw new CustomException("登陆异常,请稍后重试");
        }
        other.setEmail(userInfo.getString("email"));
        other.setIcon(userInfo.getString("avatar_url"));
        other.setUsername(userInfo.getString("login"));
        current = userDao.selectByUserIdOrGithubId(null, other.getGithubId());
        if (current == null) {
            current = userDao.selectByUsernameOrEmail(null, other.getEmail());
        }
    } else {
        throw new CustomException("不支持的登陆方式" + body.getType());
    }
    User newest = dealOauth(current, other);
    return JwtUtil.encode(Collections.singletonMap("userId", String.valueOf(newest.getUserId())), CommonConstant.jwtSecret, body.isRememberMe() ? LONG_EXPIRE_TIME : SHORT_EXPIRE_TIME);
}
Also used : User(com.fanxb.bookmark.common.entity.po.User) JSONObject(com.alibaba.fastjson.JSONObject) HashMap(java.util.HashMap) CustomException(com.fanxb.bookmark.common.exception.CustomException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

User (com.fanxb.bookmark.common.entity.po.User)6 FormDataException (com.fanxb.bookmark.common.exception.FormDataException)4 JSONObject (com.alibaba.fastjson.JSONObject)1 CustomException (com.fanxb.bookmark.common.exception.CustomException)1 HashMap (java.util.HashMap)1 Transactional (org.springframework.transaction.annotation.Transactional)1