百度短信接口以及人脸实名认证接口

2022-12-26 14:55:23 浏览数 (1)

添加MAVEN配置

代码语言:javascript复制
<!-- baidu -->
      <dependency>
          <groupId>com.baidubce</groupId>
          <artifactId>bce-java-sdk</artifactId>
          <version>${baidubce.version}</version>
          <!-- 去重避免报错 -->
          <exclusions>
              <exclusion>
                  <groupId>org.slf4j</groupId>
                  <artifactId>slf4j-log4j12</artifactId>
              </exclusion>
          </exclusions>
      </dependency>

创建百度常量类(百度API Key、百度Secret Key已经各个模板ID请自行申请)

代码语言:javascript复制
/**
 * @Description: 百度key
 * @BelongsProject: public-estimate-service
 * @BelongsPackage: com.clhb.publicestimateservice.common
 * @Author: LiYu
 * @CreateTime: 2021-03-10 09:14
 * @Email: 19740428lxh@gmail.com
 * @Version: 1.0
 */
public class BaiDuKeyConstant {

    /**
     * 百度API Key
     */
    public static final String API_KEY = "**********************";

    /**
     * 百度Secret Key
     */
    public static final String SECRET_KEY = "**********************";

    /**
     * 百度方案ID,请在人脸实名认证控制台查看创建的H5方案的方案ID信息
     */
    public static final String PLAN_ID = "**********************";

    /**
     * 获取百度Access Token地址
     */
    public static final String ACCESS_TOKEN_URL = "https://aip.baidubce.com/oauth/2.0/token";

    /**
     * 获取百度verify_token地址 POST
     */
    public static final String VERIFY_TOKEN = "https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/verifyToken/generate?access_token=";

    /**
     * 百度跳转到人脸验证接口地址
     */
    public static final String HTML5_URL = "https://brain.baidu.com/face/print";

    /**
     * 认证人脸地址
     */
    public static final String AUTHENTICATED_FACE_URL = " https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/simple?access_token=";

    /**
     * 认证结果地址
     */
    public static final String CERTIFICATION_RESULT_URL = "https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/detail?access_token=";

    /**
     * 查询统计结果地址
     */
    public static final String STATISTICAL_RESULTS_URL = "https://aip.baidubce.com/rpc/2.0/brain/solution/faceprint/result/stat?access_token=";

    /**
     * 认证成功地址
     */
    public static final String SUCCESS_URL = "https://xxx?id=";

    /**
     * 认证失败地址
     */
    public static final String FAILED_URL = "https://xxx?id=";

    /**
     * 百度云短信ACCESS_KEY_ID
     */
    public static final String ACCESS_KEY_ID = "**********************";

    /**
     * 百度云短信secret key
     */
    public static final String SECRET_ACCESS_KEY = "**********************";

    /**
     * 短信模板ID 验证码短息
     */
    public static final String BAIDU_TEMPLATE = "**********************";

    /**
     * 短信签名ID
     */
    public static final String BAIDU_SIGNATURE_ID = "**********************";

    /**
     * 百度云短信发送接口
     */
    public static final String ENDPOINT_URL = "http://smsv3.bj.baidubce.com";


}

创建百度工具类

代码语言:javascript复制
/**
 * @Description: 百度工具类
 * @BelongsProject: public-estimate-service
 * @BelongsPackage: com.clhb.publicestimateservice.utils
 * @Author: LiYu
 * @CreateTime: 2021-03-10 10:41
 * @Email: 19740428lxh@gmail.com
 * @Version: 1.0
 */
public class BaiDuUtil {

    private static final Map<String,String> HEADER = new HashMap<>();

    /**
     * 防止魔法值
     */
    private static final String SUCCESS_VALUE = "success";

    static {
        HEADER.put("Content-Type","application/json");
    }

    /**
     * 获取百度AccessToken
     * @return
     * @throws Exception
     */
    public static String getAccessToken() throws Exception {
        Map<String,Object> data = new HashMap<>(3);
        data.put("grant_type","client_credentials");
        data.put("client_id", BaiDuKeyConstant.API_KEY);
        data.put("client_secret",BaiDuKeyConstant.SECRET_KEY);
        if(RedisUtil.get(RedisKeyConstant.BAIDU_ACCESS_TOKEN)!=null){
            return RedisUtil.get(RedisKeyConstant.BAIDU_ACCESS_TOKEN);
        }
        String res = HttpUtils.doGet(BaiDuKeyConstant.ACCESS_TOKEN_URL,null,data);
        if(StringUtil.isNullOrEmpty(res)){
            System.out.println("API Key不正确或Secret Key不正确");
            throw new Exception();
        }
        JSONObject jsonObject = JSONObject.parseObject(res);
        RedisUtil.set(RedisKeyConstant.BAIDU_ACCESS_TOKEN,jsonObject.get("access_token") "",Long.parseLong((60*60*24*30) ""));
        return jsonObject.get("access_token") "";
    }

