Commit 1b348a3f by 叶立庆

优化:增加普通get接口提供给agent调用获取北森token

1 parent 05081c81
...@@ -4,7 +4,6 @@ package cn.com.glsx.dj.pad.controller; ...@@ -4,7 +4,6 @@ package cn.com.glsx.dj.pad.controller;
import cn.com.glsx.dj.pad.request.beisen.GetTokenRequest; import cn.com.glsx.dj.pad.request.beisen.GetTokenRequest;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse; import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.enums.DefaultErrorEnum; import cn.com.glsx.framework.core.enums.DefaultErrorEnum;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.framework.rop.response.RopResponse; import cn.com.glsx.framework.rop.response.RopResponse;
import cn.com.glsx.merchants.remote.MerchantStaffRemote; import cn.com.glsx.merchants.remote.MerchantStaffRemote;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
...@@ -13,7 +12,6 @@ import lombok.extern.slf4j.Slf4j; ...@@ -13,7 +12,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@Slf4j @Slf4j
...@@ -25,7 +23,7 @@ public class BeiSenForAgentController { ...@@ -25,7 +23,7 @@ public class BeiSenForAgentController {
@Autowired @Autowired
private MerchantStaffRemote beiSenStaffRemote; private MerchantStaffRemote beiSenStaffRemote;
@GetMapping("dj.pad.api.beisen.findBeiSenSSOToken") @GetMapping("findBeiSenSSOToken")
@ApiOperation("提供给agent调用,获取北森SSO鉴权") @ApiOperation("提供给agent调用,获取北森SSO鉴权")
public RopResponse<String> findBeiSenSSOTokenNoSession(GetTokenRequest request) { public RopResponse<String> findBeiSenSSOTokenNoSession(GetTokenRequest request) {
if (request.getPhone() == null || request.getPhone().isEmpty()) { if (request.getPhone() == null || request.getPhone().isEmpty()) {
...@@ -41,7 +39,7 @@ public class BeiSenForAgentController { ...@@ -41,7 +39,7 @@ public class BeiSenForAgentController {
} }
} catch (Exception e) { } catch (Exception e) {
log.error("findBeiSenSSOTokenNoSession error:{}", e.getMessage(), e); log.error("findBeiSenSSOTokenNoSession error:{}", e.getMessage(), e);
throw new RpcServiceException(e.getMessage()); return RopResponse.error(e.getMessage());
} }
} }
} }
  • @wangxw1
    commented
    Master

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

    1. 安全性风险:越权获取Token风险与底层异常信息泄露(严重)

    • 问题描述:
      • 业务安全极高风险:接口名为 findBeiSenSSOTokenNoSession 且仅通过 GET 请求传入 phone 就能获取北森 SSO Token。如果该接口没有在网关层做严格的调用方鉴权(如 AppKey/Sign 签名校验),将导致严重的安全漏洞(任何人只要知道手机号即可伪造身份获取 SSO Token)。
      • 异常信息泄露:代码将 e.getMessage() 直接作为错误信息返回给前端/调用方(return RopResponse.error(e.getMessage());)。根据《阿里巴巴 Java 开发手册》异常处理规范,禁止将底层的异常信息(可能包含 SQL 语句、内部 IP 等敏感信息)直接透传给用户。
    • 优化建议:
      • 确认该接口是否有网关层的强鉴权机制(如 AK/SK 签名),严禁无鉴权直接暴露在公网。
      • 将异常返回修改为统一的、脱敏的错误提示,例如:return RopResponse.error("获取北森Token失败,请稍后再试");。

    2. 健壮性缺陷:潜在的空指针异常(NPE)风险(高)

    • 问题描述:
      • 在进行参数校验时,直接调用了 request.getPhone() == null。虽然 Spring MVC 通常会实例化一个空的 GetTokenRequest 对象,但在某些极端情况或内部调用时,如果传入的 request 本身为 null,将直接触发 NullPointerException。
      • 如果在 try 块中发生 NullPointerException,此时 e.getMessage() 的值为 null。返回 RopResponse.error(null) 可能会导致调用方在反序列化或处理错误信息时出现二次异常。
    • 优化建议:
      • 增加对 request 对象的判空:if (request == null || request.getPhone() == null || request.getPhone().isEmpty())。

    3. 规范与可维护性:未使用标准校验框架与全局异常处理(中)

    • 问题描述:
      • 手动参数校验:使用 if-else 进行参数校验导致 Controller 代码臃肿,不符合《阿里巴巴 Java 开发手册》推荐的最佳实践。
      • 局部 try-catch:将原先抛出业务异常(RpcServiceException)改为在 Controller 层直接 try-catch 并返回错误对象。这种做法破坏了全局异常处理的统一性,导致每个接口都需要写重复的异常捕获代码。
    • 优化建议:
      • 引入 JSR-303 校验:在 GetTokenRequest 的 phone 字段上添加 @NotBlank(message = "手机号不能为空"),并在 Controller 参数前加 @Validated 或 @Valid。
      • 移除方法内部的 try-catch,直接抛出自定义业务异常,交由 Spring Boot 的 @RestControllerAdvice 全局异常处理器统一封装 RopResponse.error() 返回。

    二、评分明细

    • 1. 功能实现的正确性与健壮性(40分):28分
      • 扣分原因:存在 request 对象未判空的 NPE 风险;捕获异常时 e.getMessage() 可能为 null 导致返回毫无意义的错误信息。
    • 2. 安全性与潜在风险(30分):15分
      • 扣分原因:直接透传底层异常信息(CWE-209);仅凭手机号且无 Session 换取 SSO Token 存在极大的水平越权和身份伪造风险(假设无网关鉴权)。
    • 3. 最佳实践与可维护性(20分):12分
      • 扣分原因:未使用 Validator 验证框架;退化使用了局部的 try-catch 替代全局异常处理;使用了不推荐的 @Autowired 字段注入。
    • 4. 性能与资源利用(5分):5分
      • 得分原因:无明显的性能瓶颈,日志打印使用了占位符。
    • 5. 提交信息质量(5分):4分
      • 得分原因:提交信息说明了修改目的和调用方,但格式未使用标准的 Angular 规范(如 feat: 或 refactor:)。

    总分:64分

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