fix(security): 收紧 MCP 数据权限并移除默认凭据
This commit is contained in:
@@ -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());
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
# dev 环境:内网 192.168.1.181(沿用 xiaoqu-franchisee pom.xml dev profile)
|
||||
# 密码走环境变量占位符,默认值仅供本地 dev 使用
|
||||
# 账号和密码必须由环境变量提供;m0/m1/m2 应使用数据库只读账号
|
||||
app:
|
||||
datasource:
|
||||
m0: # 权限库 xiaoqu_comples_d(user/agent/authority/propery/agent_manage_district_auth)
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://192.168.1.181:3306/xiaoqu_comples_d?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||
username: root
|
||||
password: ${M0_DB_PASSWORD:kaixinjiuhao}
|
||||
username: ${M0_DB_USERNAME}
|
||||
password: ${M0_DB_PASSWORD}
|
||||
m1: # 商城库 xiaoqu_mall_d(按需启用)
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://192.168.1.181:3306/xiaoqu_mall_d?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||
username: root
|
||||
password: ${M1_DB_PASSWORD:kaixinjiuhao}
|
||||
username: ${M1_DB_USERNAME}
|
||||
password: ${M1_DB_PASSWORD}
|
||||
m2: # 业务库 xiaoqu_franchisee_d(warehouse_agent_auth 等)
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://192.168.1.181:3306/xiaoqu_franchisee_d?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||
username: root
|
||||
password: ${M2_DB_PASSWORD:kaixinjiuhao}
|
||||
username: ${M2_DB_USERNAME}
|
||||
password: ${M2_DB_PASSWORD}
|
||||
svc: # 自有库 xiaoqu_agent_svc_d(token/scope/tool/audit,读写)
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
jdbc-url: jdbc:mysql://192.168.1.181:3306/xiaoqu_agent_svc_d?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true
|
||||
username: root
|
||||
password: ${SVC_DB_PASSWORD:kaixinjiuhao}
|
||||
username: ${SVC_DB_USERNAME}
|
||||
password: ${SVC_DB_PASSWORD}
|
||||
|
||||
spring:
|
||||
data:
|
||||
redis:
|
||||
host: 192.168.1.181
|
||||
port: 6379
|
||||
password: ${REDIS_PASSWORD:kaixinjiuhao}
|
||||
password: ${REDIS_PASSWORD}
|
||||
database: 5
|
||||
timeout: 5s
|
||||
|
||||
@@ -4,8 +4,6 @@ server:
|
||||
spring:
|
||||
application:
|
||||
name: xiaoqu-agent-service
|
||||
profiles:
|
||||
active: dev
|
||||
ai:
|
||||
mcp:
|
||||
server:
|
||||
|
||||
Reference in New Issue
Block a user