Authored by qinchao

实名认证接口-开启图形验证码

package com.yohoufo.user.common;
import com.yohoufo.user.controller.passport.RealNameAuthorizeController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
@Configuration
public class CustomRestTemplateConfig {
private Logger logger = LoggerFactory.getLogger(CustomRestTemplateConfig.class);
/**
* 定义一个500毫秒超时的RestTemplate,调用银联接口用
... ... @@ -14,12 +22,41 @@ public class CustomRestTemplateConfig {
*/
@Bean
public RestTemplate authorizeBankRestTemplate(){
ResponseErrorHandler responseErrorHandler = new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse clientHttpResponse) throws IOException {
int statusCode = getHttpStatusResponseCode(clientHttpResponse);
if(statusCode==200){
return false;
}
return true;
}
@Override
public void handleError(ClientHttpResponse clientHttpResponse) throws IOException {
int responseCode = getHttpStatusResponseCode(clientHttpResponse);
logger.error("CustomRestTemplateConfig handleError responseCode {},response {}",responseCode,clientHttpResponse);
}
protected int getHttpStatusResponseCode(ClientHttpResponse response) throws IOException {
try {
return response.getStatusCode().value();
}
catch (IllegalArgumentException ex) {
return 400;
}
}
};
HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
httpRequestFactory.setConnectionRequestTimeout(500);
httpRequestFactory.setConnectTimeout(500);
httpRequestFactory.setReadTimeout(500);
RestTemplate restTemplate= new RestTemplate(httpRequestFactory);
restTemplate.setErrorHandler(responseErrorHandler);
return restTemplate;
}
}
... ...
... ... @@ -3,8 +3,6 @@ package com.yohoufo.user.service.impl;
import com.yoho.core.config.ConfigReader;
import com.yoho.core.redis.cluster.operations.serializer.RedisKeyBuilder;
import com.yoho.tools.common.beans.ApiResponse;
import com.yoho.uic.client.SDK.yoho.GraphicVerifiedSDK;
import com.yohoufo.common.interceptor.RemoteIPInterceptor;
import com.yohoufo.dal.user.IUserAuthorizeHistoryDao;
import com.yohoufo.dal.user.IUserAuthorizeInfoDao;
import com.yohoufo.dal.user.model.UserAuthorizeHistory;
... ... @@ -22,16 +20,17 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
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.RestTemplate;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
... ... @@ -113,11 +112,11 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
//根据报文体,生成HTTP报文头的认证内容 (open-body-sig 方式)
String authorizationContentByOpenBodySig=generateAuthorizationByOpenBodySig(msgContentParams);
//请求返回
ResponseEntity<String> responseEntity = postRequest(msgContentParams,authorizationContentByOpenBodySig);
ResponseEntity<Object> responseEntity = postRequest(msgContentParams,authorizationContentByOpenBodySig);
//返回结果处理:请求成功入认证信息库,请求失败记录的redis,后续超过一定次数则开启验证码
int responseCode=responseEntity.getStatusCodeValue();
String responseContent=responseEntity.getBody();
String responseContent=String.valueOf(responseEntity.getBody());
ApiResponse apiResponse=new ApiResponse(400,responseContent,null);
if(responseCode==200){
UserAuthorizeInfo userAuthorizeInfo =new UserAuthorizeInfo();
... ... @@ -183,27 +182,39 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
/**
* 请求银联接口,获取返回信息
* 捕获所有异常
*/
private ResponseEntity<String> postRequest(JSONObject msgContentParams,String authorizationContentByOpenBodySig){
//组成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);
//body
HttpEntity<String> bodyEntity = new HttpEntity<String>(dataParams.toString(), headers);
logger.info("RealNameAuthorizeServiceImpl authorizeRealNameWithBank request message {} ,headers {} ,body {}",msgContentParams,headers,bodyEntity);
//post
ResponseEntity<String> responseEntity = restTemplate.postForEntity(requestUrl, bodyEntity, String.class);
logger.info("RealNameAuthorizeServiceImpl authorizeRealNameWithBank response entity {} ",responseEntity);
private ResponseEntity<Object> postRequest(JSONObject msgContentParams,String authorizationContentByOpenBodySig){
ResponseEntity<Object> responseEntity=new ResponseEntity<>(HttpStatus.NOT_EXTENDED);
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);*/
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);
logger.info("RealNameAuthorizeServiceImpl authorizeRealNameWithBank response entity {} ",responseEntity);
}catch (Exception e){
logger.error("RealNameAuthorizeServiceImpl authorizeRealNameWithBank response entity {} ,error",responseEntity,e);
}
return responseEntity;
}
... ...