Commit e0c75d35 authored by hanfeng's avatar hanfeng

Merge branch 'master-Member-bug-modify'

parents 964dcfe3 bfda8484
...@@ -83,6 +83,12 @@ ...@@ -83,6 +83,12 @@
<artifactId>jiguang-common</artifactId> <artifactId>jiguang-common</artifactId>
<version>1.1.1</version> <version>1.1.1</version>
</dependency> </dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
......
package com.xxfc.platform.universal.api;
import com.xxfc.platform.universal.api.pojo.Authentication;
import com.xxfc.platform.universal.entity.IdInformation;
/**
* 调用外部接口实现类。调用不同的外部接口,需要编写不同了类实现该类
*/
public interface BaseAuthentication {
Authentication getAuthentication(IdInformation idInformation);
}
package com.xxfc.platform.universal.api.impl;
import com.xxfc.platform.universal.api.BaseAuthentication;
import com.xxfc.platform.universal.api.pojo.Authentication;
import com.xxfc.platform.universal.entity.IdInformation;
public class FQAuthentication implements BaseAuthentication {
@Override
public Authentication getAuthentication(IdInformation idInformation) {
return null;
}
}
package com.xxfc.platform.universal.api.pojo;
public class Authentication {
}
package com.xxfc.platform.universal.utils;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class HttpUtils {
/**
* get
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @return
* @throws Exception
*/
public static HttpResponse doGet(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpGet request = new HttpGet(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
return httpClient.execute(request);
}
/**
* post form
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param bodys
* @return
* @throws Exception
*/
public static HttpResponse doPost(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
Map<String, String> bodys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (bodys != null) {
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
for (String key : bodys.keySet()) {
nameValuePairList.add(new BasicNameValuePair(key, bodys.get(key)));
}
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairList, "utf-8");
formEntity.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
request.setEntity(formEntity);
}
return httpClient.execute(request);
}
/**
* Post String
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPost(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (StringUtils.isNotBlank(body)) {
request.setEntity(new StringEntity(body, "utf-8"));
}
return httpClient.execute(request);
}
/**
* Post stream
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPost(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPost request = new HttpPost(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (body != null) {
request.setEntity(new ByteArrayEntity(body));
}
return httpClient.execute(request);
}
/**
* Put String
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
String body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (StringUtils.isNotBlank(body)) {
request.setEntity(new StringEntity(body, "utf-8"));
}
return httpClient.execute(request);
}
/**
* Put stream
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @param body
* @return
* @throws Exception
*/
public static HttpResponse doPut(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys,
byte[] body)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpPut request = new HttpPut(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
if (body != null) {
request.setEntity(new ByteArrayEntity(body));
}
return httpClient.execute(request);
}
/**
* Delete
*
* @param host
* @param path
* @param method
* @param headers
* @param querys
* @return
* @throws Exception
*/
public static HttpResponse doDelete(String host, String path, String method,
Map<String, String> headers,
Map<String, String> querys)
throws Exception {
HttpClient httpClient = wrapClient(host);
HttpDelete request = new HttpDelete(buildUrl(host, path, querys));
for (Map.Entry<String, String> e : headers.entrySet()) {
request.addHeader(e.getKey(), e.getValue());
}
return httpClient.execute(request);
}
private static String buildUrl(String host, String path, Map<String, String> querys) throws UnsupportedEncodingException {
StringBuilder sbUrl = new StringBuilder();
sbUrl.append(host);
if (!StringUtils.isBlank(path)) {
sbUrl.append(path);
}
if (null != querys) {
StringBuilder sbQuery = new StringBuilder();
for (Map.Entry<String, String> query : querys.entrySet()) {
if (0 < sbQuery.length()) {
sbQuery.append("&");
}
if (StringUtils.isBlank(query.getKey()) && !StringUtils.isBlank(query.getValue())) {
sbQuery.append(query.getValue());
}
if (!StringUtils.isBlank(query.getKey())) {
sbQuery.append(query.getKey());
if (!StringUtils.isBlank(query.getValue())) {
sbQuery.append("=");
sbQuery.append(URLEncoder.encode(query.getValue(), "utf-8"));
}
}
}
if (0 < sbQuery.length()) {
sbUrl.append("?").append(sbQuery);
}
}
return sbUrl.toString();
}
private static HttpClient wrapClient(String host) {
HttpClient httpClient = new DefaultHttpClient();
if (host.startsWith("https://")) {
sslClient(httpClient);
}
return httpClient;
}
private static void sslClient(HttpClient httpClient) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] xcs, String str) {
}
public void checkServerTrusted(X509Certificate[] xcs, String str) {
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = httpClient.getConnectionManager();
SchemeRegistry registry = ccm.getSchemeRegistry();
registry.register(new Scheme("https", 443, ssf));
} catch (KeyManagementException ex) {
throw new RuntimeException(ex);
} catch (NoSuchAlgorithmException ex) {
throw new RuntimeException(ex);
}
}
}
\ No newline at end of file
package com.xxfc.platform.universal.biz;
import lombok.Data;
/**
* 用户信息类
* @author Administrator
*/
@Data
public class UserMessage {
private String name;
private String idNumber;
}
...@@ -7,8 +7,10 @@ import com.github.wxiaoqi.security.admin.feign.UserFeign; ...@@ -7,8 +7,10 @@ import com.github.wxiaoqi.security.admin.feign.UserFeign;
import com.github.wxiaoqi.security.common.exception.BaseException; import com.github.wxiaoqi.security.common.exception.BaseException;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse; import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode; import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.xxfc.platform.universal.biz.UserMessage;
import com.xxfc.platform.universal.entity.IdInformation; import com.xxfc.platform.universal.entity.IdInformation;
import com.xxfc.platform.universal.mapper.IdInformationMapper; import com.xxfc.platform.universal.mapper.IdInformationMapper;
import com.xxfc.platform.universal.service.authenticationInterface.UserAuthentication;
import com.xxfc.platform.universal.utils.CertifHttpUtils; import com.xxfc.platform.universal.utils.CertifHttpUtils;
import com.xxfc.platform.universal.utils.Validation; import com.xxfc.platform.universal.utils.Validation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
...@@ -21,6 +23,7 @@ import org.apache.http.util.EntityUtils; ...@@ -21,6 +23,7 @@ import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.transaction.interceptor.TransactionAspectSupport;
...@@ -31,9 +34,16 @@ import java.text.SimpleDateFormat; ...@@ -31,9 +34,16 @@ import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
/**
* 认证业务
* @author Administrator
*/
@Service @Service
@Slf4j @Slf4j
public class CertificationService { public class CertificationService {
@Autowired
private UserAuthentication authentication;
/** /**
* 认证相关的数据 * 认证相关的数据
*/ */
...@@ -209,17 +219,20 @@ public class CertificationService { ...@@ -209,17 +219,20 @@ public class CertificationService {
} }
//map携带身份证和姓名进行认证 //map携带身份证和姓名进行认证
Map<String, String> authMap = new HashMap<>(); // Map<String, String> authMap = new HashMap<>();
authMap.put(idCardName, (String) frontData.get(numberName)); // authMap.put(idCardName, (String) frontData.get(numberName));
authMap.put(cName, (String) frontData.get(cName)); // authMap.put(cName, (String) frontData.get(cName));
//3.调用接口进行认证 // //3.调用接口进行认证
String result = certificate(authMap); //
// boolean result = certificate(authMap);
boolean result = authentication.certificate(new UserMessage(){{
setIdNumber(number);
setName(name);
}} );
log.info("----认证结果result=========" + result); log.info("----认证结果result=========" + result);
//认证返回的参数是否为空 //认证返回的参数是否为空
if (!StringUtils.isBlank(result)) {
Map<String, Object> map = (Map<String, Object>) JSONObject.parse(result); if (result) {
log.info("----certifRet=========" + certifRet);
if (MapUtil.isNotEmpty(map) || certifResultCode.equals(map.get(certifRet))) {
//认证成功后存入保存到数据库 //认证成功后存入保存到数据库
//获得身份证正面记录的身份证号和真实姓名 //获得身份证正面记录的身份证号和真实姓名
//设置姓名 //设置姓名
...@@ -257,9 +270,6 @@ public class CertificationService { ...@@ -257,9 +270,6 @@ public class CertificationService {
// return ObjectRestResponse.succ(objRR.getData()); // return ObjectRestResponse.succ(objRR.getData());
// } // }
} }
}
return ObjectRestResponse.createFailedResult(ResultCode.INCOMPLETE_DATA,"网络异常,请稍后再试"); return ObjectRestResponse.createFailedResult(ResultCode.INCOMPLETE_DATA,"网络异常,请稍后再试");
...@@ -267,7 +277,8 @@ public class CertificationService { ...@@ -267,7 +277,8 @@ public class CertificationService {
//认证 //认证
public String certificate(Map<String, String> querys) { //认证
public boolean certificate(Map<String, String> querys) {
Map<String, String> headers = new HashMap<String, String>(); Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + cAppcode); headers.put("Authorization", "APPCODE " + cAppcode);
try { try {
...@@ -280,15 +291,27 @@ public class CertificationService { ...@@ -280,15 +291,27 @@ public class CertificationService {
*/ */
//获取response的body //获取response的body
if (statusCode == 200) { if (statusCode == 200) {
return EntityUtils.toString(response.getEntity()); String result = EntityUtils.toString(response.getEntity());
log.info("----认证结果result=========" + result);
//认证返回的参数是否为空
if (!StringUtils.isBlank(result)) {
Map<String, Object> map = (Map<String, Object>) JSONObject.parse(result);
log.info("----certifRet=========" + certifRet);
if (MapUtil.isNotEmpty(map) || certifResultCode.equals(map.get(certifRet))) {
return true;
}
}
} }
return false;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
return false;
} }
return null;
} }
//身份证照片解析 //身份证照片解析
public String imageParse(String imageUrl, String type) { public String imageParse(String imageUrl, String type) {
Map<String, String> headers = new HashMap<String, String>(); Map<String, String> headers = new HashMap<String, String>();
...@@ -317,10 +340,7 @@ public class CertificationService { ...@@ -317,10 +340,7 @@ public class CertificationService {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return null;
} }
......
package com.xxfc.platform.universal.service.authenticationInterface;
import com.xxfc.platform.universal.biz.UserMessage;
import java.util.Map;
/**
* 用户认证类
* @author Administrator
*/
public interface UserAuthentication {
/**
* 用户认证方法
* @param message
* @return
*/
boolean certificate(UserMessage message) ;
}
package com.xxfc.platform.universal.service.authenticationInterface.impl;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.xxfc.platform.universal.biz.UserMessage;
import com.xxfc.platform.universal.service.authenticationInterface.UserAuthentication;
import com.xxfc.platform.universal.utils.CertifHttpUtils;
import com.xxfc.platform.universal.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 调用北京畅游互联科技有限公司接口
*
* @author Administrator
*/
@Service
@Slf4j
@Primary
public class BJCYAuthentication implements UserAuthentication {
private final String host = "http://aliyunverifyidcard.haoservice.com";
private final String path = "/idcard/VerifyIdcardv2";
private final String method = "GET";
private final String appcode = "ee7710ce92054cae9f6c040f6864e6a7";
private final String tokenHead = "Authorization";
private final String token="APPCODE " + appcode;
private final String cardNo ="cardNo";
private final String realName ="realName";
private final Integer resultCode=0;
private final String ret="error_code";
@Override
public boolean certificate(UserMessage message) {
Map<String, String> headers = new HashMap<String, String>();
headers.put(tokenHead, token);
Map<String, String> querys = new HashMap<String, String>();
querys.put(cardNo, message.getIdNumber());
querys.put(realName, message.getName());
try {
HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
StatusLine statusLine = response.getStatusLine();
log.error(response.toString());
int statusCode = statusLine.getStatusCode();
log.error(statusCode+"");
//获取response的body
if (statusCode == 200) {
String result = EntityUtils.toString(response.getEntity());
log.info("----认证结果result=========" + result);
//认证返回的参数是否为空
if (!StringUtils.isBlank(result)) {
Map<String, Object> map = (Map<String, Object>) JSONObject.parse(result);
log.info("----certifRet=========" + map);
if (MapUtil.isNotEmpty(map) || resultCode.equals(map.get(ret))) {
return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
package com.xxfc.platform.universal.service.authenticationInterface.impl;
import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.xxfc.platform.universal.biz.UserMessage;
import com.xxfc.platform.universal.service.authenticationInterface.UserAuthentication;
import com.xxfc.platform.universal.utils.CertifHttpUtils;
import com.xxfc.platform.universal.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* 调用四川涪擎认证接口
*
* @author Administrator
*/
@Service
@Slf4j
public class XCFQAuthentication implements UserAuthentication {
private String cAppcode="acea1c8811f748b3a65815f11db357c4";
/**
* 认证相关的数据
*/
private String cHost = "https://idcert.market.alicloudapi.com";
private String cPath = "/idcard";
private String cMethod = "GET";
//响应:认证错误码字段名
private String certifRet = "status";
//响应:认证通过码
private String certifResultCode = "01";
//请求:身份证号字段名
private String idCardName = "idCard";
//请求:用户姓名字段名
private String cName = "name";
@Override
public boolean certificate(UserMessage message) {
//map携带身份证和姓名进行认证
Map<String, String> querys = new HashMap<>();
querys.put(idCardName, message.getIdNumber());
querys.put(cName, message.getName());
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "APPCODE " + cAppcode);
try {
log.info("----querys=========" + querys);
HttpResponse response = HttpUtils.doGet(cHost, cPath, cMethod, headers, querys);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
/**
* 状态码: 200 正常;400 URL无效;401 appCode错误; 403 次数用完; 500 API网管错误
*/
//获取response的body
if (statusCode == 200) {
String result = EntityUtils.toString(response.getEntity());
log.info("----认证结果result=========" + result);
//认证返回的参数是否为空
if (!StringUtils.isBlank(result)) {
Map<String, Object> map = (Map<String, Object>) JSONObject.parse(result);
log.info("----certifRet=========" + certifRet);
if (MapUtil.isNotEmpty(map) || certifResultCode.equals(map.get(certifRet))) {
return true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment