SpringBoot集成腾讯云敏感词校验API流程

1.pom.xml中引入腾讯云jar配置信息

XML 复制代码
<dependency>
    <groupId>com.tencentcloudapi</groupId>
    <artifactId>tencentcloud-sdk-java</artifactId>
    <version>4.0.11</version>
</dependency>

2.application.yaml中添加配置

java 复制代码
tencent:
  tms:
    secret-id: AKDahCRQ9bolQovlKccMpB
    secret-key: tWswkHXWEenfCpCI7fxxhA
    biz-type: test_demo

3.项目中映射配置信息

java 复制代码
@Data
@Component
@ConfigurationProperties(prefix = "tencent.tms")
public class TencentTmsProperties {
    private String secretId;
    private String secretkey;
    private String bizType;
}

4.封装敏感词校验工具类

java 复制代码
@Slf4j
@Component
public class TencentTmsUtils {
    @Autowired
    private TencentTmsProperties tencentTmsProperties;

    /**
     * 敏感词过滤
     *
     * @param content
     * @return
     */
    public Boolean getTmsResult(String content) {
        if (StringUtils.isEmpty(content) || StringUtils.isEmpty(content.trim())) {
            return Boolean.TRUE;
        }
        Credential cre = new Credential(tencentTmsProperties.getSecretId(), tencentTmsProperties.getSecretkey());
        TmsClient client = new TmsClient(cre, "ap-shanghai");
        TextModerationRequest request = new TextModerationRequest();
        request.setBizType(tencentTmsProperties.getBizType());
        request.setContent(Base64.encode(content));
        try {
            TextModerationResponse response = client.TextModeration(request);
            if (!ObjectUtils.isEmpty(response)) {
                return "Pass".equals(response.getSuggestion());
            }
            //response.getLabel();//返回值:Normal:正常,Porn:色情,Abuse:谩骂,Ad:广告,Custom
            //response.getSuggestion();//返回值:Block:建议屏蔽,Review :建议人工复审,Pass:建议通过
            log.info("敏感词类型:" + response.getLabel() + "处理结果:" + response.getSuggestion());
        } catch (TencentCloudSDKException e) {
            log.error("Sensitive word detection failed, because: ", e);
        }
        return Boolean.FALSE;
    }
}

5.业务代码中应用敏感词校验

java 复制代码
 // 获取评论内容信息
 String content = req.getCommentContent();
 if (!tencentTmsUtils.getTmsResult(content)) {
    throw new BusinessException("评论内容包含敏感词语");
 }
相关推荐
豌豆花下猫1 小时前
Python 潮流周刊#118:Python 异步为何不够流行?(摘要)
后端·python·ai
尚学教辅学习资料2 小时前
Ruoyi-vue-plus-5.x第五篇Spring框架核心技术:5.1 Spring Boot自动配置
vue.js·spring boot·spring
晚安里2 小时前
Spring 框架(IoC、AOP、Spring Boot) 的必会知识点汇总
java·spring boot·spring
秋难降2 小时前
SQL 索引突然 “罢工”?快来看看为什么
数据库·后端·sql
上官浩仁3 小时前
springboot ioc 控制反转入门与实战
java·spring boot·spring
Access开发易登软件3 小时前
Access开发导出PDF的N种姿势,你get了吗?
后端·低代码·pdf·excel·vba·access·access开发
叫我阿柒啊3 小时前
从Java全栈到前端框架:一位程序员的实战之路
java·spring boot·微服务·消息队列·vue3·前端开发·后端开发
中国胖子风清扬4 小时前
Rust 序列化技术全解析:从基础到实战
开发语言·c++·spring boot·vscode·后端·中间件·rust
bobz9654 小时前
分析 docker.service 和 docker.socket 这两个服务各自的作用
后端
野犬寒鸦4 小时前
力扣hot100:旋转图像(48)(详细图解以及核心思路剖析)
java·数据结构·后端·算法·leetcode