云
15 小时以前 d9a5ba1de2fce6bd8c50dace505e4bf7864043ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cn.iocoder.yudao.module.system.framework.agent;
 
import cn.iocoder.yudao.framework.security.core.service.SecurityFrameworkService;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
 
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.List;
 
/** 桥接统一鉴权:服务令牌 + 当前登录用户 + 业务权限,缺一不可。 */
@Component
public class SystemAgentBridgeGuard {
 
    public static final String PERMISSION_ITEM_IMPORT = "mdm:item:import";
 
    /** 各业务模块的导入权限;会话创建与附件读取只需命中其中任意一项。 */
    public static final List<String> IMPORT_PERMISSIONS = List.of(
            PERMISSION_ITEM_IMPORT,
            "srm:supplier:create",
            "mes:md-item:create",
            "mes:qc-template:create",
            "mes:pro-mps:create",
            "mes:pro-route:create");
 
    @Value("${yudao.agent.bridge-service-token:}")
    private String bridgeServiceToken;
 
    @Resource
    private SecurityFrameworkService securityFrameworkService;
 
    public void verifyServiceToken(String provided) {
        if (bridgeServiceToken == null || bridgeServiceToken.isBlank() || provided == null
                || !MessageDigest.isEqual(bridgeServiceToken.getBytes(StandardCharsets.UTF_8),
                provided.getBytes(StandardCharsets.UTF_8))) {
            throw new AgentBridgeException(HttpStatus.FORBIDDEN, "SERVICE_TOKEN_INVALID",
                    "智能体服务身份无效,请检查服务密钥配置");
        }
    }
 
    public AgentPrincipal require(String providedServiceToken, String permission) {
        verifyServiceToken(providedServiceToken);
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        if (userId == null) {
            throw new AgentBridgeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED", "未登录或登录已过期");
        }
        if (!securityFrameworkService.hasPermission(permission)) {
            throw new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN",
                    "当前用户缺少数据导入权限(" + permission + ")");
        }
        return principalOf(userId);
    }
 
    /** 会话与附件入口无法预知模块,只要求具备任意一项导入权限。 */
    public AgentPrincipal requireAny(String providedServiceToken) {
        verifyServiceToken(providedServiceToken);
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        if (userId == null) {
            throw new AgentBridgeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED", "未登录或登录已过期");
        }
        if (!securityFrameworkService.hasAnyPermissions(IMPORT_PERMISSIONS.toArray(String[]::new))) {
            throw new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN", "当前用户缺少数据导入权限");
        }
        return principalOf(userId);
    }
 
    private AgentPrincipal principalOf(Long userId) {
        Long tenantId = TenantContextHolder.getTenantId();
        return new AgentPrincipal(userId, tenantId == null ? 0L : tenantId, "live");
    }
}