Commit 103e4e18 by 叶立庆

refactor(controller): 重构员工徽章导出控制器的登录配置处理逻辑

- 提取登录配置解析逻辑到独立的 getLoginConfigList 方法中
- 实现通过账号查找登录配置的 findLoginConfigByAccountId 方法
- 实现通过账号和密码MD5查找登录配置的 findLoginConfigByAccountIdAndPasswordMd5 方法
- 实现通过账号获取许可商户号列表的 getPermissMerchantNoListByAccountId 方法
- 简化登录接口中的配置查找逻辑,使用新的辅助方法
- 优化获取商户列表的代码结构,提高可读性和维护性
- 统一错误处理和日志记录方式
1 parent c0ad1d30
...@@ -64,8 +64,72 @@ public class EmployeeBadgeOutController extends AbstractRopRequest { ...@@ -64,8 +64,72 @@ public class EmployeeBadgeOutController extends AbstractRopRequest {
@Autowired @Autowired
private Environment environment; private Environment environment;
@Autowired private List<ScriptLoginConfig> getLoginConfigList() {
private Environment env; String loginConfig = environment.getProperty("mindworkplate.out.login.config");
if (StringUtils.isBlank(loginConfig)) {
log.warn("未配置登录信息");
return null;
}
try {
List<ScriptLoginConfig> configList = JSON.parseArray(loginConfig, ScriptLoginConfig.class);
if (configList == null || configList.isEmpty()) {
log.warn("登录配置格式错误");
return null;
}
return configList;
} catch (Exception e) {
log.error("解析登录配置异常", e);
return null;
}
}
private ScriptLoginConfig findLoginConfigByAccountId(List<ScriptLoginConfig> configList, String accountId) {
if (configList == null || configList.isEmpty() || StringUtils.isBlank(accountId)) {
return null;
}
for (ScriptLoginConfig config : configList) {
if (accountId.equals(config.getAccountId())) {
return config;
}
}
return null;
}
private ScriptLoginConfig findLoginConfigByAccountIdAndPasswordMd5(List<ScriptLoginConfig> configList,
String accountId, String passwordMd5) {
if (configList == null || configList.isEmpty() || StringUtils.isBlank(accountId) || StringUtils.isBlank(passwordMd5)) {
return null;
}
for (ScriptLoginConfig config : configList) {
if (accountId.equals(config.getAccountId()) && passwordMd5.equals(MD5.convert32(config.getPassword()))) {
return config;
}
}
return null;
}
private List<Integer> getPermissMerchantNoListByAccountId(String accountId) {
List<ScriptLoginConfig> configList = getLoginConfigList();
ScriptLoginConfig config = findLoginConfigByAccountId(configList, accountId);
if (config == null || config.getMerchantList() == null || config.getMerchantList().isEmpty()) {
return null;
}
List<Integer> merchantNoList = new ArrayList<>();
for (ScriptLoginConfig.MerchantInfo merchant : config.getMerchantList()) {
String merchantId = merchant.getMerchantId();
if (StringUtils.isNotBlank(merchantId)) {
try {
merchantNoList.add(Integer.parseInt(merchantId));
} catch (NumberFormatException e) {
log.warn("门店ID格式错误: {}", merchantId);
}
}
}
return merchantNoList;
}
@ApiOperation("脚本登录接口") @ApiOperation("脚本登录接口")
@ServiceMethod(method = "dj.api.mindworkplate.badge.login.out", needInSession = NeedInSessionType.NO) @ServiceMethod(method = "dj.api.mindworkplate.badge.login.out", needInSession = NeedInSessionType.NO)
...@@ -94,27 +158,12 @@ public class EmployeeBadgeOutController extends AbstractRopRequest { ...@@ -94,27 +158,12 @@ public class EmployeeBadgeOutController extends AbstractRopRequest {
return RopResponse.error("密码加密失败"); return RopResponse.error("密码加密失败");
} }
String loginConfig = environment.getProperty("mindworkplate.out.login.config"); List<ScriptLoginConfig> configList = getLoginConfigList();
if (StringUtils.isBlank(loginConfig)) {
log.warn("未配置登录信息");
return RopResponse.error("系统配置错误");
}
// 解析配置为对象列表
List<ScriptLoginConfig> configList = JSON.parseArray(loginConfig, ScriptLoginConfig.class);
if (configList == null || configList.isEmpty()) { if (configList == null || configList.isEmpty()) {
log.warn("登录配置格式错误");
return RopResponse.error("系统配置错误"); return RopResponse.error("系统配置错误");
} }
// 查找匹配的账号配置 ScriptLoginConfig matchedConfig = findLoginConfigByAccountIdAndPasswordMd5(configList, accountId, passwordMd5);
ScriptLoginConfig matchedConfig = null;
for (ScriptLoginConfig config : configList) {
if (accountId.equals(config.getAccountId()) && passwordMd5.equals(MD5.convert32(config.getPassword()))) {
matchedConfig = config;
break;
}
}
if (matchedConfig == null) { if (matchedConfig == null) {
log.warn("登录失败,账号或密码错误: {}", accountId); log.warn("登录失败,账号或密码错误: {}", accountId);
...@@ -160,40 +209,13 @@ public class EmployeeBadgeOutController extends AbstractRopRequest { ...@@ -160,40 +209,13 @@ public class EmployeeBadgeOutController extends AbstractRopRequest {
String accountId = request.getAccountId(); String accountId = request.getAccountId();
// 根据账号从配置中获取门店列表 // 根据账号从配置中获取门店列表
List<Integer> merchantNoList = new ArrayList<>(); if (StringUtils.isBlank(accountId)) {
if (StringUtils.isNotBlank(accountId)) { log.warn("当前登录账号: {}", accountId);
String loginConfig = environment.getProperty("mindworkplate.out.login.config");
if (StringUtils.isNotBlank(loginConfig)) {
try {
// 解析配置为对象列表
List<ScriptLoginConfig> configList = JSON.parseArray(loginConfig, ScriptLoginConfig.class);
if (configList != null && !configList.isEmpty()) {
// 查找匹配的账号配置
for (ScriptLoginConfig config : configList) {
if (accountId.equals(config.getAccountId())) {
if (config.getMerchantList() != null && !config.getMerchantList().isEmpty()) {
for (ScriptLoginConfig.MerchantInfo merchant : config.getMerchantList()) {
String merchantId = merchant.getMerchantId();
if (StringUtils.isNotBlank(merchantId)) {
try {
merchantNoList.add(Integer.parseInt(merchantId));
} catch (NumberFormatException e) {
log.warn("门店ID格式错误: {}", merchantId);
}
}
}
}
break;
}
}
}
} catch (Exception e) {
log.error("解析登录配置异常", e);
return RopResponse.error("账号权限不足,请联系管理员"); return RopResponse.error("账号权限不足,请联系管理员");
} }
}
}else { List<Integer> merchantNoList = getPermissMerchantNoListByAccountId(accountId);
log.warn("当前登录账号: {}", accountId); if (merchantNoList == null || merchantNoList.isEmpty()) {
return RopResponse.error("账号权限不足,请联系管理员"); return RopResponse.error("账号权限不足,请联系管理员");
} }
...@@ -235,42 +257,16 @@ public class EmployeeBadgeOutController extends AbstractRopRequest { ...@@ -235,42 +257,16 @@ public class EmployeeBadgeOutController extends AbstractRopRequest {
String accountId = request.getAccountId(); String accountId = request.getAccountId();
// 根据账号从配置中获取门店列表 // 根据账号从配置中获取门店列表
List<Integer> merchantNoList = new ArrayList<>(); if (StringUtils.isBlank(accountId)) {
if (StringUtils.isNotBlank(accountId)) { log.warn("当前登录账号: {}", accountId);
String loginConfig = environment.getProperty("mindworkplate.out.login.config");
if (StringUtils.isNotBlank(loginConfig)) {
try {
// 解析配置为对象列表
List<ScriptLoginConfig> configList = JSON.parseArray(loginConfig, ScriptLoginConfig.class);
if (configList != null && !configList.isEmpty()) {
// 查找匹配的账号配置
for (ScriptLoginConfig config : configList) {
if (accountId.equals(config.getAccountId())) {
if (config.getMerchantList() != null && !config.getMerchantList().isEmpty()) {
for (ScriptLoginConfig.MerchantInfo merchant : config.getMerchantList()) {
String merchantId = merchant.getMerchantId();
if (StringUtils.isNotBlank(merchantId)) {
try {
merchantNoList.add(Integer.parseInt(merchantId));
} catch (NumberFormatException e) {
log.warn("门店ID格式错误: {}", merchantId);
}
}
}
}
break;
}
}
}
} catch (Exception e) {
log.error("解析登录配置异常", e);
return RopResponse.error("账号权限不足,请联系管理员"); return RopResponse.error("账号权限不足,请联系管理员");
} }
}
}else { List<Integer> merchantNoList = getPermissMerchantNoListByAccountId(accountId);
log.warn("当前登录账号: {}", accountId); if (merchantNoList == null||merchantNoList.isEmpty()) {
return RopResponse.error("账号权限不足,请联系管理员"); return RopResponse.error("账号权限不足,请联系管理员");
} }
employeeBadgeDTO.setPermissMerchantNoList(merchantNoList); employeeBadgeDTO.setPermissMerchantNoList(merchantNoList);
RpcResponse<String> rpcPaginationRpcResponse = employeeBadgeRemote.customerBaseListExportV2( RpcResponse<String> rpcPaginationRpcResponse = employeeBadgeRemote.customerBaseListExportV2(
employeeBadgeDTO); employeeBadgeDTO);
......
  • @wangxw1
    commented
    Master

    一、关键问题与优化建议(仅限 Top 3)

    1. 性能与资源利用:高频调用的接口中存在重复的 JSON 解析(严重)

    • 问题描述:在 getLoginConfigList() 方法中,每次调用该方法都会通过 environment.getProperty 获取配置字符串,并使用 JSON.parseArray 进行反序列化。由于该方法在登录和多个列表导出接口中被高频调用,每次请求都进行全量 JSON 解析会产生大量临时对象,严重消耗 CPU 和内存资源,降低系统吞吐量。
    • 优化建议:建议将该配置项映射为 Spring 的 Bean。可以通过 @ConfigurationProperties 或 @Value 将 JSON 启动时解析并缓存到内存中。如果该配置需要动态修改生效,可结合配置中心(如 Nacos、Apollo)的 @RefreshScope 或监听器来实现配置变更时的重新解析,而不是在每次 HTTP 请求时解析。

    2. 最佳实践:集合类型的方法返回 null 违反阿里巴巴开发规范(中等)

    • 问题描述:getLoginConfigList() 和 getPermissMerchantNoListByAccountId() 方法在遇到空配置或异常时返回了 null。这违反了《阿里巴巴 Java 开发手册》中的强制规范:“返回类型为所有的集合或数组时,必须返回空集合或空数组,绝对不能返回 null”。虽然当前调用方做了判空处理,但返回 null 极易在未来维护中引发 NullPointerException。
    • 优化建议:将返回 null 的地方修改为返回 Collections.emptyList() 或 new ArrayList<>()。同时,调用方可以使用 CollectionUtils.isEmpty() 来进行优雅的判空。

    3. 安全性:密码校验使用了不安全的 MD5 算法(中等)

    • 问题描述:在 findLoginConfigByAccountIdAndPasswordMd5 方法中,使用了 MD5.convert32(config.getPassword()) 进行密码匹配。MD5 已经被证明是不安全的散列算法,极易遭受彩虹表攻击和碰撞攻击。
    • 优化建议:如果是新业务,强烈建议放弃 MD5,改用带有随机盐值的安全哈希算法,如 BCrypt(Spring Security 默认支持)或 Argon2。如果是为了兼容历史老数据,建议在后续迭代中增加密码升级机制。

    二、评分明细

    • 1. 功能实现的正确性与健壮性(38/40分)
      • 优点:代码重构逻辑清晰,将原本臃肿在 Controller 接口中的鉴权和配置解析逻辑抽离成了独立的私有方法,保证了原有功能的正确性,消除了重复的 @Autowired Environment 注入。
      • 扣分点:异常处理稍显粗糙,getLoginConfigList 中捕获了顶层 Exception,吞没了具体的异常类型。
    • 2. 安全性与潜在风险(26/30分)
      • 扣分点:使用了已不再安全的 MD5 算法进行密码比对(扣4分)。
    • 3. 最佳实践与可维护性(16/20分)
      • 优点:方法拆分合理,单一职责原则体现得较好,代码可读性大幅提升。
      • 扣分点:集合方法返回 null 违反阿里规范(扣2分);魔法值 "mindworkplate.out.login.config" 在代码中硬编码,未提取为常量(扣2分)。
    • 4. 性能与资源利用(2/5分)
      • 扣分点:每次请求都进行 JSON 反序列化解析,存在较大的性能隐患和不必要的资源浪费(扣3分)。
    • 5. 提交信息质量(5/5分)
      • 优点:Commit message 非常规范,使用了 refactor: 前缀,且详细列出了每一个重构的点,非常有利于后续的代码追溯和 Review。

    总分:87分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!