【问题记录】09 对接阿里云内容安全机器审核-文本、图片审核,code报错408

一、问题描述

对接阿里云内容安全/机器审核增强版,代码报错408。

二、解决方法

1、查看错误码

408对应:无权限调用该接口。

检查key和secret之后,发现都正常。

检查之后发现,RAM对应的账号未授权对应权限。

2、解决方法:

进入"RAM 访问控制"控制台:用户/权限管理

点击"新增授权",增加权限策略:AliyunYundunGreenWebFullAccess

测试之后,OK。

备注:如果检测不太准确或者缺少一些,还可以去"规则配置"管理检测规则,开启自己需要的规则即可。

三、附:实现对接的代码

pom.xml增加:

java 复制代码
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>green20220302</artifactId>
    <version>3.0.1</version>
</dependency>

实现类AliTmpUtil:

java 复制代码
    private String key = "";
    private String secret = "";
    private String regionId = "cn-shanghai";
    private String endpoint = "green-cip.cn-shanghai.aliyuncs.com";

    private Client createClient() {
        Client client = null;
        try {
            Config config = new Config()
                    .setAccessKeyId(key)
                    .setEndpoint(endpoint)
                    .setRegionId(regionId)
                    .setAccessKeySecret(secret);
            config.setReadTimeout(6000);
            config.setConnectTimeout(3000);
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return client;
    }

文本审核:

java 复制代码
public boolean textModerationPlus(String content) {
        if (content == null || content.trim().isEmpty()) {
            return false;
        }
        Client client = this.createClient();
        if (client == null) {
            return false;
        }

        JSONObject serviceParameters = new JSONObject();
        serviceParameters.set("content", content);

        TextModerationPlusRequest textModerationPlusRequest = new TextModerationPlusRequest();
        textModerationPlusRequest.setService("chat_detection_pro");
        textModerationPlusRequest.setServiceParameters(serviceParameters.toJSONString(4));

        TextModerationPlusResponse response;
        try {
            response = client.textModerationPlus(textModerationPlusRequest);
        } catch (Exception e) {
            throw new ServiceException("安全审核失败");
        }

        if (response == null || response.getBody() == null) {
            return false;
        }

        TextModerationPlusResponseBody result = response.getBody();
        Integer code = result.getCode();
        if (!Integer.valueOf(200).equals(code)) {
            return false;
        }

        TextModerationPlusResponseBody.TextModerationPlusResponseBodyData data = result.getData();
        if (data == null) {
            return false;
        }

        if ("none".equals(data.getRiskLevel())) {
            return true;
        } else {
            throw new ServiceException("内容包含敏感词");
        }
    }

图片审核:

java 复制代码
public boolean imageModerationPlus(String content) {
        if (content == null || content.trim().isEmpty()) {
            return false;
        }
        
        // 处理多个URL的情况(逗号分隔)
        String[] urls = content.split(",");
        
        Client client = this.createClient();
        if (client == null) {
            return false;
        }

        RuntimeOptions runtime = new RuntimeOptions();

        // 遍历每个URL进行审核
        for (String url : urls) {
            String imageUrl = url.trim();
            if (imageUrl.isEmpty()) {
                continue;
            }

            JSONObject serviceParameters = new JSONObject();
            serviceParameters.set("imageUrl", imageUrl);
            serviceParameters.set("dataId", UUID.randomUUID().toString());

            ImageModerationRequest request = new ImageModerationRequest();
            request.setService("baselineCheck");
            request.setServiceParameters(serviceParameters.toJSONString(4));

            ImageModerationResponse response;
            try {
                response = client.imageModerationWithOptions(request, runtime);
            } catch (Exception e) {
                throw new ServiceException("图片安全审核失败: " + imageUrl);
            }

            if (response == null || response.getBody() == null) {
                return false;
            }

            ImageModerationResponseBody result = response.getBody();
            Integer code = result.getCode();
            if (!Integer.valueOf(200).equals(code)) {
                return false;
            }

            ImageModerationResponseBody.ImageModerationResponseBodyData data = result.getData();
            if (data == null) {
                return false;
            }

            // 只要有一个图片不通过,就返回失败
            if (!"none".equals(data.getRiskLevel())) {
                throw new ServiceException("图片包含违规信息");
            }
        }
        return true;
    }
相关推荐
骥龙3 分钟前
第九篇:安全审计与运维——自动化防线建设
运维·安全·自动化
黎阳之光9 小时前
黎阳之光:视频孪生领跑者,铸就中国数字科技全球竞争力
大数据·人工智能·算法·安全·数字孪生
上海云盾-高防顾问9 小时前
网络安全防护发展趋势:从被动防御到主动赋能
安全·web安全
CCTI_Curran9 小时前
UL4200A是美国针对纽扣电池安全标准
安全·纽扣电池·ul4200a·纽扣电池gcc认证·美国亚马逊
做个文艺程序员9 小时前
MySQL安全加固十大硬核操作
数据库·mysql·安全
是罐装可乐9 小时前
深入理解“句柄(Handle)“:从浏览器安全到文件系统访问
前端·javascript·安全
Figo_Cheung10 小时前
Figo义商本体约束推理引擎 (CRE):基于已部署CRE本地模型的技术实践研究——迈向AGI时代的AI伦理安全框架
人工智能·安全
信创DevOps先锋11 小时前
DevOps工具链选型新趋势:本土化适配与安全可控成企业核心诉求
运维·安全·devops
ayt00711 小时前
Netty AbstractNioChannel源码深度剖析:NIO Channel的抽象实现
java·数据库·网络协议·安全·nio
盟接之桥13 小时前
盟接之桥®制造业EDI软件,打通全球供应链“最后一公里”,赋能中国制造连接世界
网络·安全·低代码·重构·汽车·制造