| | |
| | | package cn.gistack.auth.service; |
| | | |
| | | import cn.gistack.auth.constant.AuthConstant; |
| | | import cn.gistack.common.utils.Base64Utils; |
| | | import cn.gistack.pwd.dto.CBCResultDTO; |
| | | import cn.gistack.pwd.dto.ResultDTO; |
| | | import cn.gistack.pwd.fegin.IPwdClient; |
| | | import cn.gistack.sm.collect.feign.ICollectClient; |
| | | import cn.gistack.sm.collect.feign.ICollectClientFallback; |
| | | import cn.gistack.sm.sjztmd.entity.AttResManagePerson; |
| | | import cn.gistack.sm.sjztmd.feign.IAttResManagePersonClient; |
| | | import cn.gistack.system.entity.Dept; |
| | | import cn.gistack.system.user.supo.UserSUPO; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.alibaba.nacos.common.utils.StringUtils; |
| | | import io.jsonwebtoken.Claims; |
| | | import lombok.AllArgsConstructor; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.time.Duration; |
| | | import java.util.Base64; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | public static final String FAIL_COUNT_VALUE = "account.failCount"; |
| | | |
| | | private final IUserClient userClient; |
| | | |
| | | private final IAttResManagePersonClient attResManagePersonClient; |
| | | private final ISysClient sysClient; |
| | | |
| | | private final BladeRedis bladeRedis; |
| | | private final JwtProperties jwtProperties; |
| | | |
| | | private final ICollectClient collectClient; |
| | | |
| | | private final IPwdClient pwdClient; |
| | | |
| | | @Override |
| | | @SneakyThrows |
| | |
| | | // 获取租户ID |
| | | String headerTenant = request.getHeader(TokenUtil.TENANT_HEADER_KEY); |
| | | String paramTenant = request.getParameter(TokenUtil.TENANT_PARAM_KEY); |
| | | String password = request.getParameter(TokenUtil.PASSWORD_KEY); |
| | | String grantType = request.getParameter(TokenUtil.GRANT_TYPE_KEY); |
| | | // 判断租户请求头 |
| | | if (StringUtil.isAllBlank(headerTenant, paramTenant)) { |
| | |
| | | // 用户不存在,但提示用户名与密码错误并锁定账号 |
| | | if (user == null || user.getId() == null) { |
| | | setFailCount(tenantId, username, count); |
| | | throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND); |
| | | } |
| | | // 用户存在但密码错误,超过次数则锁定账号 |
| | | if (grantType != null && !grantType.equals(TokenUtil.REFRESH_TOKEN_KEY) && !user.getPassword().equals(DigestUtil.hex(password))) { |
| | | setFailCount(tenantId, username, count); |
| | | throw new UsernameNotFoundException(TokenUtil.USER_NOT_FOUND); |
| | | throw new UserDeniedAuthorizationException(TokenUtil.USER_NOT_FOUND); |
| | | } |
| | | // 用户角色不存在 |
| | | if (Func.isEmpty(userInfo.getRoles())) { |
| | |
| | | } |
| | | user.setRoleId(headerRole); |
| | | } |
| | | // 成功则清除登录错误次数 |
| | | delFailCount(tenantId, username); |
| | | |
| | | //拼接用户所属行政区划、责任人需要返回水库ids |
| | | userInfo.getDetail().set("resIds",""); |
| | | if (StringUtil.isNotBlank(user.getPhone())) { |
| | | R<AttResManagePerson> attRes = attResManagePersonClient.queryListByPhone(user.getPhone()); |
| | | userInfo.getDetail().set("resIds",attRes.getData()); |
| | | } |
| | | |
| | | |
| | | String deptStr = ""; |
| | | if (userInfo.getUser().getDeptId().indexOf(",") > 0){ |
| | | deptStr = userInfo.getUser().getDeptId().split(",")[0]; |
| | | }else { |
| | | deptStr = userInfo.getUser().getDeptId(); |
| | | } |
| | | R<Dept> deptRes = sysClient.getDept(Long.valueOf(deptStr)); |
| | | if (StringUtil.isBlank(deptRes.getData().getAdCode())) { |
| | | // 部门为空 |
| | | // throw new UserDeniedAuthorizationException("没有找到有效组织机构~"); |
| | | userInfo.getDetail().set("adCodeBase", ""); |
| | | userInfo.getDetail().set("parentDept",0); |
| | | } else { |
| | | userInfo.getDetail().set("adCodeBase",attResManagePersonClient.getAdBaseById(deptRes.getData().getAdCode()).getData()); |
| | | userInfo.getDetail().set("parentDept",deptRes.getData().getParentId()); |
| | | } |
| | | |
| | | String collectResIds = collectClient.getMyCollect(user.getId().toString()); |
| | | userInfo.getDetail().set("collect",collectResIds); |
| | | |
| | | User loginUserInfo = userInfo.getUser(); |
| | | |
| | | //获取解密后的Base64字符串 |
| | | R<ResultDTO> encryptRes = pwdClient.getDecrypt(loginUserInfo.getSm4iv(), loginUserInfo.getKeyindexs(),loginUserInfo.getEncdata()); |
| | | |
| | | if (encryptRes.getCode() != 200){ |
| | | throw new UserDeniedAuthorizationException("获取密钥失败"); |
| | | } |
| | | |
| | | //解码Base64 |
| | | String base64Decode = Base64Utils.getBase64Decode(encryptRes.getData().getData()); |
| | | |
| | | UserSUPO userSUPO = JSONObject.parseObject(base64Decode, UserSUPO.class); |
| | | |
| | | //若登录用户的电话、角色、部门与原加密数据不符,登录失败 |
| | | if (!(loginUserInfo.getPhone().equals(userSUPO.getPhone()) && loginUserInfo.getRoleId().equals(userSUPO.getRoleId()) && loginUserInfo.getDeptId().equals(userSUPO.getDeptId())) ){ |
| | | throw new UserDeniedAuthorizationException("用户信息异常,请联系管理员"); |
| | | } |
| | | |
| | | return new BladeUserDetails(user.getId(), |
| | | user.getTenantId(), StringPool.EMPTY, user.getName(), user.getRealName(), user.getDeptId(), user.getPostId(), user.getRoleId(), Func.join(userInfo.getRoles()), Func.toStr(user.getAvatar(), TokenUtil.DEFAULT_AVATAR), |
| | | username, AuthConstant.ENCRYPT + user.getPassword(), userInfo.getDetail(), true, true, true, true, |