fix(security): 收紧 MCP 数据权限并移除默认凭据

This commit is contained in:
xqzp2026
2026-09-03 16:00:19 +09:30
parent ddd4b3b4ed
commit e501afe292
11 changed files with 470 additions and 86 deletions
@@ -51,18 +51,18 @@ public class AgentTools implements ToolObject {
/**
* 查询代理商列表。行权限照搬老系统 AgentController.listByPage:
* 权限组 isagent=1 时只查直属下级(supervisor_id = 用户 agentId);isagent=0 不过滤。
* 入参传 id 时精确查询(可查到自身,此时不加 supervisor 限制)。
* 入参传 id 时精确查询;代理商只能查询自己或直属下级。
* supervisorName 批量 IN 查询富化,无 N+1。
*/
@XqTool(category = "customer", kind = "read")
@Tool(name = "agent.list.query", description = "查询代理商列表(分页)。"
+ "行级权限:代理商身份的用户(权限组 isagent=1)只能查到自己名下的直属下级代理;总部身份(isagent=0)可看全部。"
+ "传 id 时为精确查询、可查到自身,其余过滤条件失效。"
+ "传 id 时为精确查询;代理商只能查询自己或直属下级,总部可查询任意合法 id,其余过滤条件失效。"
+ "level 含义:0=总部 1=一级 2=二级;channelNo 含义:Z=总部 F=分公司 D=代理。"
+ "分页:page 从 1 开始,rows 默认 20、上限 100,超出自动截断为 100 并在返回的 notice 字段提示。"
+ "返回 total(符合条件的总数)、records(本页记录)。")
public Map<String, Object> agentListQuery(
@ToolParam(required = false, description = "代理商 id,精确查询(可查到自身);传入后忽略其他过滤条件") Long id,
@ToolParam(required = false, description = "代理商 id,精确查询;代理商仅限自己或直属下级;传入后忽略其他过滤条件") Long id,
@ToolParam(required = false, description = "代理商名称,模糊匹配") String name,
@ToolParam(required = false, description = "级别:0=总部 1=一级 2=二级") Integer level,
@ToolParam(required = false, description = "渠道编号:Z=总部 F=分公司 D=代理") String channelNo,
@@ -78,9 +78,7 @@ public class AgentTools implements ToolObject {
LambdaQueryWrapper<Agent> wrapper = new LambdaQueryWrapper<Agent>()
.eq(Agent::getSoftDelete, 0);
if (id != null) {
wrapper.eq(Agent::getId, id);
} else {
if (id == null) {
if (StringUtils.hasText(name)) {
wrapper.like(Agent::getName, name);
}
@@ -99,10 +97,8 @@ public class AgentTools implements ToolObject {
if (StringUtils.hasText(serialnumber)) {
wrapper.eq(Agent::getSerialnumber, serialnumber);
}
if (permissionService.isAgentAuthority(user.getAuthorityId())) {
wrapper.eq(Agent::getSupervisorId, user.getAgentId());
}
}
permissionService.applyAgentListRowFilter(wrapper, user, id);
Long total = agentMapper.selectCount(wrapper);
wrapper.select(Agent::getId, Agent::getName, Agent::getLevel, Agent::getChannelNo,
@@ -57,13 +57,45 @@ public class PermissionService {
this.redisTemplate = redisTemplate;
}
/** 权限组 isagent=1 → 代理商视角(对应 AuthorityServiceImpl.checkAuthorityIsagent) */
/** 权限组 isagent=1 → 代理商视角;未知或已删除的权限组拒绝访问。 */
public boolean isAgentAuthority(Long authorityId) {
if (authorityId == null) {
return false;
throw new IllegalStateException("用户未配置权限组,拒绝访问业务数据");
}
Authority authority = authorityMapper.selectById(authorityId);
return authority != null && authority.getIsagent() != null && authority.getIsagent() == 1;
if (authority == null || !Integer.valueOf(0).equals(authority.getSoftDelete())) {
throw new IllegalStateException("用户权限组不存在或已删除,拒绝访问业务数据");
}
if (Integer.valueOf(1).equals(authority.getIsagent())) {
return true;
}
if (Integer.valueOf(0).equals(authority.getIsagent())) {
return false;
}
throw new IllegalStateException("用户权限组缺少有效的行权限配置,拒绝访问业务数据");
}
/**
* 代理商列表权限:总部可按任意 id 查询;代理商只能查询自己或直属下级。
* 未传 id 时保持老页面语义,只列直属下级。
*/
public void applyAgentListRowFilter(LambdaQueryWrapper<Agent> wrapper, User user, Long id) {
boolean agentAuthority = isAgentAuthority(user.getAuthorityId());
if (id != null) {
wrapper.eq(Agent::getId, id);
}
if (!agentAuthority) {
return;
}
if (user.getAgentId() == null) {
throw new IllegalStateException("代理商用户未关联代理商,拒绝访问业务数据");
}
if (id == null) {
wrapper.eq(Agent::getSupervisorId, user.getAgentId());
return;
}
wrapper.and(w -> w.eq(Agent::getId, user.getAgentId())
.or().eq(Agent::getSupervisorId, user.getAgentId()));
}
/**
@@ -75,6 +107,9 @@ public class PermissionService {
if (!isAgentAuthority(user.getAuthorityId())) {
return null;
}
if (user.getAgentId() == null) {
throw new IllegalStateException("代理商用户未关联代理商,拒绝访问业务数据");
}
Set<Long> ids = new LinkedHashSet<>();
ids.addAll(subordinateAndSelfIds(user));
ids.addAll(managedAuths(user.getId()).stream()
@@ -105,7 +140,7 @@ public class PermissionService {
/**
* 销售部门非负责人判定(老系统 getByIsAgent 内 saleFlag 逻辑,:703-710):
* Redis db5 Dictionary hash 的 authDeptId 命中 ",deptId," 且 authDeptPostId 不命中 "postId,"。
* 字典缺失时按"不限制"处理并告警(与老系统 NPE 直接报错的行为不同,属于有意收敛)。
* 字典缺失时拒绝访问,避免权限配置异常导致数据范围放宽。
*/
public boolean isSalesNonLeader(User user) {
if (user.getDeptId() == null || user.getPostId() == null) {
@@ -114,8 +149,8 @@ public class PermissionService {
Object authDeptId = redisTemplate.opsForHash().get(DICTIONARY_KEY, DICT_FIELD_AUTH_DEPT_ID);
Object authDeptPostId = redisTemplate.opsForHash().get(DICTIONARY_KEY, DICT_FIELD_AUTH_DEPT_POST_ID);
if (authDeptId == null || authDeptPostId == null) {
log.warn("Redis Dictionary 缺少 {}/{} 字段,销售部门限制未生效", DICT_FIELD_AUTH_DEPT_ID, DICT_FIELD_AUTH_DEPT_POST_ID);
return false;
log.error("Redis Dictionary 缺少 {}/{} 字段,拒绝访问业务数据", DICT_FIELD_AUTH_DEPT_ID, DICT_FIELD_AUTH_DEPT_POST_ID);
throw new IllegalStateException("销售数据权限配置不可用,拒绝访问业务数据");
}
return authDeptId.toString().contains("," + user.getDeptId() + ",")
&& !authDeptPostId.toString().contains(user.getPostId() + ",");
@@ -167,6 +202,9 @@ public class PermissionService {
if (!isAgentAuthority(user.getAuthorityId())) {
return;
}
if (user.getAgentId() == null) {
throw new IllegalStateException("代理商用户未关联代理商,拒绝访问业务数据");
}
// ids 组装:下级 + 自己 + 代管(老 :682-702)
List<Long> ids = new ArrayList<>(subordinateAndSelfIds(user));
List<WarehouseAgentAuth> agentlist = managedAuths(user.getId());