| | |
| | | @PostMapping(value = "/login", produces = "application/json; charset=UTF-8") |
| | | public ResponseMsg<TokenEntity> login(@RequestBody UserEntity user, HttpServletRequest req, HttpServletResponse res) { |
| | | try { |
| | | if (user == null) { |
| | | return fail("请输入用户名和密码!", null); |
| | | } |
| | | if (StringHelper.isEmpty(user.getUid())) { |
| | | return fail("用户名不能为空!", null); |
| | | } |
| | | if (StringHelper.isEmpty(user.getPwd())) { |
| | | return fail("密码不能为空!", null); |
| | | String str = userService.validateNewPwd(user); |
| | | if (str != null) { |
| | | return fail(str, null); |
| | | } |
| | | |
| | | // 解密 |
| | | String uid = RsaHelper.decrypt(user.getUid()); |
| | | String pwd = RsaHelper.decrypt(user.getPwd()); |
| | | |
| | | UserEntity ue = userService.selectByUid(uid); |
| | | UserEntity ue = userService.selectByUid(user.getUid()); |
| | | if (ue == null) { |
| | | return fail("用户名不存在!", null); |
| | | } |
| | | |
| | | LoginEntity le = loginService.getNewLogin(ue.getId(), 1, req); |
| | | if (!Md5Helper.validatePassword(pwd, ue.getPwd())) { |
| | | LoginEntity le = loginService.getNewLogin(ue.getId(), 1, 1, 1, req); |
| | | if (!Md5Helper.validatePassword(user.getPwd(), ue.getPwd())) { |
| | | tokenService.setPwdErrCache(ue); |
| | | le.setStatus(0); |
| | | le.setDescr("密码错误"); |
| | | loginService.insertLogin(le); |
| | |
| | | if (flag) { |
| | | // 写日志 |
| | | UserEntity ue = tokenService.getCurrentUser(req); |
| | | LoginEntity le = loginService.getNewLogin(ue.getId(), 2, req); |
| | | LoginEntity le = loginService.getNewLogin(ue.getId(), 1, 2, 1, req); |
| | | Integer rows = loginService.insertLogin(le); |
| | | } |
| | | |
| | |
| | | */ |
| | | public static void saveToken2Cookie(String token, HttpServletRequest request, HttpServletResponse response) { |
| | | // 先删除 |
| | | deleteAll(request, response); |
| | | deleteCookies(request, response); |
| | | |
| | | // 再保存 |
| | | saveCookie(StaticData.TOKEN_COOKIE_KEY, token, response); |
| | |
| | | * @param request |
| | | * @param response |
| | | */ |
| | | public static void deleteAll(HttpServletRequest request, HttpServletResponse response) { |
| | | public static void deleteCookies(HttpServletRequest request, HttpServletResponse response) { |
| | | Cookie[] cookies = request.getCookies(); |
| | | if (cookies != null && cookies.length > 0) { |
| | | for (Cookie c : cookies) { |
| | |
| | | return WebHelper.write2Page(response, NO_LOGIN); |
| | | } |
| | | |
| | | Object objCount = tokenService.redisService.get(RedisCacheKey.signPwdError(ue.getUid())); |
| | | if (objCount != null && (int) objCount > SettingData.PWD_ERR_COUNT) { |
| | | return WebHelper.write2Page(response, JSON.toJSONString(new ResponseMsg<String>(HttpStatus.NO_LOGIN_ERROR, SettingData.PWD_ERR_TIME + "分钟内禁止登录"))); |
| | | if (tokenService.isUidDisable(ue)) { |
| | | tokenService.logout(token, request, response); |
| | | return WebHelper.write2Page(response, JSON.toJSONString(new ResponseMsg<String>(HttpStatus.USER_LOCK_ERROR, SettingData.PWD_ERR_TIME + "分钟内禁止登录"))); |
| | | } |
| | | |
| | | // noinspection AlibabaRemoveCommentedCode |
| | |
| | | return loginMapper.selectLoginAll(); |
| | | } |
| | | |
| | | public LoginEntity getNewLogin(int userid, int type, HttpServletRequest req) { |
| | | /** |
| | | * 获取新的登录实体类 |
| | | */ |
| | | public LoginEntity getNewLogin(int userid, int appId, int type, int status, HttpServletRequest req) { |
| | | LoginEntity le = new LoginEntity(); |
| | | le.setAppid(1); |
| | | le.setUserid(userid); |
| | | le.setAppid(appId); |
| | | le.setIp(WebHelper.getIpAddress(req)); |
| | | le.setType(type); |
| | | le.setStatus(1); |
| | | le.setUserid(userid); |
| | | le.setStatus(status); |
| | | le.setOptime(WebHelper.getCurrentTimestamp()); |
| | | |
| | | return le; |
| | |
| | | return false; |
| | | } |
| | | |
| | | // 清除Cookie WebHelper.saveCookie(StaticData.TOKEN_COOKIE_KEY, "", 60, res) |
| | | WebHelper.deleteAll(req, res); |
| | | // 清除Cookie |
| | | WebHelper.deleteCookies(req, res); |
| | | |
| | | // 获取当前用户 |
| | | UserEntity ue = getCurrentUser(req); |
| | |
| | | |
| | | // 清除缓存 |
| | | String tokenKey = RedisCacheKey.signTokenKey(token); |
| | | redisService.delete(tokenKey); |
| | | if (redisService.hasKey(tokenKey)) { |
| | | redisService.delete(tokenKey); |
| | | } |
| | | String userKey = RedisCacheKey.signUserKey(te.getToken()); |
| | | redisService.delete(userKey); |
| | | if (redisService.hasKey(userKey)) { |
| | | redisService.delete(userKey); |
| | | } |
| | | |
| | | // db,设置令牌过期 |
| | | te.setUpdateUser(ue.getId()); |
| | |
| | | } |
| | | |
| | | // 写日志 |
| | | LoginEntity le = loginService.getNewLogin(ue.getId(), 3, req); |
| | | LoginEntity le = loginService.getNewLogin(ue.getId(), 1, 3, 1, req); |
| | | rows = loginService.insertLogin(le); |
| | | |
| | | return rows > 0; |
| | |
| | | |
| | | /** |
| | | * 获取当前用户 |
| | | * |
| | | * @param req |
| | | * @return |
| | | */ |
| | | public UserEntity getCurrentUser(HttpServletRequest req) { |
| | | String token = WebHelper.getToken(req); |
| | |
| | | |
| | | return ue; |
| | | } |
| | | |
| | | /** |
| | | * 设置密码错误缓存 |
| | | */ |
| | | public void setPwdErrCache(UserEntity ue) { |
| | | String key = RedisCacheKey.signPwdError(ue.getUid()); |
| | | Object objCount = redisService.get(key); |
| | | |
| | | int count = objCount == null ? 1 : (int) objCount + 1; |
| | | redisService.put(key, count, SettingData.PWD_ERR_TIME, TimeUnit.MINUTES); |
| | | } |
| | | |
| | | /** |
| | | * 用户ID是/否禁用 |
| | | */ |
| | | public boolean isUidDisable(UserEntity ue) { |
| | | String key = RedisCacheKey.signPwdError(ue.getUid()); |
| | | Object objCount = redisService.get(key); |
| | | |
| | | return objCount != null && (int) objCount >= SettingData.PWD_ERR_COUNT; |
| | | } |
| | | |
| | | public void logoffToken(String token) { |
| | | // |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 验证新用户密码 |
| | | * 验证用户实体类 |
| | | */ |
| | | public String validateNewPwd(UserEntity entity) { |
| | | public String validateUserEntity(UserEntity entity) { |
| | | if (entity == null) { |
| | | return "没有找到用户"; |
| | | } |
| | | if (StringHelper.isEmpty(entity.getUid())) { |
| | | return "用户ID不能为空"; |
| | | } |
| | | if (StringHelper.isEmpty(entity.getPwd())) { |
| | | return "密码不能为空"; |
| | | return "用户密码不能为空"; |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 验证新用户密码 |
| | | */ |
| | | public String validateNewPwd(UserEntity entity) { |
| | | try { |
| | | String str = validateUserEntity(entity); |
| | | if (str != null) { |
| | | return str; |
| | | } |
| | | |
| | | // 解密 |
| | | String uid = RsaHelper.decrypt(entity.getUid()); |
| | | String pwd = RsaHelper.decrypt(entity.getPwd()); |
| | | if (!StringHelper.checkPwdValid(pwd)) { |
| | | return "密码不符合要求"; |
| | |
| | | |
| | | String newPwd = Md5Helper.reverse(Md5Helper.generate(pwd)); |
| | | entity.setPwd(newPwd); |
| | | entity.setUid(uid); |
| | | |
| | | return null; |
| | | } catch (Exception ex) { |
| | |
| | | * 验证老用户密码 |
| | | */ |
| | | public String validateOldPwd(UserEntity entity) { |
| | | if (entity == null) { |
| | | return "没有找到用户"; |
| | | } |
| | | if (StringHelper.isEmpty(entity.getPwd())) { |
| | | return "用户密码不能为空"; |
| | | } |
| | | |
| | | try { |
| | | String str = validateUserEntity(entity); |
| | | if (str != null) { |
| | | return str; |
| | | } |
| | | if (StringHelper.isEmpty(entity.getSalt())) { |
| | | return "新密码不能为空"; |
| | | } |
| | | |
| | | String uid = RsaHelper.decrypt(entity.getUid()); |
| | | String pwd = RsaHelper.decrypt(entity.getPwd()); |
| | | String salt = RsaHelper.decrypt(entity.getSalt()); |
| | | |
| | | UserEntity ue = selectUser(entity.getId()); |
| | | UserEntity ue = selectByUid(uid); |
| | | if (ue == null) { |
| | | return "没有找到用户"; |
| | | } |
| | |
| | | String newPwd = Md5Helper.reverse(Md5Helper.generate(salt)); |
| | | entity.setPwd(newPwd); |
| | | entity.setSalt(null); |
| | | entity.setUid(uid); |
| | | |
| | | return null; |
| | | } catch (Exception ex) { |
| | |
| | | } catch (Exception ex) { |
| | | return "密码解密失败"; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 设置密码错误缓存 |
| | | * |
| | | * @param ue |
| | | */ |
| | | public void setPwdErrCache(UserEntity ue) { |
| | | String key = RedisCacheKey.signPwdError(ue.getUid()); |
| | | Object objCount = redisService.get(key); |
| | | |
| | | int count = objCount == null ? 1 : (int) objCount + 1; |
| | | redisService.put(key, count, SettingData.PWD_ERR_TIME, TimeUnit.MINUTES); |
| | | } |
| | | } |