• 阿里云内容审核(企业项目全部使用流程)+全部代码流程+对返回内容封装


    一、准备

    开通阿里安全内容管理账号,获取AccessKey_ID、AccessKey_Secret

    二、代码开发

    1、配置密钥

    这里使用了本地配置文件application.properties中配置如下

    # 内容审核
    AccessKey_ID=**************
    AccessKey_Secret=**************
    
    • 1
    • 2
    • 3

    获取该密钥:在编写获取阿里审核的service层直接引用即可

    @Value("${AccessKey_Secret}")
        private String AccessKey_Secret;
    
        @Value("${AccessKey_ID}")
        private String AccessKey_ID;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2、查看内容审核返回模板

    阿里内容审核返回结果详情地址

    1. 判断内容是否有违规
      在这里插入图片描述
      该字段用于区分此次内容审核里是否存在违规内容,除了pass外,其他都为违规,可以根据业务自行定义。
    2. 判断违规类型触发的违规字段名称在这里插入图片描述
      一句话中可能出现多个违规语句,会存放在details中一起返回,通过contexts进而对不同违规类型做区分
    3. 违规类型label字段含义
      在这里插入图片描述
      由于类型太多,所以使用枚举类进行封装,并且输入时可以根据返回的内容匹配成内容说明或类型id
    package com.marketingCommunityApi.enums.aliyun;
    
    /**
     * @author kanlina
     * @version 1.0
     * @description: 触发敏感词类型
     * @date 2022/8/13 19:37
     */
    public enum SensitiveType {
        /**
         * normal-正常文本
         */
        NORMAL("normal",0,"正常文本"),
        /**
         * ad-广告
         */
        AD("ad",2,"广告"),
        /**
         * politics-涉政
         */
        POLITICS("politics",3,"涉政"),
        /**
         * terrorism-暴恐
         */
        TERRORISM("terrorism",4,"暴恐"),
    
        /**
         * abuse-辱骂
         */
        ABUSE("abuse",5,"辱骂"),
    
        /**
         * porn-色情
         */
        PORN("porn",6,"色情"),
    
        /**
         * flood-灌水
         */
        FLOOD("flood",7,"灌水"),
        /**
         * contraband-违禁
         */
        CONTRABAND("contraband",8,"违禁"),
        /**
         * meaningless-无意义
         */
        MEANINGLESS("meaningless",9,"无意义"),
        CUSTOMIZED("customized",10,"自定义"),
        HARMFUL("harmful",11,"不良场景"),
    
        /**
         *spam-含垃圾信息
         */
        SPAM("spam",1,"含垃圾信息");
    
        private String status;
        private Integer type;
        private String  message;
    
        SensitiveType(String status,Integer type,String message) {
            this.status = status;
            this.type = type;
            this.message = message;
        }
    
        private static final SensitiveType[] VALUES = SensitiveType.values();
        public static Byte matcherType(String status) {
            for (SensitiveType ele : VALUES) {
                if (status.equals(ele.status)) {
                    return ele.type.byteValue();
                }
            }
            return SensitiveType.NORMAL.type.byteValue();
        }
    
        public static String matcherMessage(String status) {
            for (SensitiveType ele : VALUES) {
                if (status.equals(ele.status)) {
                    return ele.message;
                }
            }
            return SensitiveType.NORMAL.message;
        }
    
    }
    
    • 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
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    测试:

    /**
     * @author kanlina
     * @version 1.0
     * @description: 测试阿里内容审核返回枚举类型匹配结果
     * @date 2022/8/13 19:47
     */
    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = Main8010.class)
    @WebAppConfiguration
    public class TestAliyunEnum {
        @Test
        public void testAli(){
           Byte type = SensitiveType.matcherType("spam");
            System.out.println("type = " + type);
            String message = SensitiveType.matcherMessage("spam");
            System.out.println("message = " + message);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述
    4. 内容审核业务逻辑编写

    //将需要记录下来的信息封装到该Dto内,我是因为业务需求需要存库,存储了一些文章内容相关的信息
    public boolean audit(AliContentAuditDto auditDtoParams) throws UnsupportedEncodingException, com.aliyuncs.exceptions.ClientException {
            String content = auditDtoParams.getContent();
    
            IClientProfile profile = DefaultProfile.getProfile("cn-beijing", AccessKey_ID, AccessKey_Secret);
            DefaultProfile.addEndpoint("cn-beijing", "Green", "green.cn-beijing.aliyuncs.com");
            IAcsClient client = new DefaultAcsClient(profile);
            TextScanRequest textScanRequest = new TextScanRequest();
            // 指定API返回格式。
            textScanRequest.setAcceptFormat(FormatType.JSON);
            textScanRequest.setHttpContentType(FormatType.JSON);
            // 指定请求方法。
            textScanRequest.setMethod(com.aliyuncs.http.MethodType.POST);
            textScanRequest.setEncoding("UTF-8");
            textScanRequest.setRegionId("cn-beijing");
            List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
            Map<String, Object> task1 = new LinkedHashMap<String, Object>();
            task1.put("dataId", UUID.randomUUID().toString());
            task1.put("bizType","user_center_nickName");
            /**
             * 待检测的文本,长度不超过10000个字符。
             */
            task1.put("content",content);
    
            tasks.add(task1);
            JSONObject data = new JSONObject();
    
            /**
             * 检测场景。文本垃圾检测请传递antispam。
             **/
            data.put("scenes", Arrays.asList("antispam"));
            data.put("tasks", tasks);
            data.put("bizType","user_center_nickName");
            textScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);
            // 请务必设置超时时间。
            textScanRequest.setConnectTimeout(3000);
            textScanRequest.setReadTimeout(6000);
    
            try {
                HttpResponse httpResponse = client.doAction(textScanRequest);
                if (httpResponse.isSuccess()) {
                    JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                    logger.info(scrResponse.toJSONString());
                    if (200 == scrResponse.getInteger("code")) {
                        JSONArray taskResults = scrResponse.getJSONArray("data");
                        for (Object taskResult : taskResults) {
                            if (200 == ((JSONObject) taskResult).getInteger("code")) {
                                JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
                                for (Object sceneResult : sceneResults) {
                                    String suggestion = ((JSONObject) sceneResult).getString("suggestion");
                                    // 根据scene和suggetion做相关处理。
                                    // suggestion为pass表示未命中垃圾。suggestion为block表示命中了垃圾,可以通过label字段查看命中的垃圾分类。
                                    //触发的敏感词具体内容
                                    if(!"pass".equals(suggestion)){
                                        //获取命中该风险的上下文信息 contentDetail用于封装问题内容的一个实体类,不需要存库的注释掉
                                        CommunitySensitiveContentDetail contentDetail = new CommunitySensitiveContentDetail();
     								//自定义的拷贝对象工具类,不需要存库可以不使用 该类在文章后贴出  
                                    BeanCopierUtil.copy(auditDtoParams,contentDetail);
                                    //存表使用到的主键id,注释掉
                                        int shardingId = ShardingUtils.transContentIdToShardingId(auditDtoParams.getContentId());
                                        contentDetail.setShardingId(shardingId);
                                        JSONArray details = ((JSONObject) sceneResult).getJSONArray("details");
                                        for (Object detail : details) {
                                            JSONArray contexts = ((JSONObject) detail).getJSONArray("contexts");
                                            String label = ((JSONObject) sceneResult).getString("label");
                                            if (contexts!=null) {
                                                for (Object context : contexts) {
                                                    //其中的一条违规内容 sensitiveContent违规的字段
                                                    String sensitiveContent = ((JSONObject) context).getString("context");
                                                    if (StringUtils.isNotBlank(sensitiveContent)) {
                                                        contentDetail.setSensitiveContent(sensitiveContent);
                                                        contentDetail.setSensitiveType(SensitiveType.matcherType(label));
                                                    }
    												//由于业务要求,需要将内容存储到数据库
                                                   communitySensitiveContentDetailMapper.insert(contentDetail);
                                                }
                                            }
    
                                        }
                                        System.out.println("detail = " + contentDetail);
                                        logger.info("文本检测失败 = [" + suggestion + "]");
                                        return false;
                                    }
                                }
                            } else {
                                logger.info("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
                            }
                        }
    
                    } else {
                        logger.info("detect not success. code:" + scrResponse.getInteger("code"));
                    }
                } else {
                    logger.info("response not success. status:" + httpResponse.getStatus());
                }
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                throw new ClientException();
            }
            catch (com.aliyuncs.exceptions.ClientException e) {
                e.printStackTrace();
                throw new com.aliyuncs.exceptions.ClientException("9999","链接失败");
            }catch (Exception e) {
                e.printStackTrace();
            }
            return true;
        }
    
    
    • 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
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109

    封装内容审核返回的相关字段

    package com.marketingCommunityApi.pojo;
    
    import lombok.Data;
    
    import java.io.Serializable;
    import java.util.Date;
    
    /**
     * 内容审核表
     * @TableName community_sensitive_content_detail
     */
    @Data
    public class CommunitySensitiveContentDetail implements Serializable {
        /**
         *违规id主键
         */
        private Long id;
        /**
         *文章id
         */
        private Long contentId;
        /**
         *违规内容
         */
        private String content;
        /**
         *触发敏感词具体内容
         */
        private String sensitiveContent;
        /**
         *触发敏感词类型
         */
        private Byte sensitiveType;
        /**
         *违规内容类型 0-动态 1-评论 2-回复
         */
        private Byte contentType;
        /**
         *违规内容来源 0-用户、1-官方号、2-马甲号、3-游客
         */
        private Byte userType;
        /**
         *创建时间
         */
        private Date createTime;
        /**
         *违规内容发布时间
         */
        private Date publishTime;
        /**
         *处理状态 0-发布,1-隐藏,2-待处理
         */
        private Byte handleStatus;
        private Integer shardingId;
    
    
    }
    
    • 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

    拷贝对象的工具类:

    package com.marketingCommunityApi.util;
    
    import net.sf.cglib.beans.BeanCopier;
    
    import java.util.concurrent.ConcurrentHashMap;
    
    /**
    * @description: 拷贝bean工具类
    * @author kanlina
    * @date: 2022/8/5 上午11:58
    * @version 1.0
    */
    public class BeanCopierUtil {
        /**
         * BeanCopier的缓存
         */
        static final ConcurrentHashMap<String, BeanCopier> BEAN_COPIER_CACHE = new ConcurrentHashMap<>();
    
        /**
         * BeanCopier的copy
         * @param source 源文件的
         * @param target 目标文件
         */
        public static void copy(Object source, Object target) {
            String key = genKey(source.getClass(), target.getClass());
            BeanCopier beanCopier;
            if (BEAN_COPIER_CACHE.containsKey(key)) {
                beanCopier = BEAN_COPIER_CACHE.get(key);
            } else {
                beanCopier = BeanCopier.create(source.getClass(), target.getClass(), false);
                BEAN_COPIER_CACHE.put(key, beanCopier);
            }
            beanCopier.copy(source, target, null);
        }
    
        /**
         * 生成key
         * @param srcClazz 源文件的class
         * @param tgtClazz 目标文件的class
         * @return string
         */
        private static String genKey(Class<?> srcClazz, Class<?> tgtClazz) {
            return srcClazz.getName() + tgtClazz.getName();
        }
    }
    
    
    • 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
  • 相关阅读:
    PostgreSQL按月计算每天值的累加
    2022.08.05_每日一题
    java开发四年之旅
    elasticsearch聚合之bucket terms聚合
    探索LangChain Prompt模板:构建高效语言模型交互的秘诀
    「KDOI」Round 2 | CSP-S 组模拟赛
    论文精读:GHM:Gradient Harmonized Single-stage Detector
    Netty学习笔记(一)
    Java中的锁和分布式架构中的锁
    rk3588 usb网络共享连接
  • 原文地址:https://blog.csdn.net/kk_lina/article/details/126324947