liu
2 天以前 43aab58c9ecb1203770ba382efbdbae299ca87ec
yudao-module-system/src/main/java/cn/iocoder/yudao/module/system/framework/agent/SystemAgentBridgeGuard.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,116 @@
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 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;
    @Resource
    private SystemAgentDelegationService delegationService;
    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 requireAuthenticated(String providedServiceToken) {
        verifyServiceToken(providedServiceToken);
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        if (userId == null) {
            throw new AgentBridgeException(HttpStatus.UNAUTHORIZED, "UNAUTHORIZED", "未登录或登录已过期");
        }
        return principalOf(userId);
    }
    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 require(String providedServiceToken, String delegationToken, String taskId,
                                  String operation, String permission, String moduleId, Long attachmentId) {
        verifyServiceToken(providedServiceToken);
        if (delegationToken != null && !delegationToken.isBlank()) {
            return delegationService.require(delegationToken, taskId, operation, permission, moduleId, attachmentId);
        }
        return require(providedServiceToken, permission);
    }
    /** ä¼šè¯ä¸Žé™„件入口无法预知模块,只要求具备任意一项导入权限。 */
    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);
    }
    public AgentPrincipal requireAny(String providedServiceToken, String delegationToken, String taskId,
                                     String operation, String moduleId, Long attachmentId) {
        if (delegationToken == null || delegationToken.isBlank()) {
            return requireAny(providedServiceToken);
        }
        verifyServiceToken(providedServiceToken);
        AgentBridgeException last = null;
        for (String permission : IMPORT_PERMISSIONS) {
            try {
                return delegationService.require(delegationToken, taskId, operation, permission, moduleId, attachmentId);
            } catch (AgentBridgeException ex) {
                last = ex;
                if (!"DELEGATION_SCOPE_MISMATCH".equals(ex.getErrorCode())
                        && !"DELEGATION_PERMISSION_REVOKED".equals(ex.getErrorCode())) {
                    throw ex;
                }
            }
        }
        throw last == null ? new AgentBridgeException(HttpStatus.FORBIDDEN, "FORBIDDEN", "委托票据权限不足") : last;
    }
    private AgentPrincipal principalOf(Long userId) {
        return new AgentPrincipal(userId, "live");
    }
}