身份证识别
身份证识别host、path、appKey、appSecret
发送请求
/**
* 发送请求
*
* @param request request对象
* @return Response
* @throws Exception
*/
public static Response execute(Request request) throws Exception {
switch (request.getMethod()) {
case GET:
return HttpUtil.httpGet(request.getHost(), request.getPath(),
request.getTimeout(),
request.getHeaders(),
request.getQuerys(),
request.getSignHeaderPrefixList(),
request.getAppKey(), request.getAppSecret());
case POST_FORM:
return HttpUtil.httpPost(request.getHost(), request.getPath(),
request.getTimeout(),
request.getHeaders(),
request.getQuerys(),
request.getBodys(),
request.getSignHeaderPrefixList(),
request.getAppKey(), request.getAppSecret());
case POST_STRING:
return HttpUtil.httpPost(request.getHost(), request.getPath(),
request.getTimeout(),
request.getHeaders(),
request.getQuerys(),
request.getStringBody(),
request.getSignHeaderPrefixList(),
request.getAppKey(), request.getAppSecret());
case POST_BYTES:
return HttpUtil.httpPost(request.getHost(), request.getPath(),
request.getTimeout(),
request.getHeaders(),
request.getQuerys(),
request.getBytesBody(),
request.getSignHeaderPrefixList(),
request.getAppKey(), request.getAppSecret());
case PUT_STRING:
return HttpUtil.httpPut(request.getHost(), request.getPath(),
request.getTimeout(),
request.getHeaders(),
request.getQuerys(),
request.getStringBody(),
request.getSignHeaderPrefixList(),
request.getAppKey(), request.getAppSecret());
case PUT_BYTES:
return HttpUtil.httpPut(request.getHost(), request.getPath(),
request.getTimeout(),
request.getHeaders(),
request.getQuerys(),
request.getBytesBody(),
request.getSignHeaderPrefixList(),
request.getAppKey(), request.getAppSecret());
case DELETE:
return HttpUtil.httpDelete(request.getHost(), request.getPath(),
request.getTimeout(),
request.getHeaders(),
request.getQuerys(),
request.getSignHeaderPrefixList(),
request.getAppKey(), request.getAppSecret());
default:
throw new IllegalArgumentException(String.format("unsupported method:%s", request.getMethod()));
}
}
}
Request
public class Request {
public Request() {
}
public Request(Method method, String host, String path, String appKey, String appSecret, int timeout) {
this.method = method;
this.host = host;
this.path = path;
this.appKey = appKey;
this.appSecret = appSecret;
this.timeout = timeout;
}
/**
* (必选)请求方法
*/
private Method method;
/**
* (必选)Host
*/
private String host;
/**
* (必选)Path
*/
private String path;
/**
* (必选)APP KEY
*/
private String appKey;
/**
* (必选)APP密钥
*/
private String appSecret;
/**
* (必选)超时时间,单位毫秒,设置零默认使用com.aliyun.apigateway.demo.constant.Constants.DEFAULT_TIMEOUT
*/
private int timeout;
/**
* (可选) HTTP头
*/
private Map<String, String> headers;
/**
* (可选) Querys
*/
private Map<String, String> querys;
/**
* (可选)表单参数
*/
private Map<String, String> bodys;
/**
* (可选)字符串Body体
*/
private String stringBody;
/**
* (可选)字节数组类型Body体
*/
private byte[] bytesBody;
/**
* (可选)自定义参与签名Header前缀
*/
private List<String> signHeaderPrefixList;
public Method getMethod() {
return method;
}
public void setMethod(Method method) {
this.method = method;
}
public String getHost() {
return host;
}
public void setPath(String path) {
this.path = path;
}
public String getPath() {
return path;
}
public void setHost(String host) {
this.host = host;
}
public String getAppKey() {
return appKey;
}
public void setAppKey(String appKey) {
this.appKey = appKey;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public Map<String, String> getHeaders() {
return headers;
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
public Map<String, String> getQuerys() {
return querys;
}
public void setQuerys(Map<String, String> querys) {
this.querys = querys;
}
public Map<String, String> getBodys() {
return bodys;
}
public void setBodys(Map<String, String> bodys) {
this.bodys = bodys;
}
public String getStringBody() {
return stringBody;
}
public void setStringBody(String stringBody) {
this.stringBody = stringBody;
}
public byte[] getBytesBody() {
return bytesBody;
}
public void setBytesBody(byte[] bytesBody) {
this.bytesBody = bytesBody;
}
public List<String> getSignHeaderPrefixList() {
return signHeaderPrefixList;
}
public void setSignHeaderPrefixList(List<String> signHeaderPrefixList) {
this.signHeaderPrefixList = signHeaderPrefixList;
}
}
Response
public class Response {
private int statusCode;
private String contentType;
private String requestId;
private String errorMessage;
private Map<String, String> headers;
private String body;
public Response() {
}
public int getStatusCode() {
return statusCode;
}
public void setStatusCode(int statusCode) {
this.statusCode = statusCode;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getRequestId() {
return requestId;
}
public void setRequestId(String requestId) {
this.requestId = requestId;
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
public Map<String, String> getHeaders() {
return headers;
}
public String getHeader(String key) {
if (null != headers) {
return headers.get(key);
} else {
return null;
}
}
public void setHeaders(Map<String, String> headers) {
this.headers = headers;
}
public void setHeader(String key, String value) {
if (null == this.headers) {
this.headers = new HashMap<String, String>(2);
}
this.headers.put(key, value);
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
通用常量
public class Constants {
/**
* 签名算法HmacSha256
*/
public static final String HMAC_SHA256 = "HmacSHA256";
/**
* 编码UTF-8
*/
public static final String ENCODING = "UTF-8";
/**
* UserAgent
*/
public static final String USER_AGENT = "demo/aliyun/java";
/**
* 换行符
*/
public static final String LF = "\n";
/**
* 串联符
*/
public static final String SPE1 = ",";
/**
* 示意符
*/
public static final String SPE2 = ":";
/**
* 连接符
*/
public static final String SPE3 = "&";
/**
* 赋值符
*/
public static final String SPE4 = "=";
/**
* 问号符
*/
public static final String SPE5 = "?";
/**
* 默认请求超时时间,单位毫秒
*/
public static final int DEFAULT_TIMEOUT = 1000;
/**
* 参与签名的系统Header前缀,只有指定前缀的Header才会参与到签名中
*/
public static final String CA_HEADER_TO_SIGN_PREFIX_SYSTEM = "X-Ca-";
}
常用HTTP Content-Type常量
public class ContentType {
/**
* 表单类型Content-Type
*/
public static final String CONTENT_TYPE_FORM = "application/x-www-form-urlencoded; charset=UTF-8";
/**
* 流类型Content-Type
*/
public static final String CONTENT_TYPE_STREAM = "application/octet-stream; charset=UTF-8";
/**
* JSON类型Content-Type
*/
public static final String CONTENT_TYPE_JSON = "application/json; charset=UTF-8";
/**
* XML类型Content-Type
*/
public static final String CONTENT_TYPE_XML = "application/xml; charset=UTF-8";
/**
* 文本类型Content-Type
*/
public static final String CONTENT_TYPE_TEXT = "application/text; charset=UTF-8";
}
HTTP头常量
public class HttpHeader {
/**
* 请求Header Accept
*/
public static final String HTTP_HEADER_ACCEPT = "Accept";
/**
* 请求Body内容MD5 Header
*/
public static final String HTTP_HEADER_CONTENT_MD5 = "Content-MD5";
/**
* 请求Header Content-Type
*/
public static final String HTTP_HEADER_CONTENT_TYPE = "Content-Type";
/**
* 请求Header UserAgent
*/
public static final String HTTP_HEADER_USER_AGENT = "User-Agent";
/**
* 请求Header Date
*/
public static final String HTTP_HEADER_DATE = "Date";
}
HTTP方法常量
public class HttpMethod {
/**
* GET
*/
public static final String GET = "GET";
/**
* POST
*/
public static final String POST = "POST";
/**
* PUT
*/
public static final String PUT = "PUT";
/**
* DELETE
*/
public static final String DELETE = "DELETE";
}
Http请求方法
public enum Method {
/**
* Method
*/
GET, POST_FORM, POST_STRING, POST_BYTES, PUT_FORM, PUT_STRING, PUT_BYTES, DELETE;
}
身份证识别返回参数
public class CardNoResponse {
@ApiModelProperty(value = "ID")
private String requestId;
@ApiModelProperty(value = "姓名")
private String name;
@ApiModelProperty(value = "地址")
private String address;
@ApiModelProperty(value = "身份证号码")
private String num;
@ApiModelProperty(value = "性别")
private String sex;
@ApiModelProperty(value = "民族")
private String nationality;
@ApiModelProperty(value = "配置信息")
private String configStr;
@ApiModelProperty(value = "开始时间")
private String startDate;
@ApiModelProperty(value = "结束时间")
private String endDate;
@ApiModelProperty(value = "签发机关")
private String issue;
@ApiModelProperty(value = "识别成功与否 true-成功,false-失败")
private String success;
}
身份证识别工具类
@Component
@EnableConfigurationProperties(AliyunCardNoProperties.class)
public class AliyunCardNoUtils {
/**
* 自定义参与签名Header前缀(可选,默认只有"X-Ca-"开头的参与到Header签名)
*/
private final static List<String> CUSTOM_HEADERS_TO_SIGN_PREFIX = new ArrayList<String>();
@Autowired
private MinioClient minioClient;
@Resource
private MinioProperties minioProperties;
@Resource
private AliyunCardNoProperties aliyunCardNoProperties;
@Autowired
private TStaffInfoMapper staffInfoMapper;
public CardNoResponse getCardNoInfo(String imgFile, String typeId) {
//对URL进行解码
String file = URLUtils.getURLDecoderString(imgFile);
//请根据线上文档修改configure字段
JSONObject configObj = new JSONObject();
if ("2".equals(typeId)) {
configObj.put("side", "face");
} else {
configObj.put("side", "back");
}
String configStr = configObj.toString();
// 对图像进行base64编码
String bucket = minioProperties.getBucket();
// 从链接中得到文件名
String replace = file.replace(bucket + "/", "#");
String fileName = replace.split("#")[1];
InputStream inputStream;
byte[] bytes = new byte[0];
try {
inputStream = minioClient.getObject(bucket, fileName);
bytes = toByteArray(inputStream);
} catch (Exception e) {
e.printStackTrace();
}
byte[] content = bytes;
String base64 = new String(Base64.encodeBase64(content));
// 拼装请求body的json字符串
JSONObject requestObj = new JSONObject();
try {
requestObj.put("image", base64);
if (configStr.length() > 0) {
requestObj.put("configure", configStr);
}
} catch (JSONException e) {
e.printStackTrace();
}
//Body内容
String body = requestObj.toString();
Map<String, String> headers = new HashMap<String, String>(4);
//(必填)根据期望的Response内容类型设置
headers.put(HttpHeader.HTTP_HEADER_ACCEPT, "application/json");
//(可选)Body MD5,服务端会校验Body内容是否被篡改,建议Body非Form表单时添加此Header
headers.put(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.base64AndMD5(body));
//(POST/PUT请求必选)请求Body内容格式
headers.put(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_TEXT);
Request request = new Request(Method.POST_STRING,
aliyunCardNoProperties.getHost(),
aliyunCardNoProperties.getPath(),
aliyunCardNoProperties.getAppKey(),
aliyunCardNoProperties.getAppSecret(),
Constants.DEFAULT_TIMEOUT);
request.setHeaders(headers);
request.setSignHeaderPrefixList(CUSTOM_HEADERS_TO_SIGN_PREFIX);
request.setStringBody(body);
//调用服务端
Response response = null;
try {
response = Client.execute(request);
} catch (Exception e) {
e.printStackTrace();
}
if (response.getStatusCode() != HttpStatus.SC_OK) {
return new CardNoResponse().setSuccess("false");
}
String res = response.getBody();
CardNoResponse cardNoResponse = JSONObject.parseObject(res, CardNoResponse.class);
return cardNoResponse;
}
public static byte[] toByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
}
}