|
@@ -0,0 +1,209 @@
|
|
|
+package cn.com.qmth.examcloud.web.baidu;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+import org.apache.commons.compress.utils.IOUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.http.HttpStatus;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.exception.ExamCloudRuntimeException;
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.helpers.JsonHttpResponseHolder;
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLog;
|
|
|
+import cn.com.qmth.examcloud.commons.logging.ExamCloudLogFactory;
|
|
|
+import cn.com.qmth.examcloud.commons.util.JsonUtil;
|
|
|
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
+
|
|
|
+/**
|
|
|
+ * baidu 客户端
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2019年9月16日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
+public class BaiduClient {
|
|
|
+
|
|
|
+ protected static ExamCloudLog log = ExamCloudLogFactory.getLog(BaiduClient.class);
|
|
|
+
|
|
|
+ private static CloseableHttpClient httpclient;
|
|
|
+
|
|
|
+ private static RequestConfig requestConfig;
|
|
|
+
|
|
|
+ private static BaiduClient baiduClient;
|
|
|
+
|
|
|
+ private static String apiKey;
|
|
|
+
|
|
|
+ private static String apiSecret;
|
|
|
+
|
|
|
+ private BaiduClient() {
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取单例
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static BaiduClient getClient() {
|
|
|
+ if (null == baiduClient) {
|
|
|
+ synchronized (BaiduClient.class) {
|
|
|
+ if (null == baiduClient) {
|
|
|
+ baiduClient = new BaiduClient();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return baiduClient;
|
|
|
+ }
|
|
|
+
|
|
|
+ static {
|
|
|
+ PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(60,
|
|
|
+ TimeUnit.SECONDS);
|
|
|
+ cm.setValidateAfterInactivity(1000);
|
|
|
+ cm.setMaxTotal(8000);
|
|
|
+ cm.setDefaultMaxPerRoute(200);
|
|
|
+ httpclient = HttpClients.custom().setConnectionManager(cm).disableAutomaticRetries()
|
|
|
+ .build();
|
|
|
+
|
|
|
+ requestConfig = RequestConfig.custom().setConnectionRequestTimeout(500)
|
|
|
+ .setSocketTimeout(10000).setConnectTimeout(10000).build();
|
|
|
+
|
|
|
+ apiKey = PropertyHolder.getString("$baidu.apiKey");
|
|
|
+ apiSecret = PropertyHolder.getString("$baidu.apiSecret");
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(apiKey)) {
|
|
|
+ log.error("'facepp.apiKey' is not configured");
|
|
|
+ }
|
|
|
+ if (StringUtils.isBlank(apiSecret)) {
|
|
|
+ log.error("'facepp.apiSecret' is not configured");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 调用鉴权接口获取的token
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getAccessToken() {
|
|
|
+
|
|
|
+ String url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id="
|
|
|
+ + apiKey + "&client_secret=" + apiSecret;
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setConfig(BaiduClient.requestConfig);
|
|
|
+
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ JsonHttpResponseHolder responseHolder = null;
|
|
|
+ long s = System.currentTimeMillis();
|
|
|
+ try {
|
|
|
+
|
|
|
+ response = httpclient.execute(httpPost);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ String entityStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ JSONObject obj = JSON.parseObject(entityStr);
|
|
|
+ responseHolder = new JsonHttpResponseHolder(statusCode, obj);
|
|
|
+
|
|
|
+ if (HttpStatus.SC_OK != responseHolder.getStatusCode()) {
|
|
|
+ log.error("[Baidu AI]. getAccessToken; statusCode=" + statusCode
|
|
|
+ + "; responseEntity=" + entityStr);
|
|
|
+ } else {
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[Baidu AI]. getAccessToken; statusCode=" + statusCode
|
|
|
+ + "; responseEntity=" + entityStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ IOUtils.closeQuietly(response);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[Baidu AI]. getAccessToken; cost " + (System.currentTimeMillis() - s)
|
|
|
+ + " ms.");
|
|
|
+ }
|
|
|
+
|
|
|
+ int statusCode = responseHolder.getStatusCode();
|
|
|
+ if (HttpStatus.SC_OK != statusCode) {
|
|
|
+ throw new StatusException("901", "[Baidu AI]. fail to get access_token");
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject respBody = responseHolder.getRespBody();
|
|
|
+ String accessToken = respBody.getString("access_token");
|
|
|
+
|
|
|
+ return accessToken;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 活体检测
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @param imageUrl
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JsonHttpResponseHolder verifyFaceLiveness(String imageUrl) {
|
|
|
+
|
|
|
+ String accessToken = getAccessToken();
|
|
|
+ String url = "https://aip.baidubce.com/rest/2.0/face/v3/faceverify?access_token="
|
|
|
+ + accessToken;
|
|
|
+
|
|
|
+ HttpPost httpPost = new HttpPost(url);
|
|
|
+ httpPost.setConfig(BaiduClient.requestConfig);
|
|
|
+
|
|
|
+ Map<String, String> params = Maps.newHashMap();
|
|
|
+
|
|
|
+ params.put("image", imageUrl);
|
|
|
+ params.put("image_type", "imageUrl");
|
|
|
+
|
|
|
+ httpPost.setEntity(new StringEntity(JsonUtil.toJson(params), "UTF-8"));
|
|
|
+
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ JsonHttpResponseHolder responseHolder = null;
|
|
|
+ long s = System.currentTimeMillis();
|
|
|
+ try {
|
|
|
+
|
|
|
+ response = httpclient.execute(httpPost);
|
|
|
+ int statusCode = response.getStatusLine().getStatusCode();
|
|
|
+ String entityStr = EntityUtils.toString(response.getEntity(), "UTF-8");
|
|
|
+ JSONObject obj = JSON.parseObject(entityStr);
|
|
|
+ responseHolder = new JsonHttpResponseHolder(statusCode, obj);
|
|
|
+
|
|
|
+ if (HttpStatus.SC_OK != responseHolder.getStatusCode()) {
|
|
|
+ log.error("[Face++]. compare. statusCode=" + statusCode + "; responseEntity="
|
|
|
+ + entityStr);
|
|
|
+ } else {
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[Face++]. compare. statusCode=" + statusCode + "; responseEntity="
|
|
|
+ + entityStr);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ExamCloudRuntimeException(e);
|
|
|
+ } finally {
|
|
|
+ IOUtils.closeQuietly(response);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (log.isDebugEnabled()) {
|
|
|
+ log.debug("[Baidu AI]. compare. imageUrl=" + imageUrl + "; cost "
|
|
|
+ + (System.currentTimeMillis() - s) + " ms.");
|
|
|
+ }
|
|
|
+
|
|
|
+ return responseHolder;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|