1.json转为对应的泛型集合
List<String> resourceList = JSON.parseArray(JSON.toJSONString(obj), String.class);
2.集合转换为数组
String[] roles = (String[])resourceList.toArray(new String[0]);
3.json转换为对应的javabean
SLoginRule loginRule = (SLoginRule)JSONUtil.parseObj(this.cacheCommonDateService.getLoginRule()).toBean(SLoginRule.class);
4.校验注解
* @return
*/
@PostMapping("/login")
public JsonObject<Object> login(@Valid LoginUserInfo userInfo, BindingResult results, HttpServletRequest request) {
if (results.hasErrors()) {
return new JsonErrorObject(((FieldError) Objects.requireNonNull(results.getFieldError())).getDefaultMessage());
} else {
SLoginRule loginRule = (SLoginRule)JSONUtil.parseObj(this.cacheDataService.getLoginRule()).toBean(SLoginRule.class);
if (loginRule == null) {
loginRule = this.loginRuleService.cacheData();
}
if (loginRule.getShowcaptcha() == 1) {
if (StrUtil.isBlank(userInfo.getVerifyCode())) {
return new JsonErrorObject("验证码不能为空");
}
if (!this.verificationCodeService.checkCaptcha(userInfo.getVerifyCodeKey(), userInfo.getVerifyCode())) {
return new JsonErrorObject(this.verificationCodeService.getCaptcha(userInfo.getVerifyCodeKey()), APPEnums.CAPTCHA_ERR.getCode(), "验证码输入错误");
}
}
SAuUser user = this.isAuUserService.getUserByName(userInfo.getUserName());
if (user == null) {
LoginStrategyService strategyService = LoginFactory.getStrategyService(this.authorUrlProperties.getLoginExtension());
if (strategyService != null) {
user = strategyService.getUserInfoByUserName(userInfo.getUserName());
}
}
if (user == null) {
return new JsonErrorObject(APPEnums.ERROR.getCode(), "用户名或密码错误");
} else {
String originPassword;
if ((StringUtility.isNullOrEmpty(loginRule.getFirstchange()) || loginRule.getFirstchange() == 1) && StringUtility.isNullOrEmpty(user.getLastUpdatePasswordTime())) {
originPassword = IdUtil.fastSimpleUUID();
this.cacheDataService.saveInfoToCache(this.cacheProperties.getFirstLoginAccountPrefix() + ":" + originPassword, user.getId(), this.cacheProperties.getTempCacheExpirationTime());
return new JsonErrorObject(originPassword, APPEnums.CHANGE_PASSWORD.getCode(), APPEnums.CHANGE_PASSWORD.getMessage());
} else {
long betweenDay;
if (!StringUtility.isNullOrEmpty(loginRule.getChangepassword()) && loginRule.getChangepassword() > 0 && (userInfo.getIgnoreExpValid() == null || userInfo.getIgnoreExpValid() != 1)) {
Date lastUpdatePasswordTime = user.getLastUpdatePasswordTime();
if (lastUpdatePasswordTime == null) {
lastUpdatePasswordTime = user.getCreateTime();
}
betweenDay = DateUtil.between(lastUpdatePasswordTime, new Date(), DateUnit.DAY);
if (betweenDay >= (long)loginRule.getChangepassword()) {
String uuid = IdUtil.fastSimpleUUID();
SConfigSystem sConfigSystemValidity = this.sConfigSystemService.getConfigSystemByCode("com.bdsoft.rcgl.password.isLogin",null);
this.cacheDataService.saveInfoToCache(this.cacheProperties.getFirstLoginAccountPrefix() + ":" + uuid, user.getId(), this.cacheProperties.getTempCacheExpirationTime());
return new JsonErrorObjectVo(uuid,APPEnums.PWD_BE_OVERDUE.getCode(), StrUtil.format("您的密码已超过{}天未进行修改密码,请及时修改密码", new Object[]{betweenDay}),sConfigSystemValidity.getConfigValue());
}
}
if (loginRule.getLogintype() != null && loginRule.getLogintype() == 1) {
List<String> tokenIds = this.cacheDataService.getTokenIdByUseId(user.getId());
if (CollectionUtil.isNotEmpty(tokenIds)) {
boolean loginRecord = this.cacheDataService.judgeLoginAccount(userInfo.getUserName());
if (StringUtility.isNullOrEmpty(userInfo.getIsLogout())) {
if (!loginRecord) {
this.cacheDataService.setLoginAccount(userInfo.getUserName(), this.cacheProperties.getTempCacheExpirationTime());
}
return new JsonErrorObject(APPEnums.FORCE_LOGIN.getCode(), userInfo.getUserName() + "用户已在线,是否强制下线");
}
if (!StringUtility.isNullOrEmpty(userInfo.getIsLogout()) && userInfo.getIsLogout() == 1) {
if (tokenIds != null && tokenIds.size() > 0) {
this.cacheDataService.removeUserTokenByUserId(user.getId());
}
} else if (!StringUtility.isNullOrEmpty(userInfo.getIsLogout()) && userInfo.getIsLogout() == 2) {
return new JsonErrorObject(APPEnums.FORCE_LOGIN_NO.getCode(), "你已取消了登录操作");
}
}
}
if ((this.nodeProperties.getOpenVerification() || this.nodeProperties.getFailureTimeVerification()) && StrUtil.isNotBlank(user.getNodeId())) {
STreeNode treeNode = (STreeNode)this.treeNodeService.getById(user.getNodeId());
if (Objects.isNull(treeNode) && user.getUserType() != 4) {
return new JsonErrorObject("您的账号所在节点不存在,请联系管理员进行查看");
}
if (treeNode != null) {
if (this.nodeProperties.getOpenVerification() && treeNode.getPrivateClound() != 1) {
return new JsonErrorObject(StrUtil.isNotBlank(this.nodeProperties.getOpenVerificationMessage()) ? this.nodeProperties.getOpenVerificationMessage() : "您所在的节点未开通或已停用,请联系管理员进行开通!");
}
if (this.nodeProperties.getFailureTimeVerification() && user.getUserType() == 5 && treeNode.getFailureTime() != null) {
betweenDay = DateUtil.between(DateUtil.beginOfDay(new Date()), treeNode.getFailureTime(), DateUnit.DAY, false);
if (betweenDay <= 0L) {
return new JsonErrorObject(StrUtil.isNotBlank(this.nodeProperties.getFailureTimeVerificationMessage()) ? this.nodeProperties.getFailureTimeVerificationMessage() : "您的账号已失效!");
}
}
}
}
if (this.systemConfigProperties.getFailureTimeVerification() && user.getFailureDate() != null) {
betweenDay = DateUtil.between(DateUtil.beginOfDay(new Date()), user.getFailureDate(), DateUnit.DAY, false);
if (betweenDay <= 0L) {
return new JsonErrorObject(StrUtil.isNotBlank(this.systemConfigProperties.getFailureTimeVerificationMessage()) ? this.systemConfigProperties.getFailureTimeVerificationMessage() : "您的账号已失效!");
}
}
originPassword = this.userService.getOriginPwd(userInfo.getPassWord());
UsernamePasswordToken token = new UsernamePasswordToken(userInfo.getUserName(), originPassword);
Subject currentUser = SecurityUtils.getSubject();
currentUser.getSession().setTimeout(-1000L);
try {
currentUser.login(token);
} catch (IncorrectCredentialsException var10) {
log.info("登录密码{}错误", userInfo.getPassWord());
this.userService.pwdErrorHandler(userInfo.getUserName(), loginRule);
if (loginRule.getShowcaptcha() == 1) {
return new JsonErrorObject(this.verificationCodeService.getCaptcha(userInfo.getVerifyCodeKey()), APPEnums.CAPTCHA_ERR.getCode(), "用户名或密码错误");
}
return new JsonErrorObject("用户名或密码错误");
}
this.cacheDataService.removeLoginAccount(userInfo.getUserName());
Map<String, Object> dataMap = this.userService.onLoginSuccess(request, currentUser);
return new JsonSuccessObject(dataMap, "登录成功");
}
}
}
}
5.不写泛型的操作(有空要试一试)
/**
* 单点登录所有配置,前端通过获取参数实现跳转,前端不写死
* @return
*/
@ApiOperation(value = "获取单点登录相关配置")
@PostMapping(Urls.SConfigSystem.GET_SSO_CONFIG)
public JsonObject getSsoConfig() {
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("CONFIGGROUP", "singleLogin");
List<SConfigSystem> sConfigSystemList = configSystemService.list(queryWrapper);
Map map = new HashMap();
for (SConfigSystem sConfigSystem : sConfigSystemList) {
map.put(sConfigSystem.getConfigCode(), sConfigSystem);
}
return new JsonSuccessObject(map, "查询配置成功");
}
66.迭代器操作可以试一试
@Override
public List<String> getRoleFunctionUrlsByRoleID(String roleID) {
List<SAuRoleFunction> sAuRoleFunctionList = this.getRoleFunctionsByRoleID(roleID);
List<String> stringList = new ArrayList();
Iterator var4 = sAuRoleFunctionList.iterator();
while(var4.hasNext()) {
SAuRoleFunction sAuRoleFunction = (SAuRoleFunction)var4.next();
SAuFunction sAuFunction = this.isAuFunctionService.getFunctionByID(sAuRoleFunction.getFunctionId());
stringList.add(sAuFunction.getUrl());
}
return stringList;
}
67.可变参数,配合断言
@Override
public JsonObject<Object> assignFunction(String userId, String roleId, String... functionIds) {
List<SLogDetail> sLogDetailList = new ArrayList();
Assert.notNull(roleId, "角色ID不能为空");
Assert.notNull(functionIds, "功能ID不能为空");
SAuRole sAuRole = (SAuRole)this.roleService.getById(roleId);
Assert.notNull(sAuRole, "角色不存在!");
Set<String> newFunIdSet = new HashSet(ListUtil.toList(functionIds));
Set<String> topFunIds = new HashSet();
this.getTopFunId(newFunIdSet, topFunIds);
String[] menuIds = this.getMenuIds(roleId);
Set<String> disableFunctionIds = new HashSet();
String menuId;
if (menuIds != null && menuIds.length > 0) {
String[] var10 = menuIds;
int var11 = menuIds.length;
for(int var12 = 0; var12 < var11; ++var12) {
menuId = var10[var12];
if (newFunIdSet.contains(menuId)) {
newFunIdSet.remove(menuId);
} else if (!topFunIds.contains(menuId)) {
disableFunctionIds.add(menuId);
}
}
}
RoleFunctionStrategyService roleFunctionStrategyService = RoleFunctionFactory.getStrategyService();
if (roleFunctionStrategyService != null && CollectionUtil.isNotEmpty(disableFunctionIds)) {
LambdaUpdateWrapper<SAuRoleFunction> queryWrapper = new LambdaUpdateWrapper();
queryWrapper.eq(SAuRoleFunction::getRoleId, roleId);
queryWrapper.in(SAuRoleFunction::getFunctionId, disableFunctionIds);
List<SAuRoleFunction> brforeDataList = this.list(queryWrapper);
boolean remove = this.remove(queryWrapper);
if (remove) {
brforeDataList.forEach((sAuRoleFunction) -> {
List<SLogDetail> logList = LogUtil.deleteLog(sAuRoleFunction, sAuRoleFunction.getId());
sLogDetailList.addAll(logList);
});
}
roleFunctionStrategyService.calculateFunAuthForRemove(roleId, disableFunctionIds);
roleFunctionStrategyService.deleteFormSchemeForRole(roleId, disableFunctionIds);
roleFunctionStrategyService.deleteFormViewForRole(roleId, disableFunctionIds);
roleFunctionStrategyService.deleteFormExportForRole(roleId, disableFunctionIds);
}
if (newFunIdSet.size() == 0) {
return new JsonSuccessObject("", sLogDetailList);
} else {
List<SAuRoleFunction> roleFunctionList = new ArrayList();
Iterator var19 = newFunIdSet.iterator();
while(var19.hasNext()) {
menuId = (String)var19.next();
SAuRoleFunction roleFunction = new SAuRoleFunction();
roleFunction.setRoleId(roleId);
roleFunction.setId(IdUtil.simpleUUID());
roleFunction.setFunctionId(menuId);
roleFunctionList.add(roleFunction);
}
boolean b = this.saveBatch(roleFunctionList);
if (b) {
roleFunctionList.forEach((sAuRoleFunction) -> {
List<SLogDetail> logList = LogUtil.saveLog(sAuRoleFunction, sAuRoleFunction.getId());
sLogDetailList.addAll(logList);
});
}
if (roleFunctionStrategyService != null && b) {
roleFunctionStrategyService.calculateFunAuthForSave(roleId, newFunIdSet);
// roleFunctionStrategyService.initRolepeopleTemplateByDefault(roleId);
List<String> nodeDefaultSchemeIds = this.dcSchemeMapper.queryNodeAuthSchemeIds(sAuRole.getNodeId(), newFunIdSet);
if (CollectionUtil.isNotEmpty(newFunIdSet) && CollectionUtil.isNotEmpty(nodeDefaultSchemeIds)) {
roleFunctionStrategyService.saveDefaultFormSchemeForRole(roleId, newFunIdSet, nodeDefaultSchemeIds);
roleFunctionStrategyService.saveDefaultViewSchemeForRole(roleId, newFunIdSet, nodeDefaultSchemeIds);
roleFunctionStrategyService.saveDefaultExportSchemeForRole(roleId, newFunIdSet, nodeDefaultSchemeIds);
}
}
return (JsonObject)(b ? new JsonSuccessObject("", sLogDetailList) : JsonErrorObject.ERROR);
}
}
68.
private void getTopFunId(Set<String> newFunIdSet, Set<String> topFunIds) {
if (!CollectionUtils.isEmpty(newFunIdSet)) {
List<SAuFunction> functions = this.isAuFunctionService.listByIds(newFunIdSet);
if (!CollectionUtils.isEmpty(functions)) {
Set<String> parentIds = (Set)functions.stream().filter((sAuFunction) -> {
String parentId = sAuFunction.getParentId();
boolean isTop = parentId == null || parentId.equals("-1");
if (isTop) {
topFunIds.add(sAuFunction.getId());
}
return !isTop;
}).map(SAuFunction::getParentId).collect(Collectors.toSet());
this.getTopFunId(parentIds, topFunIds);
}
}
}
78.可变参数
@Override
public boolean removeRoleFunction(String roleId, String... functionIds) {
Assert.notNull(roleId, "角色ID不可为空!");
Assert.notNull(functionIds, "功能名称不能为空!");
LambdaUpdateWrapper<SAuRoleFunction> queryWrapper = new LambdaUpdateWrapper();
queryWrapper.eq(SAuRoleFunction::getRoleId, roleId);
queryWrapper.in(SAuRoleFunction::getFunctionId, functionIds);
return this.remove(queryWrapper);
}
68.返回一个对象
69.转化为泛型数组
70.
Assert.hasText(configCode, "配置编码不能为空");