    /**
     * 获取百度VerifyToken
     * @return
     * @throws Exception
     */
    public static String getVerifyToken() throws Exception {
        JSONObject data = new JSONObject();
        data.put("plan_id",BaiDuKeyConstant.PLAN_ID);
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.doPost(BaiDuKeyConstant.VERIFY_TOKEN getAccessToken(),HEADER,data));
        return JSONObject.parseObject(jsonObject.getString("result")).getString("verify_token") "";
    }

    /**
     * 获取认证人脸图片接口
     * @param verifyToken
     * @return
     */
    public static String getTheAuthenticationFaceInterface(String verifyToken){
        JSONObject data = new JSONObject();
        data.put("verify_token",verifyToken);
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.doPost(BaiDuKeyConstant.AUTHENTICATED_FACE_URL verifyToken,HEADER,data));
        return JSONObject.parseObject(jsonObject.getString("result")).getString("image");
    }

    /**
     * 查询认证结果接口
     * @param verifyToken
     * @return
     */
    public static JSONObject queryAuthenticationResultInterface(String verifyToken){
        JSONObject data = new JSONObject();
        data.put("verify_token",verifyToken);
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.doPost(BaiDuKeyConstant.CERTIFICATION_RESULT_URL verifyToken,HEADER,data));
        if(Boolean.getBoolean(jsonObject.getString(SUCCESS_VALUE))){
            return null;
        }
        return jsonObject;
    }

    /**
     * 查询统计结果
     * @param verifyToken
     * @return
     */
    public static JSONObject queryStatisticalResults(String verifyToken){
        JSONObject data = new JSONObject();
        data.put("verifyToken",verifyToken);
        JSONObject jsonObject = JSONObject.parseObject(HttpUtils.doPost(BaiDuKeyConstant.STATISTICAL_RESULTS_URL verifyToken,HEADER,data));
        if(Boolean.getBoolean(jsonObject.getString(SUCCESS_VALUE))){
            return null;
        }
        return jsonObject;
    }

    /**
     * 发送短信验证码
     * @param mobile 手机号码
     * @return
     */
    public static boolean sendingTextVerifyCode(String mobile){
        SmsClientConfiguration config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(BaiDuKeyConstant.ACCESS_KEY_ID,BaiDuKeyConstant.SECRET_ACCESS_KEY));
        config.setEndpoint(BaiDuKeyConstant.ENDPOINT_URL);
        SmsClient smsClient = new SmsClient(config);
        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(mobile);
        request.setSignatureId(BaiDuKeyConstant.BAIDU_SIGNATURE_ID);
        request.setTemplate(BaiDuKeyConstant.BAIDU_TEMPLATE);
        Map<String, String> contentVar = new HashMap<>(2);
        String randomNumber = CommonUtil.getRandom1(4);
        contentVar.put("code", randomNumber);
        contentVar.put("minute", "5");
        request.setContentVar(contentVar);
        SendMessageV3Response response = smsClient.sendMessage(request);
        // 解析请求响应 response.isSuccess()为true 表示成功
        if (response != null && response.isSuccess()) {
            //  验证码存放到redis
            //RedisUtil.set(RedisKeyConstant.SMS_VERIFICATION_CODE_KEY mobile,randomNumber,Long.parseLong((60*60*5) ""));
            return true;
        } else {
            // fail
            return false;
        }
    }

    /**
     * 发送短信(全自定义)
     * @param mobile 电话号码
     * @param baiduTemplate 模板ID
     * @param baiduSignatureId 签名ID
     * @param templateArray
     * @param contentArray
     * @return
     */
    public static boolean sendSmsFullyCustomized(String mobile, String baiduTemplate, String baiduSignatureId, String[] templateArray, String[] contentArray){
        SmsClientConfiguration config = new SmsClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(BaiDuKeyConstant.ACCESS_KEY_ID,BaiDuKeyConstant.SECRET_ACCESS_KEY));
        config.setEndpoint(BaiDuKeyConstant.ENDPOINT_URL);
        SmsClient smsClient = new SmsClient(config);
        SendMessageV3Request request = new SendMessageV3Request();
        request.setMobile(mobile);
        request.setSignatureId(baiduSignatureId);
        request.setTemplate(baiduTemplate);
        Map<String, String> contentVar = new HashMap<>(templateArray.length);
        for (int i = 0;i< templateArray.length;i  ){
            contentVar.put(templateArray[i],contentArray[i]);
        }
        request.setContentVar(contentVar);
        SendMessageV3Response response = smsClient.sendMessage(request);
        // 解析请求响应 response.isSuccess()为true 表示成功
        if (response != null && response.isSuccess()) {
            //  submit success
            return true;
        } else {
            // fail
            return false;
        }
    }
}

获取识别结果请访问百度官方文档(https://ai.baidu.com/ai-doc/FACE/Bk8k29mmq)

0 人点赞