|
@@ -1,14 +1,24 @@
|
|
|
package com.chelvc.framework.aliyun;
|
|
|
|
|
|
+import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
|
|
|
import com.aliyun.captcha20230305.models.VerifyIntelligentCaptchaRequest;
|
|
|
import com.aliyun.captcha20230305.models.VerifyIntelligentCaptchaResponse;
|
|
|
import com.aliyun.dypnsapi20170525.models.GetMobileRequest;
|
|
|
import com.aliyun.dypnsapi20170525.models.GetMobileResponse;
|
|
|
+import com.aliyun.green20220302.Client;
|
|
|
+import com.aliyun.green20220302.models.ImageModerationRequest;
|
|
|
+import com.aliyun.green20220302.models.ImageModerationResponse;
|
|
|
+import com.aliyun.green20220302.models.TextModerationRequest;
|
|
|
+import com.aliyun.green20220302.models.TextModerationResponse;
|
|
|
import com.aliyun.teaopenapi.models.Config;
|
|
|
import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
import com.chelvc.framework.aliyun.config.AliyunProperties;
|
|
|
+import com.chelvc.framework.common.util.JacksonUtils;
|
|
|
+import com.chelvc.framework.common.util.ObjectUtils;
|
|
|
+import com.chelvc.framework.common.util.StringUtils;
|
|
|
+import com.google.common.collect.ImmutableMap;
|
|
|
import lombok.NonNull;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
@@ -21,15 +31,27 @@ import lombok.extern.slf4j.Slf4j;
|
|
|
@Slf4j
|
|
|
public class DefaultAliyunHandler implements AliyunHandler {
|
|
|
private final AliyunProperties properties;
|
|
|
+ private final com.aliyun.green20220302.Client green;
|
|
|
private final com.aliyun.captcha20230305.Client captcha;
|
|
|
private final com.aliyun.dypnsapi20170525.Client dypnsapi;
|
|
|
+ private final RuntimeOptions runtime = new RuntimeOptions();
|
|
|
|
|
|
public DefaultAliyunHandler(@NonNull AliyunProperties properties) {
|
|
|
this.properties = properties;
|
|
|
+ this.runtime.readTimeout = 10000;
|
|
|
+ this.runtime.connectTimeout = 10000;
|
|
|
|
|
|
try {
|
|
|
- // 初始化captcha客户端
|
|
|
+ // 初始化green客户端
|
|
|
Config config = this.getConfig(properties);
|
|
|
+ config.endpoint = "green-cip.cn-shanghai.aliyuncs.com";
|
|
|
+ config.setRegionId("cn-shanghai");
|
|
|
+ config.setReadTimeout(6000);
|
|
|
+ config.setConnectTimeout(3000);
|
|
|
+ this.green = new Client(config);
|
|
|
+
|
|
|
+ // 初始化captcha客户端
|
|
|
+ config = this.getConfig(properties);
|
|
|
config.endpoint = "captcha.cn-shanghai.aliyuncs.com";
|
|
|
this.captcha = new com.aliyun.captcha20230305.Client(config);
|
|
|
|
|
@@ -71,6 +93,60 @@ public class DefaultAliyunHandler implements AliyunHandler {
|
|
|
return response.body.getGetMobileResultDTO().getMobile();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Risk verifyAvatar(@NonNull String url) {
|
|
|
+ ImageModerationRequest request = new ImageModerationRequest();
|
|
|
+ request.setService("profilePhotoCheck");
|
|
|
+ request.setServiceParameters(JacksonUtils.serialize(ImmutableMap.of("imageUrl", url)));
|
|
|
+ ImageModerationResponse response;
|
|
|
+ try {
|
|
|
+ response = this.green.imageModerationWithOptions(request, this.runtime);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ if (response == null || response.body == null) {
|
|
|
+ throw new RuntimeException("Invalid aliyun response");
|
|
|
+ }
|
|
|
+ if (response.body.code != 200) {
|
|
|
+ log.error("Aliyun avatar green verify failed: {}, {}", response.body.code, response.body.msg);
|
|
|
+ return Risk.builder().level(RiskLevel.NONE).build();
|
|
|
+ }
|
|
|
+ RiskLevel level = RiskLevel.valueOf(response.body.data.riskLevel.toUpperCase());
|
|
|
+ if (level != RiskLevel.NONE && ObjectUtils.notEmpty(response.body.data.result)) {
|
|
|
+ String description = response.body.data.result.get(0).getDescription();
|
|
|
+ return Risk.builder().level(level).description(description).build();
|
|
|
+ }
|
|
|
+ return Risk.builder().level(level).build();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Risk verifyNickname(@NonNull String nickname) {
|
|
|
+ TextModerationRequest request = new TextModerationRequest();
|
|
|
+ request.setService("nickname_detection");
|
|
|
+ request.setServiceParameters(JacksonUtils.serialize(ImmutableMap.of("content", nickname)));
|
|
|
+ TextModerationResponse response;
|
|
|
+ try {
|
|
|
+ response = this.green.textModerationWithOptions(request, this.runtime);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ if (response == null || response.body == null) {
|
|
|
+ throw new RuntimeException("Invalid aliyun response");
|
|
|
+ }
|
|
|
+ if (response.body.code != 200) {
|
|
|
+ log.error("Aliyun nickname green verify failed: {}, {}", response.body.code, response.body.message);
|
|
|
+ return Risk.builder().level(RiskLevel.NONE).build();
|
|
|
+ }
|
|
|
+ if (StringUtils.notEmpty(response.body.data.reason)) {
|
|
|
+ Map<?, ?> reason = JacksonUtils.deserialize(response.body.data.reason, Map.class);
|
|
|
+ RiskLevel level = RiskLevel.valueOf(String.valueOf(reason.get("riskLevel")).toUpperCase());
|
|
|
+ if (level != RiskLevel.NONE) {
|
|
|
+ return Risk.builder().level(level).description(response.body.data.descriptions).build();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Risk.builder().level(RiskLevel.NONE).build();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public boolean verifyCaptcha(@NonNull String verification) {
|
|
|
VerifyIntelligentCaptchaRequest request = new VerifyIntelligentCaptchaRequest();
|