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("评论内容包含敏感词语");
 }
相关推荐
吃面不喝汤661 小时前
Flask + Swagger 完整指南:从安装到配置和注释
后端·python·flask
讓丄帝愛伱2 小时前
spring boot启动报错:so that it conforms to the canonical names requirements
java·spring boot·后端
weixin_586062022 小时前
Spring Boot 入门指南
java·spring boot·后端
雷袭月启2 小时前
SpringBoot实现OAuth客户端
spring boot·oauth客户端
IT毕设梦工厂7 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
凡人的AI工具箱8 小时前
AI教你学Python 第11天 : 局部变量与全局变量
开发语言·人工智能·后端·python
是店小二呀8 小时前
【C++】C++ STL探索:Priority Queue与仿函数的深入解析
开发语言·c++·后端
canonical_entropy9 小时前
金蝶云苍穹的Extension与Nop平台的Delta的区别
后端·低代码·架构
是梦终空9 小时前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
我叫啥都行9 小时前
计算机基础知识复习9.7
运维·服务器·网络·笔记·后端