|
@@ -17,6 +17,7 @@ import com.diagbot.entity.SysUserRole;
|
|
|
import com.diagbot.entity.wrapper.SysMenuWrapper;
|
|
|
import com.diagbot.enums.ConstantEnum;
|
|
|
import com.diagbot.enums.IsDeleteEnum;
|
|
|
+import com.diagbot.enums.LockEnum;
|
|
|
import com.diagbot.enums.StatusEnum;
|
|
|
import com.diagbot.exception.CommonErrorCode;
|
|
|
import com.diagbot.exception.CommonException;
|
|
@@ -24,6 +25,7 @@ import com.diagbot.exception.ServiceErrorCode;
|
|
|
import com.diagbot.service.impl.SysUserDeptServiceImpl;
|
|
|
import com.diagbot.service.impl.SysUserRoleServiceImpl;
|
|
|
import com.diagbot.service.impl.SysUserServiceImpl;
|
|
|
+import com.diagbot.task.LockTask;
|
|
|
import com.diagbot.util.BeanUtil;
|
|
|
import com.diagbot.util.DateUtil;
|
|
|
import com.diagbot.util.EntityUtil;
|
|
@@ -52,9 +54,9 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.servlet.http.HttpSession;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
-import java.io.ByteArrayOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
@@ -101,8 +103,8 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
private SysHospitalSetFacade sysHospitalSetFacade;
|
|
|
@Autowired
|
|
|
private RedisUtils redisUtils;
|
|
|
-
|
|
|
-
|
|
|
+ @Autowired
|
|
|
+ private LockTask lockTask;
|
|
|
|
|
|
|
|
|
public void getCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
@@ -125,6 +127,7 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
|
|
|
/**
|
|
|
* 获取标识--选择登录页面
|
|
|
+ *
|
|
|
* @return java.lang.Long
|
|
|
*/
|
|
|
public Long getHospitalMark() {
|
|
@@ -133,16 +136,18 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
.eq(SysHospitalSet::getHospitalId, 35)
|
|
|
.eq(SysHospitalSet::getIsDeleted, IsDeleteEnum.N.getKey())
|
|
|
.eq(SysHospitalSet::getCode, "special_page_csxy").one();
|
|
|
- if(null != sysHospitalSet){
|
|
|
+ if (null != sysHospitalSet) {
|
|
|
String val = sysHospitalSet.getValue();
|
|
|
- if(StringUtil.isNotBlank(val) && "1".equals(val)){
|
|
|
- mark = Long.valueOf(val);
|
|
|
+ if (StringUtil.isNotBlank(val) && "1".equals(val)) {
|
|
|
+ mark = Long.valueOf(val);
|
|
|
}
|
|
|
}
|
|
|
return mark;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 获取jwt
|
|
|
+ *
|
|
|
* @param username 用户名
|
|
|
* @param password 密码
|
|
|
* @return jwt
|
|
@@ -161,12 +166,10 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
|
|
|
"请输入验证码");
|
|
|
}
|
|
|
+
|
|
|
// 验证码校验
|
|
|
String captchaId = request.getSession().getId();
|
|
|
- Object captchaObject = redisUtils.get("user:captchaIds:" + captchaId);
|
|
|
- if (null == captchaObject || StringUtil.isBlank(captchaObject.toString()) || !captchaObject.toString().trim().equalsIgnoreCase(captcha)) {
|
|
|
- throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "验证码错误");
|
|
|
- }
|
|
|
+ checkCaptcha(captchaId, captcha, redisUtils);
|
|
|
//使用MD5对密码进行加密
|
|
|
String MD5Password = DigestUtils.md5DigestAsHex(password.getBytes());
|
|
|
QueryWrapper<SysUser> userQueryWrapper = new QueryWrapper<>();
|
|
@@ -174,13 +177,46 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
.eq("status", StatusEnum.Enable.getKey())
|
|
|
.eq("is_deleted", IsDeleteEnum.N.getKey());
|
|
|
SysUser user = this.getOne(userQueryWrapper, false);
|
|
|
+ //判断用户是否存在
|
|
|
if (null == user) {
|
|
|
throw new CommonException(ServiceErrorCode.USER_NOT_FOUND);
|
|
|
}
|
|
|
+ //获取用户所在医院
|
|
|
+ Long id = user.getId();
|
|
|
+ QueryWrapper<SysUserHospital> UserHospitalQueryWrapper = new QueryWrapper<>();
|
|
|
+ UserHospitalQueryWrapper
|
|
|
+ .eq("user_id", id)
|
|
|
+ .eq("is_deleted", IsDeleteEnum.N.getKey());
|
|
|
+ SysUserHospital userHospital = sysUserHospitalFacade.getOne(UserHospitalQueryWrapper, false);
|
|
|
+ Long hospitalId = userHospital.getHospitalId();
|
|
|
+
|
|
|
+ //判断医院是否启用用户锁定校验
|
|
|
+ QueryWrapper<SysHospitalSet> hospitalSetQueryWrapper = new QueryWrapper<>();
|
|
|
+ hospitalSetQueryWrapper.eq("is_deleted", 'N')
|
|
|
+ .eq("hospital_id", hospitalId)
|
|
|
+ .eq("code", "lock_user");
|
|
|
+ SysHospitalSet hospitalSet = sysHospitalSetFacade.getOne(hospitalSetQueryWrapper);
|
|
|
+ boolean lockFlag = false;
|
|
|
+ if (hospitalSet != null) {
|
|
|
+ lockFlag = Boolean.parseBoolean(hospitalSet.getValue());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //判断用户是否锁定
|
|
|
+ if (lockFlag) {
|
|
|
+ lockCheck(redisUtils, user);
|
|
|
+ }
|
|
|
+ //密码是否正确
|
|
|
PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
|
|
|
if (!passwordEncoder.matches(MD5Password, user.getPassword())) {
|
|
|
- throw new CommonException(ServiceErrorCode.USER_PASSWORD_ERROR);
|
|
|
+ if (!lockFlag) {
|
|
|
+ throw new CommonException(ServiceErrorCode.USER_PASSWORD_ERROR);
|
|
|
+ } else {
|
|
|
+ //用户获取错误次数
|
|
|
+ lockNumCheck(redisUtils, user, sysDictionaryFacade);
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
JWT jwt = authServiceClient.getToken("Basic dWFhLXNlcnZpY2U6MTIzNDU2",
|
|
|
"password", username, MD5Password);
|
|
|
if (null == jwt) {
|
|
@@ -195,15 +231,12 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
try {
|
|
|
SysUserBaseVO sysUserBaseVO = new SysUserBaseVO();
|
|
|
sysUserBaseVO.setUserId(user.getId());
|
|
|
- List<SysRoleDTO> selRoles =getlocalUserRoles(sysUserBaseVO);
|
|
|
- if(selRoles!=null&&!selRoles.isEmpty())
|
|
|
- {
|
|
|
+ List<SysRoleDTO> selRoles = getlocalUserRoles(sysUserBaseVO);
|
|
|
+ if (selRoles != null && !selRoles.isEmpty()) {
|
|
|
data.setSelRoles(selRoles);
|
|
|
}
|
|
|
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
+ } catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
//token存入redis
|
|
@@ -214,23 +247,124 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
/***
|
|
|
* 未经过MD5加密密码复杂度判断
|
|
|
*/
|
|
|
- // 获取用户医院id
|
|
|
- // String hospitalID = SysUserUtils.getCurrentHospitalID();
|
|
|
- Long id = user.getId();
|
|
|
- QueryWrapper<SysUserHospital> UserHospitalQueryWrapper = new QueryWrapper<>();
|
|
|
- UserHospitalQueryWrapper
|
|
|
- .eq("user_id", id)
|
|
|
- .eq("is_deleted", IsDeleteEnum.N.getKey());
|
|
|
- SysUserHospital userHospital = sysUserHospitalFacade.getOne(UserHospitalQueryWrapper, false);
|
|
|
- Long hospitalId = userHospital.getHospitalId();
|
|
|
+ // 获取用户医院id
|
|
|
+ // String hospitalID = SysUserUtils.getCurrentHospitalID();
|
|
|
+
|
|
|
String idStr = String.valueOf(hospitalId);
|
|
|
- Boolean passwordRegular = passwordRegular(password,idStr);
|
|
|
- if(!passwordRegular){
|
|
|
+ Boolean passwordRegular = passwordRegular(password, idStr);
|
|
|
+ if (!passwordRegular) {
|
|
|
data.setPasswordComplexity("未修改初始密码,请及时修改密码");
|
|
|
}
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @param redisUtils
|
|
|
+ * @param user
|
|
|
+ * @param sysDictionaryFacade
|
|
|
+ * @Description锁定次数校验
|
|
|
+ * @Return void
|
|
|
+ */
|
|
|
+ private void lockNumCheck(RedisUtils redisUtils, SysUser user, SysDictionaryFacade sysDictionaryFacade) {
|
|
|
+ //获取锁定时间、解锁时间配置
|
|
|
+ int unlockTime = 180;
|
|
|
+ int lockTime = 60;
|
|
|
+ int lockNum = 5;
|
|
|
+ if (sysDictionaryFacade.getDictionaryWithKey() != null
|
|
|
+ && sysDictionaryFacade.getDictionaryWithKey().containsKey("31")
|
|
|
+ && sysDictionaryFacade.getDictionaryWithKey().get("31").containsKey("unlockTime")
|
|
|
+ && sysDictionaryFacade.getDictionaryWithKey().get("31").containsKey("lockNum")
|
|
|
+ && sysDictionaryFacade.getDictionaryWithKey().get("31").containsKey("lockTime")) {
|
|
|
+ unlockTime = Integer.parseInt(sysDictionaryFacade.getDictionaryWithKey().get("31").get("unlockTime"));
|
|
|
+ lockTime = Integer.parseInt(sysDictionaryFacade.getDictionaryWithKey().get("31").get("lockTime"));
|
|
|
+ lockNum = Integer.parseInt(sysDictionaryFacade.getDictionaryWithKey().get("31").get("lockNum"));
|
|
|
+ }
|
|
|
+
|
|
|
+ Object numObj = redisUtils.get("user:pasError_" + user.getId());
|
|
|
+ int num = 1;
|
|
|
+ if (numObj != null) {
|
|
|
+ num = (Integer) numObj;
|
|
|
+ num = num + 1;
|
|
|
+ if (num < lockNum) {
|
|
|
+ redisUtils.opsForValue("user:pasError_" + user.getId(), num);
|
|
|
+ } else {
|
|
|
+ //锁定账号
|
|
|
+ lockUser(user.getId(), LockEnum.LOCK.getKey());
|
|
|
+ //启动定时任务解锁
|
|
|
+ runTimerTask(user.getId(), LockEnum.UNLOCK.getKey(), unlockTime);
|
|
|
+ //redis存入解锁账号(用户解锁剩余时间)
|
|
|
+ redisUtils.set("user:lockTime_" + user.getId(), "lock", unlockTime);
|
|
|
+ //获取锁定时间
|
|
|
+ getLockError(user.getId(), redisUtils);
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ redisUtils.set("user:pasError_" + user.getId(), num, lockTime);
|
|
|
+ }
|
|
|
+ String error = "账号或密码不正确,剩%s次机会,之后账号将被锁定10分钟";
|
|
|
+ throw new CommonException(ServiceErrorCode.USER_PASSWORD_ERROR, String.format(error, lockNum - num));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param redisUtils
|
|
|
+ * @param user
|
|
|
+ * @Description锁定校验
|
|
|
+ * @Return void
|
|
|
+ */
|
|
|
+ private void lockCheck(RedisUtils redisUtils, SysUser user) {
|
|
|
+ if (LockEnum.LOCK.getKey().equals(user.getLocked())) {
|
|
|
+ //获取锁定时间
|
|
|
+ getLockError(user.getId(), redisUtils);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param captchaId
|
|
|
+ * @param captcha
|
|
|
+ * @param redisUtils
|
|
|
+ * @Description验证码校验
|
|
|
+ * @Return void
|
|
|
+ */
|
|
|
+ private void checkCaptcha(String captchaId, String captcha, RedisUtils redisUtils) {
|
|
|
+ Object captchaObject = redisUtils.get("user:captchaIds:" + captchaId);
|
|
|
+ if (null == captchaObject || StringUtil.isBlank(captchaObject.toString()) || !captchaObject.toString().trim().equalsIgnoreCase(captcha)) {
|
|
|
+ throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "验证码错误");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void getLockError(Long id, RedisUtils redisUtils) {
|
|
|
+ Long endTime = redisUtils.getExpire("user:lockTime_" + id);
|
|
|
+ long mint = endTime / 60;
|
|
|
+ if (endTime % 60 > 0) {
|
|
|
+ mint = mint + 1;
|
|
|
+ }
|
|
|
+ if (endTime >= 0) {
|
|
|
+ String error = "账号已被锁定,%s分钟后将自动解锁或联系管理员解锁";
|
|
|
+ throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, String.format(error, mint));
|
|
|
+ }
|
|
|
+ throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "用户被锁定,自动解锁异常请联系管理员!!!");
|
|
|
+ }
|
|
|
+
|
|
|
+ private void runTimerTask(Long id, String lock, int time) {
|
|
|
+ lockTask.getMScheduledExecutorService().schedule(new Runnable() {
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ lockUser(id, lock);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }, time, TimeUnit.SECONDS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean lockUser(Long id, String key) {
|
|
|
+ SysUser user = new SysUser();
|
|
|
+ user.setId(id);
|
|
|
+ user.setLocked(key);
|
|
|
+ return this.updateById(user);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 外部获取jwt
|
|
|
*
|
|
@@ -337,8 +471,8 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
}
|
|
|
//对传入的密码进行格式验证
|
|
|
String hospitalID = SysUserUtils.getCurrentHospitalID();
|
|
|
- Boolean regularBoolean = passwordRegular(modifyPassword,hospitalID);
|
|
|
- if(!regularBoolean){
|
|
|
+ Boolean regularBoolean = passwordRegular(modifyPassword, hospitalID);
|
|
|
+ if (!regularBoolean) {
|
|
|
throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "请输入正确格式的新密码");
|
|
|
}
|
|
|
String userId = SysUserUtils.getCurrentPrincipleID();
|
|
@@ -368,23 +502,25 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
|
|
|
/**
|
|
|
* 未加密密文正则表达式 至少8个字符,1个大写字母,1个小写字母,1个数字和1个特殊字符:
|
|
|
+ *
|
|
|
* @param password
|
|
|
* @return
|
|
|
*/
|
|
|
- public Boolean passwordRegular(String password,String hospitalId){
|
|
|
- boolean check=true;
|
|
|
+ public Boolean passwordRegular(String password, String hospitalId) {
|
|
|
+ boolean check = true;
|
|
|
Map<String, Map<String, String>> dictionaryWithKey = sysDictionaryFacade.getDictionaryWithKey();
|
|
|
- if(dictionaryWithKey!=null){
|
|
|
+ if (dictionaryWithKey != null) {
|
|
|
Map<String, String> stringStringMap = dictionaryWithKey.get("30");
|
|
|
- if(stringStringMap!=null) {
|
|
|
+ if (stringStringMap != null) {
|
|
|
String regular = stringStringMap.get(hospitalId);
|
|
|
- if(StringUtil.isNotEmpty(regular)) {
|
|
|
+ if (StringUtil.isNotEmpty(regular)) {
|
|
|
check = password.matches(regular);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return check;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 登录
|
|
|
*
|
|
@@ -399,13 +535,13 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
.eq("status", StatusEnum.Enable.getKey())
|
|
|
.eq("id", userId), false);
|
|
|
QueryWrapper<SysUserRole> sysUserRoleQueryWrapper = new QueryWrapper<>();
|
|
|
- sysUserRoleQueryWrapper.eq("user_id",userId);
|
|
|
- sysUserRoleQueryWrapper .eq("is_deleted", IsDeleteEnum.N.getKey());
|
|
|
+ sysUserRoleQueryWrapper.eq("user_id", userId);
|
|
|
+ sysUserRoleQueryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey());
|
|
|
List<SysUserRole> sysUserRoleList = sysUserRoleFacade.list(sysUserRoleQueryWrapper);
|
|
|
- if(ListUtil.isNotEmpty(sysUserRoleList)){
|
|
|
- sysUserRoleList.forEach(sysUserRole ->{
|
|
|
- roleSet.add(sysUserRole.getRoleId()+"");
|
|
|
- } );
|
|
|
+ if (ListUtil.isNotEmpty(sysUserRoleList)) {
|
|
|
+ sysUserRoleList.forEach(sysUserRole -> {
|
|
|
+ roleSet.add(sysUserRole.getRoleId() + "");
|
|
|
+ });
|
|
|
}
|
|
|
if (user == null) {
|
|
|
throw new CommonException(CommonErrorCode.SERVER_IS_ERROR,
|
|
@@ -428,8 +564,8 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
}
|
|
|
|
|
|
//添加菜单信息
|
|
|
- List<SysMenuWrapper> menuList = sysMenuFacade.getByRole(user.getId(),roleSet);
|
|
|
- List<SysUserPermissionDTO> sysUserPermissionDTOList = sysMenuFacade.getByRolePermission(user.getId(),roleSet);
|
|
|
+ List<SysMenuWrapper> menuList = sysMenuFacade.getByRole(user.getId(), roleSet);
|
|
|
+ List<SysUserPermissionDTO> sysUserPermissionDTOList = sysMenuFacade.getByRolePermission(user.getId(), roleSet);
|
|
|
Map<Long, List<SysMenuWrapper>> menuMap = EntityUtil.makeEntityListMap(menuList, "parentId");
|
|
|
Map<Long, List<SysUserPermissionDTO>> menuPermissionMap = EntityUtil.makeEntityListMap(sysUserPermissionDTOList, "menuId");
|
|
|
List<SysMenuWrapper> menuRes = menuMap.get(-1L);
|
|
@@ -585,6 +721,7 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
|
|
|
return sysUserRoleDTO;
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 获取用户角色
|
|
|
*
|
|
@@ -766,6 +903,23 @@ public class SysUserFacade extends SysUserServiceImpl {
|
|
|
throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "该医院下无该用户");
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
//-------------用户维护END---------------------------
|
|
|
|
|
|
+ /**
|
|
|
+ * @param
|
|
|
+ * @Description登出功能
|
|
|
+ * @Return java.lang.Boolean
|
|
|
+ */
|
|
|
+ public Boolean logout() {
|
|
|
+ try {
|
|
|
+ //清除缓存
|
|
|
+
|
|
|
+ //销毁token
|
|
|
+ redisUtils.del("user_tokens_" + SysUserUtils.getCurrentPrincipleID());
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new CommonException(CommonErrorCode.FAIL, "登出失败");
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|