TokenInfoDTO tokenInfoDTO = TokenInfoDTO.builder().userId(userAuthRequestDTO.getUserId()). tokenType(userAuthRequestDTO.getTokenType()).deviceToken(deviceToken) .seq(System.currentTimeMillis()).build();是啥意思
时间: 2024-04-26 21:27:04 浏览: 192
这是一个使用了Lombok注解的Java代码,它创建了一个TokenInfoDTO对象。该对象包含以下字段:
- userId:用户ID
- tokenType:令牌类型
- deviceToken:设备令牌
- seq:一个随机的序列号,用于标识每个请求
这个代码使用了Lombok的@Builder注解,它会为TokenInfoDTO类自动生成一个带有所有字段的构造函数,以及一个名为builder()的静态方法,该方法可以用来创建TokenInfoDTO对象。在这个代码中,我们使用builder()方法来设置TokenInfoDTO对象的各个字段的值,并最终创建一个新的TokenInfoDTO对象。
相关问题
try { if (StringUtils.isBlank(AccessTokenInfoRequestDTO.getAccessToken())) { throw new BusinessException(ResponseCodeEnum.ACCESSTOKEN_ILLEGAL.getCode(), ResponseCodeEnum.ACCESSTOKEN_ILLEGAL.getMessage()); } else { TokenInfoDTO tokenInfoDTO = this.getTokenInfo(AccessTokenInfoRequestDTO.getAccessToken()); String accountId = tokenInfoDTO.getUserId(); String deviceToken = tokenInfoDTO.getDeviceToken(); String accToken = RedisCacheUtil.getStringForPrefix(accountId, new RedisTokenKeyEnum[]{RedisTokenKeyEnum.APP_USER_ACCOUNT}); if (StringUtils.isEmpty(accToken)) { throw new BusinessException(ResponseCodeEnum.ACCESS_TOKEN_LOSE.getCode(), ResponseCodeEnum.ACCESS_TOKEN_LOSE.getMessage()); } else if (!AccessTokenInfoRequestDTO.getAccessToken().equals(accToken)) { throw new BusinessException(ResponseCodeEnum.ACCESS_TOKEN_COVER.getCode(), ResponseCodeEnum.ACCESS_TOKEN_COVER.getMessage()); } else if (!ContextUtils.getAppRequestHeaderDTO().getDeviceToken().equals(deviceToken)) { throw new BusinessException(ResponseCodeEnum.ACCESS_TOKEN_COVER.getCode(), ResponseCodeEnum.ACCESS_TOKEN_COVER.getMessage()); } } } catch (BusinessException businessException) { LogUtil.boErrorLog("校验用户token失败:" + businessException.getMessage()); verifyAccessTokenResponseDTO = VerifyAccessTokenResponseDTO.builder().verifyFlag(false).errorDescription(businessException.getMessage()).build(); } catch (Exception exception) { LogUtil.boErrorLog("校验用户token异常:" + exception.getMessage()); verifyAccessTokenResponseDTO = VerifyAccessTokenResponseDTO.builder().verifyFlag(false).errorDescription(ResponseCodeEnum.ACCESSTOKEN_ILLEGAL.getMessage()).build(); }是啥意思
这段代码是一个Java程序,其主要功能是校验用户的访问令牌(access token)是否合法。如果访问令牌为空,或者无法从缓存中获取到该用户的访问令牌,或者用户提交的访问令牌与缓存中的不一致,或者用户提交的设备标识与缓存中的不一致,都会抛出异常,提示用户访问令牌不合法或已失效。如果校验失败,该程序会记录错误日志并返回一个包含错误信息的数据对象。
//获取用户id String userId = UserAgent.get().getUserId(); //根据id拿到用户组织 Map<String, UserEo> userMapByUid = comDataComponent.getUserMapByUid(); String orgCode = userMapByUid.get(userId).getOrgCode(); //所有的行权限 列权限 List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList(); List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList(); List<SchemaRowRule> userRowRuleList = new ArrayList<>(); DataPermissionSchema build = DataPermissionSchema.builder().build(); //设置行权限 根据用户id和用户组织去获取 for (SchemaRowRuleEo schemaRowRuleEo : schemaRowRuleList) { SchemaRowRule schemaRowRule = SchemaRowRule.builder().build(); if(StringUtils.isNotEmpty(schemaRowRuleEo.getRowPermissionUserList())){ List<String> userlist = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class); if(userlist.contains(userId)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaRowRule); userRowRuleList.add(schemaRowRule); } }else if(StringUtils.isNotEmpty(schemaRowRuleEo.getRowPermissionOrgList())){ List<String> orglist = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class); if(orglist.contains(orgCode)){ BeanUtils.copyProperties(schemaRowRuleEo,schemaRowRule); userRowRuleList.add(schemaRowRule); } } } build.setSchemaRowRuleList(userRowRuleList);代码优化及原因
可以将循环中的if语句提取出来,避免重复代码。同时,使用stream流简化代码,并提高代码可读性。代码优化后如下:
```
String userId = UserAgent.get().getUserId();
Map<String, UserEo> userMapByUid = comDataComponent.getUserMapByUid();
String orgCode = userMapByUid.get(userId).getOrgCode();
List<SchemaRowRuleEo> schemaRowRuleList = comDataComponent.getSchemaRowRuleList();
List<SchemaColumnRuleEo> schemaColumnRuleList = comDataComponent.getSchemaColumnRuleList();
List<SchemaRowRule> userRowRuleList = schemaRowRuleList.stream()
.filter(schemaRowRuleEo -> {
List<String> userList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionUserList(), String.class);
List<String> orgList = JSONArray.parseArray(schemaRowRuleEo.getRowPermissionOrgList(), String.class);
return userList.contains(userId) || orgList.contains(orgCode);
})
.map(schemaRowRuleEo -> {
SchemaRowRule schemaRowRule = SchemaRowRule.builder().build();
BeanUtils.copyProperties(schemaRowRuleEo, schemaRowRule);
return schemaRowRule;
})
.collect(Collectors.toList());
DataPermissionSchema build = DataPermissionSchema.builder().build();
build.setSchemaRowRuleList(userRowRuleList);
```
优化后的代码更加简洁易读,使用stream流可以避免重复代码,同时也可以提高代码的执行效率。
阅读全文