对接百度云实名认证接口工具类:
package com.guomo.app.utils;
import java.util.*;
/**
* 身份验证
*/
public class PersonVerify {
public static String personVerify(String neme , String id_card_number) {
// 请求url接口地址
String url = "https://aip.baidubce.com/rest/2.0/face/v3/person/********";
try {
Map<String, Object> map = new HashMap<>();
map.put("image", "sfasq35sadvsvqwr5q...");
map.put("liveness_control", "HIGH");
map.put("name", neme);//姓名
map.put("id_card_number", id_card_number);//身份证号
map.put("image_type", "BASE64");
map.put("quality_control", "LOW");
String param = GsonUtils.toJson(map);
// 注意这里仅为了简化编码每一次请求都去获取access_token,线上环境access_token有过期时间, 客户端可自行缓存,过期后重新获取。
// String accessToken = "[调用鉴权接口获取的token]";
String accessToken = AuthService.getAuth(); //这里我每次都调用了token类
String result = HttpUtil.post(url, accessToken, "application/json", param);
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
PersonVerify.personVerify();
}
}
返回参数:
{
"error_code": 0,
"error_msg": "SUCCESS",
"log_id": 123,
"timestamp": 123,
"cached": 0,
"result":
{
"score": 20.1
}
}
还需要一个获取token的工具类:
package com.guomo.app.utils;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* 获取token类
*/
public class AuthService {
/**
* 获取权限token
* @return 返回示例:
* {
* "access_token": "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567",
* "expires_in": 2592000
* }
*/
public static String getAuth() {
// 官网获取的 API Key 更新为你注册的
// 百度云应用的AK
String clientId = "百度云应用的AK";
// 官网获取的 Secret Key 更新为你注册的
//百度云应用的SK
String clientSecret = "百度云应用的SK";
return getAuth(clientId, clientSecret);
}
/**
* 获取API访问token
* 该token有一定的有效期,需要自行管理,当失效时需重新获取.
* @param ak - 百度云官网获取的 API Key
* @param sk - 百度云官网获取的 Securet Key
* @return assess_token 示例:
* "24.460da4889caad24cccdb1fea17221975.2592000.1491995545.282335-1234567"
*/
public static String getAuth(String ak, String sk) {
// 获取token地址
String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
String getAccessTokenUrl = authHost
// 1. grant_type为固定参数
+ "grant_type=client_credentials"
// 2. 官网获取的 API Key
+ "&client_id=" + ak
// 3. 官网获取的 Secret Key
+ "&client_secret=" + sk;
try {
URL realUrl = new URL(getAccessTokenUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
connection.setRequestMethod("GET");
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.err.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String result = "";
String line;
while ((line = in.readLine()) != null) {
result += line;
}
/**
* 返回结果示例
*/
System.err.println("result:" + result);
JSONObject jsonObject = new JSONObject(result);
String access_token = jsonObject.getString("access_token");
return access_token;
} catch (Exception e) {
System.err.printf("获取token失败!");
e.printStackTrace(System.err);
}
return null;
}
}
我的接口:
//完善资料——实名认证20201205
@PostMapping(value = “自己项目的接口地址”)
@ApiOperation(value = "实名认证", notes="实名认证", httpMethod = "POST")
public Result UpdateCertification(CertificationQo certificationQo, HttpServletRequest request ){
AuthService authService = new AuthService();
MessageCode code = MessageCode.error;
try {
//调用第三方身份验证
JSONObject jsonObject=JSONObject.parseObject( PersonVerify.personVerify(certificationQo.getRealName(),certificationQo.getIdNo()));
//对传过来的值做一个判断0为符合其他请看状态码
if (jsonObject.getInteger("error_code")==0){
int back =userActionService.UpdateCertification(certificationQo);
if (back==1)
code = MessageCode.success;
}else {
code = MessageCode.verify_no;
}
}catch (Exception e){
log.error("系统出现异常:{}",e);
}
return Result.success(code);
}
关于工具类项目踩的坑:
原项目缺少依赖:不够的自己在网上找
<!--添加fastjson依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.4.5</version>
</dependency>
还有所需要的工具类:
下载地址