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 小时前
Django 项目的 models 目录中,__init__.py 文件的作用
后端·python·django
小林学习编程7 小时前
SpringBoot校园失物招领信息平台
java·spring boot·后端
愿你天黑有灯下雨有伞7 小时前
Spring Boot整合Kafka实战指南:从环境搭建到消息处理全解析
spring boot·kafka·linq
Clf丶忆笙8 小时前
SpringBoot异步处理@Async深度解析:从基础到高阶实战
spring boot
java1234_小锋8 小时前
Spring Bean有哪几种配置方式?
java·后端·spring
柯南二号9 小时前
【后端】SpringBoot用CORS解决无法跨域访问的问题
java·spring boot·后端
每天一个秃顶小技巧10 小时前
02.Golang 切片(slice)源码分析(一、定义与基础操作实现)
开发语言·后端·python·golang
帮帮志10 小时前
vue实现与后台springboot传递数据【传值/取值 Axios 】
前端·vue.js·spring boot
gCode Teacher 格码致知11 小时前
《Asp.net Mvc 网站开发》复习试题
后端·asp.net·mvc
杨不易呀12 小时前
Java面试高阶篇:Spring Boot+Quarkus+Redis高并发架构设计与性能优化实战
spring boot·redis·高并发·分布式锁·java面试·quarkus