android客戶端直接呼叫芝麻信用的人臉認證
阿新 • • 發佈:2019-02-01
按理說,這些都應該是服務端完成的事情。可是由於種種原因,我放了一句狠話。。。
然後只能自己搞了。。。。
1、由於芝麻信用提供的SDK,在android端直接使用SSL證書驗證是不通過的。所以當時有點後悔說的狠話。
2、SDK用不了,沒辦法只能自己動手,自己拼接引數,寫請求。
3、不廢話,直接開始。
(1)、解壓下載的SDK原始碼。
找到 WebUtils.java (在解壓後的“com\antgroup\zmxy\openplatform\api\internal\util”目錄下)。開啟 WebUtils.java 檢視原始碼。
找到“getConnection()”方法,你會看到很蛋疼的一句話“預設認證不通過,進行證書校驗。”
我們修改 return true; //預設認證不通過,進行證書校驗。(真操蛋,直接改成true,看你還怎麼不給我通過)。之後儲存,把webutils.java 複製到自己的工程中,呼叫芝麻信用時使用。
(2)、自己拼引數,開始認證。就直接上程式碼了。
public class FaceUtils {
private String path = "";//回撥地址
private static String biz_no = "";
private Context context;
private static FaceUtils faceUtils;
private String IP = "https://zmopenapi.zmxy.com.cn/openapi.do";
private String app_id = "自己的APP_ID";
private String private_key = "自己的私鑰";
private String public_key = "支付寶的公鑰(不是應用公鑰,別搞錯了)";
private AdapterViewClickListener clickListener;
public static FaceUtils getInstance() {
if (faceUtils == null) {
faceUtils = new FaceUtils();
}
return faceUtils;
}
/**
* 啟動芝麻認證
*
* @param name
* @param id_num
*/
public void startFace(Context context, final String name,
final String id_num,String path) {
this.path = path;
this.context = context;
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String params = "";
String sign = "";
JSONObject identity_param = new JSONObject();
try {
identity_param.put("identity_type", "CERT_INFO");
identity_param.put("cert_type", "IDENTITY_CARD");
identity_param.put("cert_name", name);
identity_param.put("cert_no", id_num);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
long time = System.currentTimeMillis();
String signCanshu = "transaction_id=ZGYD"
+ new SimpleDateFormat("yyyyMMddHHmmss").format(time)
+ "0001234" + "&product_code=w1010100000000002978"
+ "&biz_code=FACE" + "&identity_param="
+ identity_param.toString() + "&ext_biz_param={}";
try {
params = RSACoderUtil.encrypt(signCanshu, "UTF-8",
public_key);
sign = RSACoderUtil.sign(signCanshu, "UTF-8", private_key);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, String> map = new HashMap<>();
map.put("app_id", app_id);
map.put("charset", "UTF-8");
map.put("method", "zhima.customer.certification.initialize");
map.put("version", "1.0");
map.put("platform", "zmop");
map.put("params", params);
map.put("sign", sign);
String result = "";
try {
result = com.huahan.finance.utils.WebUtils.doPost(IP, map,
15000, 15000);
HHLog.i("mtj", "result = " + result);
try {
JSONObject object = new JSONObject(result);
String jieresult = RSACoderUtil.decrypt(
object.getString("biz_response"), private_key,
"UTF-8");
HHLog.i("mtj", "解密== " + jieresult);
JSONObject bizObject = new JSONObject(jieresult);
biz_no = bizObject.getString("biz_no");
handler.sendEmptyMessage(0);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
HHLog.i("mtj", "result 解密e= " + e.getMessage());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
HHLog.i("mtj", "result e= " + e.getMessage());
}
}
}).start();
}
/**
* 獲取認證url
*
* @param biz
*/
public void getUrl() {
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(path);// 必要引數
DefaultZhimaClient client = new DefaultZhimaClient(
"https://zmopenapi.zmxy.com.cn/openapi.do", app_id,
private_key, public_key);
try {
String url = client.generatePageRedirectInvokeUrl(request);
HHLog.i("mtj", "開始認證url==" + url);
doVerify(url);
} catch (ZhimaApiException e) {
e.printStackTrace();
HHLog.i("mtj", "開始認證錯誤==" + e.getMessage());
}
}
public String Result(String params) {
String result = "";
try {
result = RSACoderUtil.decrypt(params, private_key, "UTF-8");
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
/**
* 查詢認證結果
*
* @param biz
*/
public void QueryNum(AdapterViewClickListener clickListener) {
this.clickListener = clickListener;
if (!TextUtils.isEmpty(biz_no)) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String params = "";
String sign = "";
String signCanshu = "biz_no=" + biz_no;
try {
params = RSACoderUtil.encrypt(signCanshu, "UTF-8",
public_key);
sign = RSACoderUtil.sign(signCanshu, "UTF-8",
private_key);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, String> map = new HashMap<>();
map.put("app_id", app_id);
map.put("charset", "UTF-8");
map.put("method", "zhima.customer.certification.query");
map.put("version", "1.0");
map.put("platform", "zmop");
map.put("params", params);
map.put("sign", sign);
String result = "";
String passed = "";
try {
result = WebUtils.doPost(IP, map, 15000, 15000);
JSONObject jsonObject = new JSONObject(result);
JSONObject jObject = new JSONObject(
RSACoderUtil.decrypt(
jsonObject.getString("biz_response"),
private_key, "UTF-8"));
HHLog.i("mtj", "認證結果==" + jObject.toString());
passed = jObject.getString("passed");
} catch (Exception e) {
e.printStackTrace();
HHLog.i("mtj", "認證結果=e=" + e.getMessage());
}
Message message = new Message();
message.what = 1;
message.obj = passed;
handler.sendMessage(message);
}
}).start();
}
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
switch (msg.what) {
case 0:// 認證
getUrl();
break;
case 1:// 查詢
String passed = (String) msg.obj;
if ("true".equals(passed)) {
clickListener.adapterViewClick(1, null);
} else {// 未通過
clickListener.adapterViewClick(0, null);
}
biz_no = "";
break;
default:
break;
}
}
};
/**
* 啟動支付寶進行認證
*
* @param url
* 開放平臺返回的URL
* @param url
*/
@SuppressWarnings("deprecation")
private void doVerify(String url) {
if (hasApplication()) {
Intent action = new Intent(Intent.ACTION_VIEW);
StringBuilder builder = new StringBuilder();
// 這裡使用固定appid 20000067
builder.append("alipays://platformapi/startapp?appId=20000067&url=");
builder.append(URLEncoder.encode(url));
action.setData(Uri.parse(builder.toString()));
context.startActivity(action);
} else {
// 處理沒有安裝支付寶的情況
DialogUtils.showOptionDialog(context,
context.getString(R.string.load_zfb),
new OnOptionDialogClickListener() {
@Override
public void onClick(Dialog paramDialog, View paramView) {
// TODO Auto-generated method stub
Intent action = new Intent(Intent.ACTION_VIEW);
action.setData(Uri.parse("https://m.alipay.com"));
action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(action);
paramDialog.dismiss();
}
}, new OnOptionDialogClickListener() {
@Override
public void onClick(Dialog paramDialog, View paramView) {
// TODO Auto-generated method stub
paramDialog.dismiss();
}
}, true);
}
}
/**
* 判斷是否安裝了支付寶
*
* @return true 為已經安裝
* @return
*/
private boolean hasApplication() {
PackageManager manager = context.getPackageManager();
Intent action = new Intent(Intent.ACTION_VIEW);
action.setData(Uri.parse("alipays://"));
List list = manager.queryIntentActivities(action,
PackageManager.GET_RESOLVED_FILTER);
return list != null && list.size() > 0;
}
}