Search in sources :

Example 6 with User

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

the class UserServiceImpl method login.

/**
 * Description: 登录
 *
 * @param body 登录表单
 * @return string
 * @author fanxb
 * @date 2019/7/6 16:37
 */
public String login(LoginBody body) {
    String key = RedisConstant.getUserFailCountKey(body.getStr());
    String count = redisTemplate.opsForValue().get(key);
    if (count != null && Integer.parseInt(count) >= LOGIN_COUNT) {
        redisTemplate.expire(key, 30, TimeUnit.MINUTES);
        throw new FormDataException("您已连续输错密码5次,请30分钟后再试,或联系管理员处理");
    }
    User userInfo = userDao.selectByUsernameOrEmail(body.getStr(), body.getStr());
    if (userInfo == null || StrUtil.isEmpty(userInfo.getPassword()) || !HashUtil.sha1(HashUtil.md5(body.getPassword())).equals(userInfo.getPassword())) {
        redisTemplate.opsForValue().set(key, count == null ? "1" : String.valueOf(Integer.parseInt(count) + 1), 30, TimeUnit.MINUTES);
        throw new FormDataException("账号密码错误");
    }
    redisTemplate.delete(key);
    userDao.updateLastLoginTime(System.currentTimeMillis(), userInfo.getUserId());
    return JwtUtil.encode(Collections.singletonMap("userId", String.valueOf(userInfo.getUserId())), CommonConstant.jwtSecret, body.isRememberMe() ? LONG_EXPIRE_TIME : SHORT_EXPIRE_TIME);
}
Also used : User(com.fanxb.bookmark.common.entity.po.User) FormDataException(com.fanxb.bookmark.common.exception.FormDataException)

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