...
|
...
|
@@ -8,10 +8,12 @@ import com.yohoufo.dal.user.IUserAuthorizeInfoDao; |
|
|
import com.yohoufo.dal.user.model.UserAuthorizeHistory;
|
|
|
import com.yohoufo.dal.user.model.UserAuthorizeInfo;
|
|
|
import com.yohoufo.user.cache.RedisValueCache;
|
|
|
import com.yohoufo.user.common.EnumBankBackCode;
|
|
|
import com.yohoufo.user.requestVO.RealNameAuthorizeReqVO;
|
|
|
import com.yohoufo.user.responseVO.AuthorizeResultRespVO;
|
|
|
import com.yohoufo.user.service.IRealNameAuthorizeService;
|
|
|
import com.yohoufo.user.service.risk.GraphVerifyService;
|
|
|
import lombok.Data;
|
|
|
import net.sf.json.JSONObject;
|
|
|
import org.apache.commons.codec.binary.Base64;
|
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
...
|
...
|
@@ -20,12 +22,14 @@ import org.slf4j.Logger; |
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.HttpClientErrorException;
|
|
|
import org.springframework.web.client.HttpServerErrorException;
|
|
|
import org.springframework.web.client.RestClientException;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
|
|
import javax.annotation.Resource;
|
...
|
...
|
@@ -136,14 +140,12 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService { |
|
|
//根据报文体,生成HTTP报文头的认证内容 (open-body-sig 方式)
|
|
|
String authorizationContentByOpenBodySig=generateAuthorizationByOpenBodySig(msgContentParams);
|
|
|
//请求返回
|
|
|
ResponseEntity<Object> responseEntity = postRequest(msgContentParams,authorizationContentByOpenBodySig);
|
|
|
PostBankResult responseResult = postRequest(msgContentParams,authorizationContentByOpenBodySig);
|
|
|
|
|
|
//返回结果处理:请求成功入认证信息库,请求失败记录的redis,后续超过一定次数则开启验证码
|
|
|
int responseCode=responseEntity.getStatusCodeValue();
|
|
|
String responseContent=String.valueOf(responseEntity.getBody());
|
|
|
ApiResponse apiResponse=new ApiResponse(400,responseContent,null);
|
|
|
ApiResponse apiResponse=new ApiResponse(400,responseResult.getErrInfo(),null);
|
|
|
long ts=getLocalDateTime().toEpochSecond(ZoneOffset.of("+8"));
|
|
|
if(responseCode==200){
|
|
|
if(responseResult.isSucFlag()){
|
|
|
UserAuthorizeInfo userAuthorizeInfo =new UserAuthorizeInfo();
|
|
|
try{
|
|
|
userAuthorizeInfo.setUid(uid);
|
...
|
...
|
@@ -166,7 +168,7 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService { |
|
|
}
|
|
|
|
|
|
//访问银联接口记录日志
|
|
|
recordHistory(uid,cardNo,certNo,name,responseCode,responseContent,ts);
|
|
|
recordHistory(uid,cardNo,certNo,name,responseResult,ts);
|
|
|
|
|
|
return apiResponse;
|
|
|
}
|
...
|
...
|
@@ -211,51 +213,104 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService { |
|
|
* 请求银联接口,获取返回信息
|
|
|
* 捕获所有异常
|
|
|
*/
|
|
|
private ResponseEntity<Object> postRequest(JSONObject msgContentParams,String authorizationContentByOpenBodySig){
|
|
|
ResponseEntity<Object> responseEntity=new ResponseEntity<>(HttpStatus.NOT_EXTENDED);
|
|
|
private PostBankResult postRequest(JSONObject msgContentParams,String authorizationContentByOpenBodySig){
|
|
|
PostBankResult result=new PostBankResult();
|
|
|
try{
|
|
|
//组成post的请求参数
|
|
|
JSONObject dataParams=new JSONObject();
|
|
|
dataParams.put("data",msgContentParams);
|
|
|
|
|
|
//return RealNameAuthorizePostBankUtil.postByhttpClient(authorizationContentByOpenBodySig,dataParams);
|
|
|
|
|
|
//headers
|
|
|
/* HttpHeaders headers = new HttpHeaders();
|
|
|
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
headers.setContentType(type);
|
|
|
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
headers.add("Authorization",authorizationContentByOpenBodySig);*/
|
|
|
|
|
|
//header
|
|
|
MultiValueMap<String, String> headers = new LinkedMultiValueMap();
|
|
|
headers.set("Content-Type", "application/json; charset=UTF-8");
|
|
|
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
|
|
|
headers.set("Authorization",authorizationContentByOpenBodySig);
|
|
|
|
|
|
//body
|
|
|
HttpEntity<JSONObject> bodyEntity = new HttpEntity<>(dataParams, headers);
|
|
|
logger.info("RealNameAuthorizeServiceImpl authorizeRealNameWithBank request message {} ,headers {} ,body {}",msgContentParams,headers,bodyEntity);
|
|
|
//post
|
|
|
responseEntity = restTemplate.postForEntity(requestUrl, bodyEntity, Object.class);
|
|
|
ResponseEntity<JSONObject> responseEntity = restTemplate.postForEntity(requestUrl, bodyEntity, JSONObject.class);
|
|
|
logger.info("RealNameAuthorizeServiceImpl authorizeRealNameWithBank response entity {} ",responseEntity);
|
|
|
//处理结果
|
|
|
JSONObject jo=responseEntity.getBody();
|
|
|
if(jo==null){
|
|
|
result.setSucFlag(false);
|
|
|
result.setBackMsg("请求银联返回内容为空");
|
|
|
return result;
|
|
|
}
|
|
|
result.setBackMsg(jo.toString());
|
|
|
result.setStatusCode(responseEntity.getStatusCodeValue());
|
|
|
//实名认证成功的应答码:"0000"
|
|
|
if(responseEntity.getStatusCodeValue()==200&&jo!=null&&"0000".equals(jo.getString("errCode"))){
|
|
|
result.setSucFlag(true);
|
|
|
result.setErrCode("0000");
|
|
|
result.setErrInfo("请求银联实名认证成功");
|
|
|
}else{
|
|
|
result.setSucFlag(false);
|
|
|
result.setErrCode(jo==null?"":jo.getString("errCode"));
|
|
|
result.setErrInfo(jo==null?"":jo.getString("errInfo"));
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
logger.error("RealNameAuthorizeServiceImpl authorizeRealNameWithBank response entity {} ,error",responseEntity,e);
|
|
|
logger.error("RealNameAuthorizeServiceImpl authorizeRealNameWithBank response msgContentParams {} ,error",msgContentParams,e);
|
|
|
|
|
|
//记录错误码
|
|
|
result.setSucFlag(false);
|
|
|
if(e instanceof HttpClientErrorException){
|
|
|
HttpClientErrorException errorException=(HttpClientErrorException)e;
|
|
|
result.setStatusCode(errorException.getStatusCode().value());
|
|
|
result.setBackMsg(errorException.getResponseBodyAsString());
|
|
|
try{
|
|
|
JSONObject jo=JSONObject.fromObject(result.getBackMsg());
|
|
|
result.setErrCode(jo==null?"":jo.getString("errCode"));
|
|
|
result.setErrInfo(jo==null?"":jo.getString("errInfo"));
|
|
|
}catch (Exception jError){
|
|
|
logger.error("change to json error {} ",result.getBackMsg(),jError);
|
|
|
}
|
|
|
}else if(e instanceof HttpServerErrorException){
|
|
|
HttpServerErrorException errorException=(HttpServerErrorException)e;
|
|
|
result.setStatusCode(errorException.getStatusCode().value());
|
|
|
result.setBackMsg(errorException.getResponseBodyAsString());
|
|
|
try{
|
|
|
JSONObject jo=JSONObject.fromObject(errorException.getResponseBodyAsString());
|
|
|
result.setErrCode(jo==null?"":jo.getString("errCode"));
|
|
|
result.setErrInfo(jo==null?"":jo.getString("errInfo"));
|
|
|
}catch (Exception jError){
|
|
|
logger.error("change to json error {} ",result.getBackMsg(),jError);
|
|
|
}
|
|
|
}else if(e instanceof RestClientException){
|
|
|
RestClientException errorException = (RestClientException)e;
|
|
|
result.setBackMsg(errorException.getMessage());
|
|
|
}else{
|
|
|
result.setBackMsg("error happen unknown reason");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
return responseEntity;
|
|
|
@Data
|
|
|
private static class PostBankResult {
|
|
|
private boolean sucFlag;
|
|
|
private int statusCode;
|
|
|
private String errCode;
|
|
|
private String errInfo;
|
|
|
private String backMsg;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 无论成功还是失败,都把访问记录日志表
|
|
|
*/
|
|
|
private void recordHistory(int uid,String cardNo,String certNo,String name,int responseCode,String responseContent,long ts){
|
|
|
private void recordHistory(int uid,String cardNo,String certNo,String name,PostBankResult responseResult ,long ts){
|
|
|
UserAuthorizeHistory history=new UserAuthorizeHistory();
|
|
|
history.setUid(uid);
|
|
|
history.setCardNo(cardNo);
|
|
|
history.setCertNo(certNo);
|
|
|
history.setCertName(name);
|
|
|
history.setResponseCode(responseCode);
|
|
|
history.setResponseContent(responseContent);
|
|
|
history.setResponseCode(responseResult.getStatusCode());
|
|
|
history.setResponseContent(responseResult.getBackMsg());
|
|
|
history.setBackErrorCode(responseResult.getErrCode());
|
|
|
history.setBackErrorInfo(responseResult.getErrInfo());
|
|
|
history.setFirstErrorCode(EnumBankBackCode.getFirstCodeBySecondCode(responseResult.getErrCode()));
|
|
|
history.setCreateTime(ts);
|
|
|
history.setUpdateTime(ts);
|
|
|
//最后记录日志 ,异步
|
...
|
...
|
|