Commit 5e783e7e584450fdb959b807722717bab15aeb7f
1 parent
0d15fe48
易盾工具类update
Showing
1 changed file
with
77 additions
and
73 deletions
src/main/java/com/cnlive/shenhe/utils/YiDunAntiFraudUtil.java
| @@ -74,12 +74,9 @@ public class YiDunAntiFraudUtil { | @@ -74,12 +74,9 @@ public class YiDunAntiFraudUtil { | ||
| 74 | private final static String IMAGE_RESULT = "https://as.dun.163yun.com/v4/image/check"; | 74 | private final static String IMAGE_RESULT = "https://as.dun.163yun.com/v4/image/check"; |
| 75 | 75 | ||
| 76 | public static void main(String[] args) { | 76 | public static void main(String[] args) { |
| 77 | - //智能网页识别审核测试 | ||
| 78 | - //getURLFilter(FILE_URL); | ||
| 79 | - //getURLFilter("http://3g.huabian.com/api/hz0227/show/440549.html"); | ||
| 80 | 77 | ||
| 81 | //智能文本识别审核测试 | 78 | //智能文本识别审核测试 |
| 82 | - getTextFilter("好人"); | 79 | + //getTextFilter("好人"); |
| 83 | 80 | ||
| 84 | //智能图片识别审核测试 | 81 | //智能图片识别审核测试 |
| 85 | /*JSONArray jsonArray = new JSONArray(); | 82 | /*JSONArray jsonArray = new JSONArray(); |
| @@ -98,11 +95,77 @@ public class YiDunAntiFraudUtil { | @@ -98,11 +95,77 @@ public class YiDunAntiFraudUtil { | ||
| 98 | image3.put("type", 1); | 95 | image3.put("type", 1); |
| 99 | image3.put("data", "https://wjj.ys2.cnliveimg.com/769/img/0/2020/03/21/158474856886077030277.jpg"); | 96 | image3.put("data", "https://wjj.ys2.cnliveimg.com/769/img/0/2020/03/21/158474856886077030277.jpg"); |
| 100 | jsonArray.add(image3); | 97 | jsonArray.add(image3); |
| 101 | - // | ||
| 102 | getImagesFilter(jsonArray.toJSONString());*/ | 98 | getImagesFilter(jsonArray.toJSONString());*/ |
| 103 | 99 | ||
| 100 | + //智能网页识别审核测试 | ||
| 101 | + //getURLFilter(FILE_URL); | ||
| 102 | + //getURLFilter("http://3g.huabian.com/api/hz0227/show/440549.html"); | ||
| 104 | } | 103 | } |
| 105 | 104 | ||
| 105 | + /** | ||
| 106 | + * 智能文本审核 | ||
| 107 | + * | ||
| 108 | + * @param text | ||
| 109 | + * @return | ||
| 110 | + */ | ||
| 111 | + public static JSONObject getTextFilter(String text) { | ||
| 112 | + JSONObject jsonObj = new JSONObject(); | ||
| 113 | + try { | ||
| 114 | + Map<String, String> params = new HashMap<String, String>(); | ||
| 115 | + // 1.设置公共参数 | ||
| 116 | + params.put("secretId", TEXTANDIMAGESECRETID); | ||
| 117 | + params.put("businessId", TEXTBUSINESSID); | ||
| 118 | + params.put("version", "v3.1"); | ||
| 119 | + params.put("timestamp", String.valueOf(System.currentTimeMillis())); | ||
| 120 | + params.put("nonce", String.valueOf(new Random().nextInt())); | ||
| 121 | + | ||
| 122 | + // 2.设置私有参数 | ||
| 123 | + params.put("dataId", UUID.randomUUID().toString()); | ||
| 124 | + params.put("content", text); | ||
| 125 | + | ||
| 126 | + // 3.生成签名信息 | ||
| 127 | + String signature = genSignature(TEXTANDIMAGESECRETKEY, params); | ||
| 128 | + params.put("signature", signature); | ||
| 129 | + | ||
| 130 | + // 4.发送HTTP请求 | ||
| 131 | + String result = HttpClientUtils.post_str(TEXT_RESULT, params); | ||
| 132 | + logger.info("易盾智能文本识别接口返回信息:{}", result); | ||
| 133 | + | ||
| 134 | + if (!StringUtils.isEmpty(result)) { | ||
| 135 | + JSONObject jsonResult = JSONObject.parseObject(result); | ||
| 136 | + int code = jsonResult.getIntValue("code"); | ||
| 137 | + String msg = jsonResult.getString("msg"); | ||
| 138 | + JSONObject resultJsonObject = jsonResult.getJSONObject("result"); | ||
| 139 | + if (code == 200 && "ok".equals(msg) && !StringUtils.isEmpty(resultJsonObject)) { | ||
| 140 | + //检测结果,0:通过,1:嫌疑,2:不通过 | ||
| 141 | + int action = resultJsonObject.getIntValue("action"); | ||
| 142 | + String taskId = resultJsonObject.getString("taskId"); | ||
| 143 | + if (action == 0) { | ||
| 144 | + jsonObj.put("type", "PASS"); | ||
| 145 | + jsonObj.put("desc", "易盾审核通过"); | ||
| 146 | + } | ||
| 147 | + if (action == 1) { | ||
| 148 | + jsonObj.put("type", "REJECT"); | ||
| 149 | + jsonObj.put("desc", "文本嫌疑"); | ||
| 150 | + } | ||
| 151 | + if (action == 2) { | ||
| 152 | + jsonObj.put("type", "REJECT"); | ||
| 153 | + jsonObj.put("desc", "文本违规"); | ||
| 154 | + } | ||
| 155 | + jsonObj.put("taskId", taskId); | ||
| 156 | + logger.info("审核云文本识别处理后结果:{}", jsonObj.toString()); | ||
| 157 | + return jsonObj; | ||
| 158 | + } | ||
| 159 | + | ||
| 160 | + } | ||
| 161 | + } catch (Exception e) { | ||
| 162 | + e.printStackTrace(); | ||
| 163 | + } | ||
| 164 | + jsonObj.put("type", "REJECT"); | ||
| 165 | + jsonObj.put("desc", "易盾文本识别其它问题需排查"); | ||
| 166 | + logger.info("审核云文本识别处理后结果:{}", jsonObj.toString()); | ||
| 167 | + return jsonObj; | ||
| 168 | + } | ||
| 106 | 169 | ||
| 107 | /** | 170 | /** |
| 108 | * 智能图片审核 | 171 | * 智能图片审核 |
| @@ -184,72 +247,7 @@ public class YiDunAntiFraudUtil { | @@ -184,72 +247,7 @@ public class YiDunAntiFraudUtil { | ||
| 184 | } | 247 | } |
| 185 | 248 | ||
| 186 | /** | 249 | /** |
| 187 | - * 智能文本审核 | ||
| 188 | - * | ||
| 189 | - * @param text | ||
| 190 | - * @return | ||
| 191 | - */ | ||
| 192 | - public static JSONObject getTextFilter(String text) { | ||
| 193 | - JSONObject jsonObj = new JSONObject(); | ||
| 194 | - try { | ||
| 195 | - Map<String, String> params = new HashMap<String, String>(); | ||
| 196 | - // 1.设置公共参数 | ||
| 197 | - params.put("secretId", TEXTANDIMAGESECRETID); | ||
| 198 | - params.put("businessId", TEXTBUSINESSID); | ||
| 199 | - params.put("version", "v3.1"); | ||
| 200 | - params.put("timestamp", String.valueOf(System.currentTimeMillis())); | ||
| 201 | - params.put("nonce", String.valueOf(new Random().nextInt())); | ||
| 202 | - | ||
| 203 | - // 2.设置私有参数 | ||
| 204 | - params.put("dataId", UUID.randomUUID().toString()); | ||
| 205 | - params.put("content", text); | ||
| 206 | - | ||
| 207 | - // 3.生成签名信息 | ||
| 208 | - String signature = genSignature(TEXTANDIMAGESECRETKEY, params); | ||
| 209 | - params.put("signature", signature); | ||
| 210 | - | ||
| 211 | - // 4.发送HTTP请求 | ||
| 212 | - String result = HttpClientUtils.post_str(TEXT_RESULT, params); | ||
| 213 | - logger.info("易盾智能文本识别接口返回信息:{}", result); | ||
| 214 | - | ||
| 215 | - if (!StringUtils.isEmpty(result)) { | ||
| 216 | - JSONObject jsonResult = JSONObject.parseObject(result); | ||
| 217 | - int code = jsonResult.getIntValue("code"); | ||
| 218 | - String msg = jsonResult.getString("msg"); | ||
| 219 | - JSONObject resultJsonObject = jsonResult.getJSONObject("result"); | ||
| 220 | - if (code == 200 && "ok".equals(msg) && !StringUtils.isEmpty(resultJsonObject)) { | ||
| 221 | - //检测结果,0:通过,1:嫌疑,2:不通过 | ||
| 222 | - int action = resultJsonObject.getIntValue("action"); | ||
| 223 | - String taskId = resultJsonObject.getString("taskId"); | ||
| 224 | - if (action == 0) { | ||
| 225 | - jsonObj.put("type", "PASS"); | ||
| 226 | - jsonObj.put("desc", "易盾审核通过"); | ||
| 227 | - } | ||
| 228 | - if (action == 1) { | ||
| 229 | - jsonObj.put("type", "REJECT"); | ||
| 230 | - jsonObj.put("desc", "文本嫌疑"); | ||
| 231 | - } | ||
| 232 | - if (action == 2) { | ||
| 233 | - jsonObj.put("type", "REJECT"); | ||
| 234 | - jsonObj.put("desc", "文本违规"); | ||
| 235 | - } | ||
| 236 | - jsonObj.put("taskId", taskId); | ||
| 237 | - logger.info("审核云文本识别处理后结果:{}", jsonObj.toString()); | ||
| 238 | - return jsonObj; | ||
| 239 | - } | ||
| 240 | - | ||
| 241 | - } | ||
| 242 | - } catch (Exception e) { | ||
| 243 | - e.printStackTrace(); | ||
| 244 | - } | ||
| 245 | - jsonObj.put("type", "REJECT"); | ||
| 246 | - jsonObj.put("desc", "易盾文本识别其它问题需排查"); | ||
| 247 | - logger.info("审核云文本识别处理后结果:{}", jsonObj.toString()); | ||
| 248 | - return jsonObj; | ||
| 249 | - } | ||
| 250 | - | ||
| 251 | - /** | ||
| 252 | - * 智能网页识别 | 250 | + * 图文识别检测接口+获取图文检测结果接口(用于测试) |
| 253 | * | 251 | * |
| 254 | * @param url | 252 | * @param url |
| 255 | * @return | 253 | * @return |
| @@ -280,7 +278,7 @@ public class YiDunAntiFraudUtil { | @@ -280,7 +278,7 @@ public class YiDunAntiFraudUtil { | ||
| 280 | // 4.发送HTTP请求 | 278 | // 4.发送HTTP请求 |
| 281 | //String response = HttpClient4Utils.sendPost(httpClient, API_URL, params, Consts.UTF_8); | 279 | //String response = HttpClient4Utils.sendPost(httpClient, API_URL, params, Consts.UTF_8); |
| 282 | String s = HttpClientUtils.post_str(URL_SUBMIT, params); | 280 | String s = HttpClientUtils.post_str(URL_SUBMIT, params); |
| 283 | - System.out.println(s); | 281 | + System.out.println("图文检测提交url结果" + s); |
| 284 | 282 | ||
| 285 | 283 | ||
| 286 | Map<String, String> params1 = new HashMap<String, String>(); | 284 | Map<String, String> params1 = new HashMap<String, String>(); |
| @@ -296,13 +294,19 @@ public class YiDunAntiFraudUtil { | @@ -296,13 +294,19 @@ public class YiDunAntiFraudUtil { | ||
| 296 | 294 | ||
| 297 | // 3.发送HTTP请求 | 295 | // 3.发送HTTP请求 |
| 298 | String s1 = HttpClientUtils.post_str(URL_RESULT, params1); | 296 | String s1 = HttpClientUtils.post_str(URL_RESULT, params1); |
| 299 | - System.out.println(s1); | 297 | + System.out.println("图文检测获取url结果" + s1); |
| 300 | 298 | ||
| 301 | // 4.解析接口返回值 | 299 | // 4.解析接口返回值 |
| 302 | JSONObject resultObject = JSONObject.parseObject(s1); | 300 | JSONObject resultObject = JSONObject.parseObject(s1); |
| 303 | return resultObject; | 301 | return resultObject; |
| 304 | } | 302 | } |
| 305 | 303 | ||
| 304 | + /** | ||
| 305 | + * 图文识别检测接口 | ||
| 306 | + * | ||
| 307 | + * @param url | ||
| 308 | + * @return | ||
| 309 | + */ | ||
| 306 | public static JSONObject submitYiDunUrl(String url) { | 310 | public static JSONObject submitYiDunUrl(String url) { |
| 307 | try { | 311 | try { |
| 308 | Map<String, String> params = new HashMap<String, String>(); | 312 | Map<String, String> params = new HashMap<String, String>(); |