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("评论内容包含敏感词语");
 }
相关推荐
朱雨鹏5 分钟前
图解RocketMQ运行原理
后端·rocketmq
颜颜颜yan_8 分钟前
从千毫秒到亚毫秒:连接条件下推如何让复杂 SQL 飞起来
后端
程序员小崔日记24 分钟前
WebSocket 全面解析:让浏览器“实时说话”的黑科技(建议收藏)
后端·websocket·实时通信
-Da-37 分钟前
【操作系统学习日记】《现代处理器性能的三重奏:ISA架构、流水线与缓存系统》
后端·缓存·架构·系统架构
李慕婉学姐41 分钟前
Springboot养老服务管理系统c0t92vu6(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
码农刚子1 小时前
.NET 权限系统(RBAC)怎么设计?直接可复用
后端·.net
把你毕设抢过来1 小时前
基于Spring Boot的演唱会购票系统的设计与实现(源码+文档)
java·spring boot·后端
李白的粉2 小时前
基于springboot的桂林旅游景点导游平台
java·spring boot·毕业设计·课程设计·源代码·桂林旅游景点导游平台
yiyaozjk2 小时前
Go基础之环境搭建
开发语言·后端·golang
颜酱2 小时前
环检测与拓扑排序:BFS/DFS双实现
javascript·后端·算法