Initial commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.oneone.cloud</groupId>
|
||||
<artifactId>oneone-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>common-sensitive</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.oneone.cloud</groupId>
|
||||
<artifactId>common-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>green20220302</artifactId>
|
||||
<version>1.0.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
package com.oneone.common.sensitive;
|
||||
|
||||
import com.oneone.common.sensitive.sensitiveimage.AliSensitiveImageService;
|
||||
import com.oneone.common.sensitive.sensitiveimage.SensitiveImageClient;
|
||||
import com.oneone.common.sensitive.sensitiveimage.SensitiveImageProperties;
|
||||
import com.oneone.common.sensitive.sensitiveimage.Xp;
|
||||
import com.oneone.common.sensitive.sensitivewords.AliSensitiveWordsService;
|
||||
import com.oneone.common.sensitive.sensitivewords.FqSensitiveWordsService;
|
||||
import com.oneone.common.sensitive.sensitivewords.SensitiveProperties;
|
||||
import com.oneone.common.sensitive.sensitivewords.SensitiveWordsClient;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 19:20
|
||||
*/
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
@EnableConfigurationProperties({SensitiveProperties.class,SensitiveImageProperties.class})
|
||||
public class SensitiveAutoConfiguration {
|
||||
private final SensitiveProperties sensitiveProperties;
|
||||
private final SensitiveImageProperties sensitiveImageProperties;
|
||||
|
||||
@Bean
|
||||
public FqSensitiveWordsService fqSensitiveWordsService() {
|
||||
return new FqSensitiveWordsService(sensitiveProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AliSensitiveWordsService aliSensitiveWordsService() {
|
||||
return new AliSensitiveWordsService(sensitiveProperties.getAli());
|
||||
}
|
||||
|
||||
@Bean
|
||||
SensitiveWordsClient sensitiveWordsClient() {
|
||||
return new SensitiveWordsClient(sensitiveProperties, fqSensitiveWordsService(),aliSensitiveWordsService());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public Xp xq() {
|
||||
return new Xp(sensitiveImageProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AliSensitiveImageService aliSensitiveImageService() {
|
||||
return new AliSensitiveImageService(sensitiveImageProperties.getAli());
|
||||
}
|
||||
@Bean
|
||||
SensitiveImageClient sensitiveImageClient() {
|
||||
return new SensitiveImageClient(sensitiveImageProperties, xq(),aliSensitiveImageService());
|
||||
}
|
||||
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.oneone.common.sensitive.sensitiveimage;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.green20220302.Client;
|
||||
import com.aliyun.green20220302.models.ImageModerationRequest;
|
||||
import com.aliyun.green20220302.models.ImageModerationResponse;
|
||||
import com.aliyun.green20220302.models.ImageModerationResponseBody;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-26 10:21
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor
|
||||
public class AliSensitiveImageService implements SensitiveImageService {
|
||||
private Client client;
|
||||
|
||||
public AliSensitiveImageService(SensitiveImageProperties.Ali properties) {
|
||||
Config config = new Config();
|
||||
config.setAccessKeyId(properties.getAccessKeyId());
|
||||
config.setAccessKeySecret(properties.getAccessKeySecret());
|
||||
//接入区域和地址请根据实际情况修改
|
||||
config.setRegionId(properties.getRegionId());
|
||||
config.setEndpoint(properties.getEndpoint());
|
||||
//连接时超时时间,单位毫秒(ms)。
|
||||
config.setReadTimeout(6000);
|
||||
//读取时超时时间,单位毫秒(ms)。
|
||||
config.setConnectTimeout(3000);
|
||||
//设置http代理。
|
||||
//config.setHttpProxy("http://10.10.xx.xx:xxxx");
|
||||
//设置https代理。
|
||||
//config.setHttpsProxy("https://10.10.xx.xx:xxxx");
|
||||
// 注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能
|
||||
try {
|
||||
client = new Client(config);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* nickname_detection:用户昵称
|
||||
* <p>
|
||||
* chat_detection:私聊互动
|
||||
* <p>
|
||||
* comment_detection:公聊评论
|
||||
* <p>
|
||||
* ai_art_detection:AIGC文字指令
|
||||
* <p>
|
||||
* ad_compliance_detection:广告法合规
|
||||
* <p>
|
||||
* pgc_detection:教学物料PGC
|
||||
*
|
||||
* @param content
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean check(String content, String type) {
|
||||
// 创建RuntimeObject实例并设置运行参数。
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
runtime.readTimeout = 10000;
|
||||
runtime.connectTimeout = 10000;
|
||||
|
||||
//检测参数构造
|
||||
JSONObject serviceParameters = new JSONObject();
|
||||
serviceParameters.put("imageUrl", content);
|
||||
serviceParameters.put("dataId", UUID.randomUUID().toString());
|
||||
|
||||
ImageModerationRequest request = new ImageModerationRequest();
|
||||
//图片检测service: baselineCheck 通用基线检测。
|
||||
request.setService(type);
|
||||
request.setServiceParameters(JSON.toJSONString(serviceParameters));
|
||||
|
||||
try {
|
||||
// 调用方法获取检测结果。
|
||||
ImageModerationResponse response = client.imageModerationWithOptions(request, runtime);
|
||||
// 打印检测结果。
|
||||
if (response != null) {
|
||||
if (response.getStatusCode() == 200) {
|
||||
ImageModerationResponseBody result = response.getBody();
|
||||
log.info("阿里敏感图片测结果:{}", JSON.toJSONString(result));
|
||||
Integer code = result.getCode();
|
||||
if (code != null && code == 200) {
|
||||
ImageModerationResponseBody.ImageModerationResponseBodyData data = result.getData();
|
||||
List<ImageModerationResponseBody.ImageModerationResponseBodyDataResult> results = data.getResult();
|
||||
for (ImageModerationResponseBody.ImageModerationResponseBodyDataResult d : results) {
|
||||
if ("nonLabel".equals(d.getLabel()) || "nonLabel_lib".equals(d.getLabel())) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
log.error("image moderation not success. code:" + code);
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
} else {
|
||||
log.error("response not success. status:" + response.getStatusCode());
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
}
|
||||
throw new BusinessException("请稍后再试");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
}
|
||||
+311
@@ -0,0 +1,311 @@
|
||||
package com.oneone.common.sensitive.sensitiveimage;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpDelete;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpPut;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.conn.scheme.Scheme;
|
||||
import org.apache.http.conn.scheme.SchemeRegistry;
|
||||
import org.apache.http.conn.ssl.SSLSocketFactory;
|
||||
import org.apache.http.entity.ByteArrayEntity;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManager;
|
||||
import javax.net.ssl.X509TrustManager;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HttpUtils {
|
||||
|
||||
/**
|
||||
* get
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doGet(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpGet request = new HttpGet(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* post form
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param bodys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
Map<String, String> bodys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (bodys != null) {
|
||||
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
|
||||
|
||||
for (String key : bodys.keySet()) {
|
||||
nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
|
||||
}
|
||||
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
|
||||
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
|
||||
request.setEntity(formEntity);
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post String
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Post stream
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPost(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPost request = new HttpPost(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put String
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
String body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(body)) {
|
||||
request.setEntity(new StringEntity(body, "utf-8"));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Put stream
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @param body
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doPut(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys,
|
||||
byte[] body)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpPut request = new HttpPut(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
if (body != null) {
|
||||
request.setEntity(new ByteArrayEntity(body));
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete
|
||||
*
|
||||
* @param host
|
||||
* @param path
|
||||
* @param method
|
||||
* @param headers
|
||||
* @param querys
|
||||
* @return
|
||||
* @throws Exception
|
||||
*/
|
||||
public static HttpResponse doDelete(String host, String path, String method,
|
||||
Map<String, String> headers,
|
||||
Map<String, String> querys)
|
||||
throws Exception {
|
||||
HttpClient httpClient = wrapClient(host);
|
||||
|
||||
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
|
||||
for (Map.Entry<String, String> e : headers.entrySet()) {
|
||||
request.addHeader(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
return httpClient.execute(request);
|
||||
}
|
||||
|
||||
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
|
||||
StringBuilder sbUrl = new StringBuilder();
|
||||
sbUrl.append(host);
|
||||
if (!StringUtils.isBlank(path)) {
|
||||
sbUrl.append(path);
|
||||
}
|
||||
if (null != querys) {
|
||||
StringBuilder sbQuery = new StringBuilder();
|
||||
for (Map.Entry<String, String> query : querys.entrySet()) {
|
||||
if (0 < sbQuery.length()) {
|
||||
sbQuery.append("&");
|
||||
}
|
||||
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append(query.getValue());
|
||||
}
|
||||
if (!StringUtils.isBlank(query.getKey())) {
|
||||
sbQuery.append(query.getKey());
|
||||
if (!StringUtils.isBlank(query.getValue())) {
|
||||
sbQuery.append("=");
|
||||
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (0 < sbQuery.length()) {
|
||||
sbUrl.append("?").append(sbQuery);
|
||||
}
|
||||
}
|
||||
|
||||
return sbUrl.toString();
|
||||
}
|
||||
|
||||
private static HttpClient wrapClient(String host) {
|
||||
HttpClient httpClient = new DefaultHttpClient();
|
||||
if (host.startsWith("https://")) {
|
||||
sslClient(httpClient);
|
||||
}
|
||||
|
||||
return httpClient;
|
||||
}
|
||||
|
||||
private static void sslClient(HttpClient httpClient) {
|
||||
try {
|
||||
SSLContext ctx = SSLContext.getInstance("TLS");
|
||||
X509TrustManager tm = new X509TrustManager() {
|
||||
public X509Certificate[] getAcceptedIssuers() {
|
||||
return null;
|
||||
}
|
||||
public void checkClientTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
public void checkServerTrusted(X509Certificate[] xcs, String str) {
|
||||
|
||||
}
|
||||
};
|
||||
ctx.init(null, new TrustManager[] { tm }, null);
|
||||
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
|
||||
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||
ClientConnectionManager ccm = httpClient.getConnectionManager();
|
||||
SchemeRegistry registry = ccm.getSchemeRegistry();
|
||||
registry.register(new Scheme("https", 443, ssf));
|
||||
} catch (KeyManagementException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
} catch (NoSuchAlgorithmException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.oneone.common.sensitive.sensitiveimage;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 18:49
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class SensitiveImageClient {
|
||||
|
||||
private SensitiveImageProperties sensitiveImageProperties;
|
||||
private Xp xp;
|
||||
private AliSensitiveImageService aliSensitiveImageService;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param content
|
||||
* @param imageType:imgUrl,base64
|
||||
* @return
|
||||
*/
|
||||
public boolean check(String content,SensitiveImageType imageType) {
|
||||
if (sensitiveImageProperties.getType().equals("ali")) {
|
||||
return aliSensitiveImageService.check(content,imageType.getCode());
|
||||
}else if (sensitiveImageProperties.getType().equals("xp")) {
|
||||
return xp.check(content,"imgUrl");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.oneone.common.sensitive.sensitiveimage;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 18:31
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "sensitive.image")
|
||||
@Configuration
|
||||
public class SensitiveImageProperties {
|
||||
|
||||
private String type = "ali";
|
||||
|
||||
private Xp xp = new Xp();
|
||||
|
||||
private Ali ali = new Ali();
|
||||
|
||||
|
||||
@Data
|
||||
public static class Xp {
|
||||
private String host = "http://imgaudit.market.alicloudapi.com/greenImg";
|
||||
private String appcode = "9737d843a0664621b57b4aaa043351b1";
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Ali {
|
||||
private String regionId = "cn-chengdu";
|
||||
private String endpoint = "green-cip.cn-chengdu.aliyuncs.com";
|
||||
private String accessKeyId = "LTAI5tJvgpJEDMNyzgN9MYPp";
|
||||
private String accessKeySecret = "gpmHu00i0Sbpmmxvqq4XWBvjov7FAq";
|
||||
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.oneone.common.sensitive.sensitiveimage;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 18:52
|
||||
*/
|
||||
public interface SensitiveImageService {
|
||||
|
||||
/**
|
||||
* 敏感词检测
|
||||
* @param content
|
||||
* @param imageType:imgUrl,base64
|
||||
* @return
|
||||
*/
|
||||
boolean check(String content,String imageType);
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.oneone.common.sensitive.sensitiveimage;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-26 11:04
|
||||
*/
|
||||
public enum SensitiveImageType {
|
||||
BASELINE_CHECK("baselineCheck", "通用基线检测"),
|
||||
BASELINE_CHECK_PRO("baselineCheck_pro", "通用基线检测_专业版"),
|
||||
AIGC_CHECK("aigcCheck", "AIGC图片检测"),
|
||||
;
|
||||
|
||||
@Getter
|
||||
private String code;
|
||||
@Getter
|
||||
private String msg;
|
||||
|
||||
SensitiveImageType(String code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
package com.oneone.common.sensitive.sensitiveimage;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 18:54
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class Xp implements SensitiveImageService {
|
||||
|
||||
private SensitiveImageProperties sensitiveImageProperties;
|
||||
|
||||
@Override
|
||||
public boolean check(String content,String imageType) {
|
||||
String method = "POST";
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
|
||||
headers.put("Authorization", "APPCODE " + sensitiveImageProperties.getXp().getAppcode());
|
||||
//根据API的要求,定义相对应的Content-Type
|
||||
headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
|
||||
Map<String, String> querys = new HashMap<String, String>();
|
||||
Map<String, String> bodys = new HashMap<String, String>();
|
||||
bodys.put(imageType, content);
|
||||
|
||||
bodys.put("type", "1");
|
||||
|
||||
|
||||
try {
|
||||
/**
|
||||
* 重要提示如下:
|
||||
* HttpUtils请从
|
||||
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
|
||||
* 下载
|
||||
*
|
||||
* 相应的依赖请参照
|
||||
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
|
||||
*/
|
||||
HttpResponse response = HttpUtils.doPost(sensitiveImageProperties.getXp().getHost(), "", method, headers, querys, bodys);
|
||||
Integer status = response.getStatusLine().getStatusCode();
|
||||
String json = EntityUtils.toString(response.getEntity());
|
||||
log.info("xp敏感图片服务-返回结果:{}", json);
|
||||
if (status == 200) {
|
||||
JSONObject result = JSONObject.parseObject(json);
|
||||
if (result.getInteger("showapi_res_code") != 0) {
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
return !result.getJSONObject("showapi_res_body").getString("code").equals("normal");
|
||||
} else {
|
||||
log.error("fq敏感图服务-异常,httpCode:{}", status);
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
|
||||
}
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
package com.oneone.common.sensitive.sensitivewords;
|
||||
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import lombok.NoArgsConstructor;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.aliyun.green20220302.Client;
|
||||
import com.aliyun.green20220302.models.TextModerationRequest;
|
||||
import com.aliyun.green20220302.models.TextModerationResponse;
|
||||
import com.aliyun.green20220302.models.TextModerationResponseBody;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-26 10:21
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor
|
||||
public class AliSensitiveWordsService implements SensitiveWordsService{
|
||||
private Client client;
|
||||
|
||||
public AliSensitiveWordsService(SensitiveProperties.Ali properties) {
|
||||
Config config = new Config();
|
||||
config.setAccessKeyId(properties.getAccessKeyId());
|
||||
config.setAccessKeySecret(properties.getAccessKeySecret());
|
||||
//接入区域和地址请根据实际情况修改
|
||||
config.setRegionId(properties.getRegionId());
|
||||
config.setEndpoint(properties.getEndpoint());
|
||||
//连接时超时时间,单位毫秒(ms)。
|
||||
config.setReadTimeout(6000);
|
||||
//读取时超时时间,单位毫秒(ms)。
|
||||
config.setConnectTimeout(3000);
|
||||
//设置http代理。
|
||||
//config.setHttpProxy("http://10.10.xx.xx:xxxx");
|
||||
//设置https代理。
|
||||
//config.setHttpsProxy("https://10.10.xx.xx:xxxx");
|
||||
// 注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能
|
||||
try {
|
||||
client = new Client(config);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public boolean check(String content) {
|
||||
return check(content,"chat_detection");
|
||||
}
|
||||
|
||||
/**
|
||||
* nickname_detection:用户昵称
|
||||
*
|
||||
* chat_detection:私聊互动
|
||||
*
|
||||
* comment_detection:公聊评论
|
||||
*
|
||||
* ai_art_detection:AIGC文字指令
|
||||
*
|
||||
* ad_compliance_detection:广告法合规
|
||||
*
|
||||
* pgc_detection:教学物料PGC
|
||||
* @param content
|
||||
* @param type
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean check(String content, String type) {
|
||||
// 创建RuntimeObject实例并设置运行参数。
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
runtime.readTimeout = 10000;
|
||||
runtime.connectTimeout = 10000;
|
||||
|
||||
//检测参数构造
|
||||
JSONObject serviceParameters = new JSONObject();
|
||||
serviceParameters.put("content", content);
|
||||
|
||||
if (serviceParameters.get("content") == null || serviceParameters.getString("content").trim().length() == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TextModerationRequest textModerationRequest = new TextModerationRequest();
|
||||
/*
|
||||
文本检测服务 service code
|
||||
*/
|
||||
textModerationRequest.setService(type);
|
||||
textModerationRequest.setServiceParameters(serviceParameters.toJSONString());
|
||||
try {
|
||||
// 调用方法获取检测结果。
|
||||
TextModerationResponse response = client.textModerationWithOptions(textModerationRequest, runtime);
|
||||
// 打印检测结果。
|
||||
if (response != null) {
|
||||
if (response.getStatusCode() == 200) {
|
||||
TextModerationResponseBody result = response.getBody();
|
||||
log.info("阿里敏感词检测结果:{}",JSON.toJSONString(result));
|
||||
Integer code = result.getCode();
|
||||
if (code != null && code == 200) {
|
||||
TextModerationResponseBody.TextModerationResponseBodyData data = result.getData();
|
||||
if (StringUtils.isEmpty(data.getLabels())) {
|
||||
return false;
|
||||
}else {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
log.error("text moderation not success. code:" + code);
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
} else {
|
||||
log.error("response not success. status:" + response.getStatusCode());
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
}
|
||||
throw new BusinessException("请稍后再试");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Config config = new Config();
|
||||
config.setAccessKeyId("LTAI5tJvgpJEDMNyzgN9MYPp");
|
||||
config.setAccessKeySecret("gpmHu00i0Sbpmmxvqq4XWBvjov7FAq");
|
||||
//接入区域和地址请根据实际情况修改
|
||||
config.setRegionId("cn-shanghai");
|
||||
config.setEndpoint("green-cip.cn-shanghai.aliyuncs.com");
|
||||
//连接时超时时间,单位毫秒(ms)。
|
||||
config.setReadTimeout(6000);
|
||||
//读取时超时时间,单位毫秒(ms)。
|
||||
config.setConnectTimeout(3000);
|
||||
//设置http代理。
|
||||
//config.setHttpProxy("http://10.10.xx.xx:xxxx");
|
||||
//设置https代理。
|
||||
//config.setHttpsProxy("https://10.10.xx.xx:xxxx");
|
||||
// 注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能
|
||||
Client client = new Client(config);
|
||||
|
||||
// 创建RuntimeObject实例并设置运行参数。
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
runtime.readTimeout = 10000;
|
||||
runtime.connectTimeout = 10000;
|
||||
|
||||
//检测参数构造
|
||||
JSONObject serviceParameters = new JSONObject();
|
||||
serviceParameters.put("content", "< input text >");
|
||||
|
||||
if (serviceParameters.get("content") == null || serviceParameters.getString("content").trim().length() == 0) {
|
||||
System.out.println("text moderation content is empty");
|
||||
return;
|
||||
}
|
||||
|
||||
TextModerationRequest textModerationRequest = new TextModerationRequest();
|
||||
/*
|
||||
文本检测服务 service code
|
||||
*/
|
||||
textModerationRequest.setService("comment_detection");
|
||||
textModerationRequest.setServiceParameters(serviceParameters.toJSONString());
|
||||
try {
|
||||
// 调用方法获取检测结果。
|
||||
TextModerationResponse response = client.textModerationWithOptions(textModerationRequest, runtime);
|
||||
|
||||
// 自动路由。
|
||||
if (response != null) {
|
||||
// 服务端错误,区域切换到cn-beijing。
|
||||
if (500 == response.getStatusCode() || (response.getBody() != null && 500 == (response.getBody().getCode()))) {
|
||||
// 接入区域和地址请根据实际情况修改。
|
||||
config.setRegionId("cn-beijing");
|
||||
config.setEndpoint("green-cip.cn-beijing.aliyuncs.com");
|
||||
client = new Client(config);
|
||||
response = client.textModerationWithOptions(textModerationRequest, runtime);
|
||||
}
|
||||
|
||||
}
|
||||
// 打印检测结果。
|
||||
if (response != null) {
|
||||
if (response.getStatusCode() == 200) {
|
||||
TextModerationResponseBody result = response.getBody();
|
||||
System.out.println(JSON.toJSONString(result));
|
||||
Integer code = result.getCode();
|
||||
if (code != null && code == 200) {
|
||||
TextModerationResponseBody.TextModerationResponseBodyData data = result.getData();
|
||||
System.out.println("labels = [" + data.getLabels() + "]");
|
||||
System.out.println("reason = [" + data.getReason() + "]");
|
||||
} else {
|
||||
System.out.println("text moderation not success. code:" + code);
|
||||
}
|
||||
} else {
|
||||
System.out.println("response not success. status:" + response.getStatusCode());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
package com.oneone.common.sensitive.sensitivewords;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 18:54
|
||||
*/
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class FqSensitiveWordsService implements SensitiveWordsService {
|
||||
|
||||
private SensitiveProperties sensitiveProperties;
|
||||
|
||||
@Override
|
||||
public boolean check(String content) {
|
||||
return check(content, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(String content, String type) {
|
||||
try {
|
||||
URL url = new URL(sensitiveProperties.getFq().getHost());
|
||||
HttpURLConnection httpURLCon = (HttpURLConnection) url.openConnection();
|
||||
httpURLCon.setRequestMethod("POST");
|
||||
httpURLCon.setRequestProperty("Authorization", "APPCODE " + sensitiveProperties.getFq().getAppcode());// 格式Authorization:APPCODE
|
||||
StringBuilder postData = new StringBuilder("content=");
|
||||
postData.append(content);
|
||||
postData.append("&type=0");
|
||||
|
||||
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
|
||||
httpURLCon.setDoOutput(true);
|
||||
OutputStream out = httpURLCon.getOutputStream();
|
||||
out.write(postDataBytes);
|
||||
out.close();
|
||||
int httpCode = httpURLCon.getResponseCode();
|
||||
if (httpCode == 200) {
|
||||
String json = read(httpURLCon.getInputStream());
|
||||
JSONObject result = JSONObject.parseObject(json);
|
||||
log.info("fq敏感词服务-返回结果:{}", result);
|
||||
return result.getString("status").equals("02");
|
||||
} else {
|
||||
Map<String, List<String>> map = httpURLCon.getHeaderFields();
|
||||
String error = map.get("X-Ca-Error-Message").get(0);
|
||||
log.error("fq敏感词服务-异常,httpCode:{},error:{}", httpCode, error);
|
||||
}
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
log.error("URL格式错误");
|
||||
} catch (UnknownHostException e) {
|
||||
log.error("URL地址错误");
|
||||
} catch (Exception e) {
|
||||
// 打开注释查看详细报错异常信息
|
||||
e.printStackTrace();
|
||||
}
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 读取返回结果
|
||||
*/
|
||||
private static String read(InputStream is) throws IOException {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is));
|
||||
String line = null;
|
||||
while ((line = br.readLine()) != null) {
|
||||
line = new String(line.getBytes(), "utf-8");
|
||||
sb.append(line);
|
||||
}
|
||||
br.close();
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.oneone.common.sensitive.sensitivewords;
|
||||
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.oneone.common.exception.BusinessException;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-26 10:21
|
||||
*/
|
||||
@Slf4j
|
||||
@NoArgsConstructor
|
||||
public class KpSensitiveWordsService implements SensitiveWordsService{
|
||||
private SensitiveProperties sensitiveProperties;
|
||||
|
||||
public KpSensitiveWordsService(SensitiveProperties properties) {
|
||||
this.sensitiveProperties = properties;
|
||||
}
|
||||
@Override
|
||||
public boolean check(String content) {
|
||||
return check(content,"null");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean check(String content, String type) {
|
||||
String s = HttpUtil.post(sensitiveProperties.getKp().getHost(), ImmutableMap.of("userKey",sensitiveProperties.getKp().getUserKey(),"content",content));
|
||||
if (StringUtils.isEmpty(s)){
|
||||
log.error("开普敏感词接口返回为空");
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
log.info("开普敏感词接口返回:{}",s);
|
||||
JSONObject result = JSONObject.parseObject(s);
|
||||
if (result.getInteger("code") != 0){
|
||||
log.error("开普敏感词接口返回错误:{}",result.getString("msg"));
|
||||
throw new BusinessException("请稍后再试");
|
||||
}
|
||||
JSONArray contents = result.getJSONArray("content");
|
||||
if (contents == null || contents.size() == 0){
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SensitiveProperties sensitiveProperties1 = new SensitiveProperties();
|
||||
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
//判断用户还有没有输入字符
|
||||
while (scanner.hasNext()) {
|
||||
String str = scanner.next();
|
||||
String s = HttpUtil.post(sensitiveProperties1.getKp().getHost(), ImmutableMap.of("userKey",sensitiveProperties1.getKp().getUserKey(),"content",str));
|
||||
if (StringUtils.isEmpty(s)){
|
||||
System.out.println("开普敏感词接口返回为空");
|
||||
}
|
||||
System.out.println("开普敏感词接口返回:"+s);
|
||||
}
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.oneone.common.sensitive.sensitivewords;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.dfa.FoundWord;
|
||||
import cn.hutool.dfa.SensitiveUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.io.ClassPathResource;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2022-04-15 20:15
|
||||
*/
|
||||
@Configuration
|
||||
@Slf4j
|
||||
public class SensitiveFilterConfig implements InitializingBean {
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws IOException {
|
||||
ClassPathResource resource = new ClassPathResource("sensi_words_back.txt");
|
||||
InputStream inputStream = resource.getInputStream();
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(
|
||||
inputStream
|
||||
, StandardCharsets.UTF_8));
|
||||
List<String> lines = new ArrayList<>();
|
||||
try {
|
||||
for (String line = reader.readLine(); line != null; line = reader.readLine()) {
|
||||
lines.add(line);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
reader.close();
|
||||
}
|
||||
SensitiveUtil.init(lines);
|
||||
log.info("初始化敏感词库成功");
|
||||
}
|
||||
|
||||
|
||||
public static String sensitiveWordFiltering(String text) {
|
||||
List<FoundWord> matchAll = SensitiveUtil.getFoundAllSensitive(text, false, false);
|
||||
if (matchAll.size() > 0) {
|
||||
for (FoundWord match : matchAll) {
|
||||
StringBuilder replace = new StringBuilder();
|
||||
for (int i = 0; i < StrUtil.length(match.getWord()); i++) {
|
||||
replace.append("*");
|
||||
}
|
||||
text = StrUtil.replace(text, match.getWord(), replace.toString());
|
||||
}
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package com.oneone.common.sensitive.sensitivewords;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 18:31
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "sensitive.words")
|
||||
@Configuration
|
||||
public class SensitiveProperties {
|
||||
|
||||
private String type = "ali";
|
||||
|
||||
private Fq fq = new Fq();
|
||||
private Ali ali = new Ali();
|
||||
|
||||
private Kp kp = new Kp();
|
||||
|
||||
@Data
|
||||
public static class Fq {
|
||||
private String host = "http://forwords.market.alicloudapi.com/words";
|
||||
private String appcode = "9737d843a0664621b57b4aaa043351b1";
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Ali {
|
||||
private String regionId = "cn-chengdu";
|
||||
private String endpoint = "green-cip.cn-chengdu.aliyuncs.com";
|
||||
private String accessKeyId = "LTAI5tJvgpJEDMNyzgN9MYPp";
|
||||
private String accessKeySecret = "gpmHu00i0Sbpmmxvqq4XWBvjov7FAq";
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
public static class Kp {
|
||||
private String host = "http://safeguard.ucap.com.cn/safe-guard-back/openApi/transferArithmetic";
|
||||
private String userKey = "3f47e10107eac26d6a1333df76d1e964";
|
||||
}
|
||||
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.oneone.common.sensitive.sensitivewords;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 18:49
|
||||
*/
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Slf4j
|
||||
public class SensitiveWordsClient {
|
||||
|
||||
private SensitiveProperties sensitiveProperties;
|
||||
private FqSensitiveWordsService fqSensitiveWordsService;
|
||||
private AliSensitiveWordsService aliSensitiveWordsService;
|
||||
|
||||
|
||||
public boolean check(String content) {
|
||||
/*Boolean hasSensitiveWords = SensitiveUtil.containsSensitive(content);
|
||||
if (hasSensitiveWords) {
|
||||
log.info("本地敏感词库拦截成功:{}", content);
|
||||
return true;
|
||||
}*/
|
||||
if (sensitiveProperties.getType().equals("fq")) {
|
||||
return fqSensitiveWordsService.check(content);
|
||||
}else if (sensitiveProperties.getType().equals("ali")) {
|
||||
return aliSensitiveWordsService.check(content);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean check(String content,SensitiveWordsType type) {
|
||||
/*Boolean hasSensitiveWords = SensitiveUtil.containsSensitive(content);
|
||||
if (hasSensitiveWords) {
|
||||
log.info("本地敏感词库拦截成功:{}", content);
|
||||
return true;
|
||||
}*/
|
||||
if (sensitiveProperties.getType().equals("fq")) {
|
||||
return fqSensitiveWordsService.check(content,null);
|
||||
}else if (sensitiveProperties.getType().equals("ali")) {
|
||||
return aliSensitiveWordsService.check(content,type.getCode());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.oneone.common.sensitive.sensitivewords;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-23 18:52
|
||||
*/
|
||||
public interface SensitiveWordsService {
|
||||
|
||||
/**
|
||||
* 敏感词检测
|
||||
* @param content
|
||||
* @return
|
||||
*/
|
||||
boolean check(String content);
|
||||
|
||||
/**
|
||||
* 敏感词检测
|
||||
* @param content
|
||||
* @return
|
||||
*/
|
||||
boolean check(String content,String type);
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.oneone.common.sensitive.sensitivewords;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author mice
|
||||
* @version 1.0
|
||||
* @date 2023-06-26 11:04
|
||||
*/
|
||||
public enum SensitiveWordsType {
|
||||
NICKNAME_DETECTION("nickname_detection", "用户昵称"),
|
||||
CHAT_DETECTION("chat_detection", "私聊互动"),
|
||||
COMMENT_DETECTION("comment_detection", "公聊评论"),
|
||||
AI_ART_DETECTION("ai_art_detection", "AIGC文字指令"),
|
||||
AD_COMPLIANCE_DETECTION("ad_compliance_detection", "广告法合规");
|
||||
|
||||
@Getter
|
||||
private String code;
|
||||
@Getter
|
||||
private String msg;
|
||||
|
||||
SensitiveWordsType(String code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.oneone.common.sensitive.SensitiveAutoConfiguration,\
|
||||
com.oneone.common.sensitive.sensitivewords.SensitiveFilterConfig
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user