关于芝麻信用认证和授权,请先下载官方的jdk,并将xxxx处填写有效信息。或者点击下载
package ailv;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import com.antgroup.zmxy.openplatform.api.DefaultZhimaClient;
import com.antgroup.zmxy.openplatform.api.ZhimaApiException;
import com.antgroup.zmxy.openplatform.api.request.ZhimaAuthInfoAuthorizeRequest;
import com.antgroup.zmxy.openplatform.api.request.ZhimaCustomerCertificationCertifyRequest;
import com.antgroup.zmxy.openplatform.api.request.ZhimaCustomerCertificationInitializeRequest;
import com.antgroup.zmxy.openplatform.api.request.ZhimaCustomerCertificationQueryRequest;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCustomerCertificationInitializeResponse;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCustomerCertificationQueryResponse;
public class Test {
//芝麻开放平台地址
private String gatewayUrl = "https://zmopenapi.zmxy.com.cn/openapi.do";
//商户应用 Id
private String appId = "xxxxx";
//商户 RSA 私钥
private String privateKey = "xxxxx";
//芝麻 RSA 公钥
private String zhimaPublicKey = "xxxxx";
private String biz_no = "";
//认证初始化,根据信息获取biz_no,用户可以据次对账
public String getBiz_no(String name,String idCard) {
ZhimaCustomerCertificationInitializeRequest request = new ZhimaCustomerCertificationInitializeRequest();
request.setPlatform("zmop");
//request.setTransactionId(Serial.dao.getNewCode("transactionId"));// 必要参数,可以了解为和芝麻的订单号,自定义生成,芝麻的transid小于32位
request.setTransactionId("zmxy20170511123456123");
request.setProductCode("w1010100000000002978");// 必要参数,认证的产品编号,此处不变
request.setBizCode("FACE");// 必要参数,认证方式,
request.setIdentityParam("{\"identity_type\":\"CERT_INFO\",\"cert_type\":\"IDENTITY_CARD\",\"cert_name\":\""+name+"\",\"cert_no\":\""+idCard+"\"}");// 必要参数
request.setExtBizParam("{}");// 必要参数
DefaultZhimaClient client = new DefaultZhimaClient(
gatewayUrl,
appId,
privateKey,
zhimaPublicKey);
try {
ZhimaCustomerCertificationInitializeResponse response = (ZhimaCustomerCertificationInitializeResponse) client
.execute(request);
//System.out.println(response.isSuccess());
//System.out.println(response.getErrorCode());
//System.out.println(response.getErrorMessage());
//System.out.println(response.getBizNo());
System.out.println(response.getBody());
biz_no = response.getBizNo();
} catch (ZhimaApiException e) {
e.printStackTrace();
}
return biz_no;
}
//创建加密url,复制到手机浏览器打开app
public void createdUrl(){
if(biz_no==null){
System.out.print("出错");
return;
}
ZhimaCustomerCertificationCertifyRequest request = new ZhimaCustomerCertificationCertifyRequest();
request.setPlatform("zmop");
request.setBizNo(biz_no);// 必要参数
// 设置回调地址,必填. 如果需要直接在支付宝APP里面打开回调地址使用alipay协议
// alipay://www.taobao.com 或者 alipays://www.taobao.com,分别对应http和https请求
request.setReturnUrl("http://localhost");// 必要参数
DefaultZhimaClient client = new DefaultZhimaClient(gatewayUrl, appId, privateKey,zhimaPublicKey);
try {
String url = client.generatePageRedirectInvokeUrl(request);
System.out.println("generateCertifyUrl url:" + url);
} catch (ZhimaApiException e) {
e.printStackTrace();
}
}
//认证查询
public void CertificationQuery(){
ZhimaCustomerCertificationQueryRequest req = new ZhimaCustomerCertificationQueryRequest();
req.setChannel("apppc");
req.setPlatform("zmop");
req.setBizNo(biz_no);// 必要参数
DefaultZhimaClient client = new DefaultZhimaClient(gatewayUrl, appId, privateKey, zhimaPublicKey);
try {
ZhimaCustomerCertificationQueryResponse response =(ZhimaCustomerCertificationQueryResponse)client.execute(req);
System.out.println(response.isSuccess());
System.out.println(response.getErrorCode());
System.out.println(response.getErrorMessage());
System.out.println(response.getBody());
} catch (ZhimaApiException e) {
e.printStackTrace();
}
}
//获取用户进行授权操作的url,当点击操作授权后,会跳转到指定页面,同时携带open_id
public void testZhimaAuthInfoAuthorize(String name,String idCard, String state) {
ZhimaAuthInfoAuthorizeRequest req = new ZhimaAuthInfoAuthorizeRequest();
req.setChannel("apppc");
req.setPlatform("zmop");
req.setIdentityType("2");// 必要参数
req.setIdentityParam("{\"name\":\""+name+"\",\"certType\":\"IDENTITY_CARD\",\"certNo\":\""+idCard+"\"}");// 必要参数
req.setBizParams("{\"auth_code\":\"M_APPSDK\",\"channelType\":\"app\",\"state\":\""+state+"\"}");//
DefaultZhimaClient client = new DefaultZhimaClient(gatewayUrl, appId, privateKey, zhimaPublicKey);
try {
String url = client.generatePageRedirectInvokeUrl(req);
System.out.println(url);
} catch (ZhimaApiException e) {
e.printStackTrace();
}
}
//解密验签url
public Map getResult(String url) {
//将加密前的url参数放在map中
Map paramsMap = URLParamsToMap(url);
//从回调URL中获取params参数
String params = paramsMap.get("params");
//从回调URL中获取sign参数
String sign = paramsMap.get("sign");
//需要将最后解析好的url参数放在map中,所以先定义好
String result = "";
DefaultZhimaClient client = new DefaultZhimaClient(gatewayUrl, appId, privateKey, zhimaPublicKey);
try {
result = client.decryptAndVerifySign(params, sign);
} catch (ZhimaApiException e) {
e.printStackTrace();
}
return URLParamsToMap(result);
}
//将url参数改成放到map中
public Map URLParamsToMap(String url){
Map params = new HashMap();
String parameterString = url.substring(url.indexOf("?")+1);
String[] parameterKeyValues = parameterString.split("&");
for (int i = 0; i < parameterKeyValues.length; i++) {
String[] KeyValues = parameterKeyValues[i].split("=");
//可能会出现有key没有value的情况
if(KeyValues.length == 1){
KeyValues = Arrays.copyOf(KeyValues, 2);
KeyValues[1] = null;
}
String key = KeyValues[0].trim();
String values;
try {
values = null == KeyValues[1] ? null : URLDecoder.decode(KeyValues[1].trim(),"UTF-8");
params.put(key, values);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return params;
}
public static void main(String[] args) {
Test result = new Test();
//result.getBiz_no("XXX","xxxxx");
//result.createdUrl();
//result.testZhimaAuthInfoAuthorize("XXX","xxxxx");
//result.getResult(此处填写回传的url即上面方法的返回值授权后的url);
}
}