【内容安全】微服务学习笔记八:使用腾讯云T-Sec天御对文本及图片内容进行安全检测

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 【内容安全】微服务学习笔记八:使用腾讯云T-Sec天御对文本及图片内容进行安全检测

 一:需求分析

       由于我最近在做的是新闻体裁的项目,由于互联网的发展,网络上面经常充斥着很多不可控的风险因素,如色情暴力、垃圾广告等,因此发布文章需要进行审核才能发布到App端。如果全部都要人工审核的话会很耗时间,这时候就可以借助第三方提供的服务来对内容进行审核。市面上提供的内容安全审核服务有很多,如腾讯云、阿里云、网易易盾等,不过都是收费的,我是学生党,没必要为了一个项目而去购买这些服务(这些服务还不便宜),不过好在这些服务都会免费送一个月给新用户使用,这样就还有的玩。

二:功能介绍

1.文本内容安全

       什么是文本内容安全,主要指的是检测文本中是否包含色情暴力、广告内容等违规内容,并对检测出违规的文章进行屏蔽。而腾讯文本内容安全(Text Moderation System,TMS)是一款文本内容智能识别服务,对用户上传的文本进行内容安全识别,能够做到识别准确率高、召回率高,多维度覆盖对内容识别的要求,并实时更新识别服务的识别标准和能力。其具有以下特点:

    • 能够对文本文件进行多样化场景检测,精准识别文本中出现可能令人反感、不安全或不适宜的内容,有效降低内容违规风险与有害信息识别成本。
    • 能够精准识别涉黄等有害内容,支持用户配置词库,打击自定义的违规文本。文本内容安全服务能检测内容的危险等级,对于高危部分直接过滤,对于可疑部分提交人工复审,从而节省识别人力,降低业务风险。
    • 以开放 API(Application Programming Interface,应用程序编程接口)的方式提供服务,用户通过调用API即可获取识别结果,高效构建智能化业务系统,提升业务运营效率。

    2.图片内容安全

           什么是图片内容安全,图片内容安全(Image Moderation System,IMS)是一款图片内容智能识别服务。能够对图片文件进行多样化场景检测,精准识别图片中出现可能令人反感、不安全或不适宜内容;帮助我们有效降低内容违规风险与有害信息过滤成本。 IMS也能对色情、广告等进行检测,还能自定义检测内容,当然文本内容安全也支持自定义检测内容。

    三:功能实现

    1.前期工作  

    (1)开通服务并配置策略

            要使用腾讯云的内容安全服务,首先需要注册一个腾讯与账号,并且开通相应的服务(文本内容安全&图片内容安全),然后可以在内容安全控制台创建自己的策略,具体配置教程见官方文档。

    (2)导入坐标

            我使用的是Java语言进行开发,所以选择接入Java SDK进行接入,导入以下坐标

    <!--腾讯云-->
    <dependency>
        <groupId>com.tencentcloudapi</groupId>
        <artifactId>tencentcloud-sdk-java-cvm</artifactId>
        <version>3.1.528</version>
    </dependency>
    <dependency>
        <groupId>com.tencentcloudapi</groupId>
        <artifactId>tencentcloud-sdk-java-tms</artifactId>
        <version>3.1.528</version>
    </dependency>
    <dependency>
        <groupId>com.tencentcloudapi</groupId>
        <artifactId>tencentcloud-sdk-java-ims</artifactId>
        <version>3.1.528</version>
    </dependency>

    image.gif

    (3)配置腾讯云 API 访问密钥

           这两个服务都需要用到腾讯云API的访问密钥,因此使用在接入服务之前需要先申请自己的秘钥,申请地址点击这里。获取密钥之后,便可以将其配置到相应的微服务中,由于我使用了Nacos进行注册管理,这里我就将密钥信息放入Nacos配置中心中(密钥已部分删除,不能直接使用):

    tencentcloud:
     secretId: AKIDjLjtq1rSe5JJ3
     secretKey: bxDFH66IH1glxeASULOJ

    image.gif

    2.代码实现

    (1)文本内容安全

      • 需要的参数:密钥信息、待检测文本(String类型)
      • 注意事项:不能直接将文本扔进去检测,检测之前需要对文本进行Base64加密
      • 返回类型:JSON
      package com.my.common.tencentcloud;
      import com.alibaba.fastjson.JSON;
      import com.alibaba.fastjson.JSONObject;
      import com.tencentcloudapi.common.Credential;
      import com.tencentcloudapi.common.profile.ClientProfile;
      import com.tencentcloudapi.common.profile.HttpProfile;
      import com.tencentcloudapi.common.exception.TencentCloudSDKException;
      import com.tencentcloudapi.tms.v20201229.TmsClient;
      import com.tencentcloudapi.tms.v20201229.models.*;
      import lombok.Getter;
      import lombok.Setter;
      import org.springframework.boot.context.properties.ConfigurationProperties;
      import org.springframework.stereotype.Component;
      import java.nio.charset.StandardCharsets;
      import java.util.Base64;
      @Getter
      @Setter
      @Component
      @ConfigurationProperties(prefix = "tencentcloud")
      public class TextDetection {
          private String secretId;
          private String secretKey;
          public JSONObject greenTextDetection(String text) throws TencentCloudSDKException {
              // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
              Credential cred = new Credential(secretId, secretKey);
              // 实例化一个http选项,可选的,没有特殊需求可以跳过
              HttpProfile httpProfile = new HttpProfile();
              httpProfile.setEndpoint("tms.tencentcloudapi.com");
              // 实例化一个client选项,可选的,没有特殊需求可以跳过
              ClientProfile clientProfile = new ClientProfile();
              clientProfile.setHttpProfile(httpProfile);
              // 实例化要请求产品的client对象,clientProfile是可选的
              TmsClient client = new TmsClient(cred, "ap-guangzhou", clientProfile);
              // 实例化一个请求对象,每个接口都会对应一个request对象
              TextModerationRequest req = new TextModerationRequest();
              //Base64加密
              String encryptionText = Base64.getEncoder().encodeToString(text.getBytes(StandardCharsets.UTF_8));
              //设置内容参数
              req.setContent(encryptionText);
              // 返回的resp是一个TextModerationResponse的实例,与请求对象对应
              TextModerationResponse resp = client.TextModeration(req);
              // 输出json格式的字符串回包
              String result = TextModerationResponse.toJsonString(resp);
              return JSON.parseObject(result);
          }
      }

      image.gif

      (2)图片内容安全

        • 需要的参数:密钥信息、待检测图片URL路径
        • 注意事项:如果输入参数为待检测图片文件内容,也需要对其进行Base64编码,且图片大小不超过5MB
        • 返回类型:JSON
        package com.my.common.tencentcloud;
        import com.alibaba.fastjson.JSON;
        import com.alibaba.fastjson.JSONObject;
        import com.tencentcloudapi.common.exception.TencentCloudSDKException;
        import lombok.Getter;
        import lombok.Setter;
        import org.springframework.boot.context.properties.ConfigurationProperties;
        import org.springframework.stereotype.Component;
        import com.tencentcloudapi.common.Credential;
        import com.tencentcloudapi.common.profile.ClientProfile;
        import com.tencentcloudapi.common.profile.HttpProfile;
        import com.tencentcloudapi.ims.v20201229.ImsClient;
        import com.tencentcloudapi.ims.v20201229.models.*;
        @Getter
        @Setter
        @Component
        @ConfigurationProperties(prefix = "tencentcloud")
        public class ImageDetection {
            private String secretId;
            private String secretKey;
            public JSONObject greenImageDetection(String imageUrl) throws TencentCloudSDKException {
                // 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
                Credential cred = new Credential(secretId, secretKey);
                // 实例化一个http选项,可选的,没有特殊需求可以跳过
                HttpProfile httpProfile = new HttpProfile();
                httpProfile.setEndpoint("ims.tencentcloudapi.com");
                // 实例化一个client选项,可选的,没有特殊需求可以跳过
                ClientProfile clientProfile = new ClientProfile();
                clientProfile.setHttpProfile(httpProfile);
                // 实例化要请求产品的client对象,clientProfile是可选的
                ImsClient client = new ImsClient(cred, "ap-guangzhou", clientProfile);
                // 实例化一个请求对象,每个接口都会对应一个request对象
                ImageModerationRequest req = new ImageModerationRequest();
                //设置图片url地址
                req.setFileUrl(imageUrl);
                // 返回的resp是一个ImageModerationResponse的实例,与请求对象对应
                ImageModerationResponse resp = client.ImageModeration(req);
                // 输出json格式的字符串回包
                String result = ImageModerationResponse.toJsonString(resp);
                return JSON.parseObject(result);
            }
        }

        image.gif

        四:结果处理

        1.返回参数

        两个服务返回的参数基本相同,见下表:

        image.gif编辑

        我这里主要用到的参数是SubLabel参数,其包含Block(建议屏蔽)、Review(建议人工复审)、Pass(通过)三种类型。

        2.功能测试

        (1)文本内容安全测试

        @Autowired
        private TextDetection textDetection;
        @Autowired
        private ImageDetection imageDetection;
        @Test
        public void textTest() throws TencentCloudSDKException {
            JSONObject result_json = textDetection.greenTextDetection("冰毒");
            String result = (String) result_json.get("Suggestion");
            System.out.println(result);
        }

        image.gif

        image.gif编辑

        可以看到建议屏蔽。

        (2)图片内容安全测试

        检测图片:是一个冰毒图片,由于放上来会造成图片违规,这里就不放上来了,需要说明的是好像IMS对于纯图片识别效果不是很好,要是上面包含某些违规文字能够检测出来,应该是使用了OCR技术。

        @Test
        public void imageTest() throws TencentCloudSDKException {
            JSONObject result_json = imageDetection.greenImageDetection("http://49.234.52.192:9000/headlines/2022/07/21/e3428ce741f04602b6984196af18c4d4.png");
            String result = (String) result_json.get("Suggestion");
            System.out.println(result);
        }

        image.gif

         image.gif编辑

        可以看到建议屏蔽。

        (3)总结

                腾讯云T-Sec 天御的内容安全检测结果还是挺靠谱的,还能自定义检测内容,大大提高了检测的精准度,而且调用起来十分方便,用来做项目练手很不错。

        相关文章
        |
        24天前
        |
        安全 Java API
        第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
        第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)
        44 0
        第7章 Spring Security 的 REST API 与微服务安全(2024 最新版)(上)
        |
        2月前
        |
        SpringCloudAlibaba Java 网络架构
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(二)Rest微服务工程搭建
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(二)Rest微服务工程搭建
        63 0
        |
        2月前
        |
        SpringCloudAlibaba Java 网络架构
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(七)Spring Cloud Gateway服务网关
        121 0
        |
        2月前
        |
        监控 安全 数据管理
        现代化后端开发:微服务架构下的数据管理与安全挑战
        随着信息技术的不断发展,现代化后端开发正日益注重微服务架构下的数据管理与安全挑战。本文将探讨微服务架构在后端开发中的应用,重点关注数据管理和安全方面的挑战,并提供相应的解决方案。
        |
        2月前
        |
        SpringCloudAlibaba 负载均衡 Java
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(目录大纲)
        71 1
        |
        2月前
        |
        Java Nacos Sentinel
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(九)Nacos+Sentinel+Seata
        237 0
        |
        2月前
        |
        消息中间件 SpringCloudAlibaba Java
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
        790 0
        |
        2月前
        |
        SpringCloudAlibaba Java 测试技术
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(六)Hystrix(豪猪哥)的使用
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(六)Hystrix(豪猪哥)的使用
        46 1
        |
        2月前
        |
        SpringCloudAlibaba 负载均衡 Java
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(五)OpenFeign的使用
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(五)OpenFeign的使用
        46 0
        |
        2月前
        |
        负载均衡 算法 Java
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(四)Ribbon的使用
        【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(四)Ribbon的使用
        29 0