From f98946750a535c03520e2853bd9c6709fced3c27 Mon Sep 17 00:00:00 2001 From: YUANYE <418471657@qq.com> Date: Tue, 8 Jun 2021 14:46:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=88=86=E8=B4=A6=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/pay/WechatPayController.java | 7 +- .../java/com/hai/common/utils/HttpsUtils.java | 85 +++++++++++++++--- .../java/com/hai/common/utils/WxUtils.java | 12 +++ .../java/com/hai/service/pay/PayService.java | 1 + .../main/java/privatekey/apiclient_cert.p12 | Bin 0 -> 2694 bytes .../main/java/privatekey/apiclient_cert.pem | 23 +++++ .../main/java/privatekey/apiclient_key.pem | 28 ++++++ 7 files changed, 144 insertions(+), 12 deletions(-) create mode 100644 hai-service/src/main/java/privatekey/apiclient_cert.p12 create mode 100644 hai-service/src/main/java/privatekey/apiclient_cert.pem create mode 100644 hai-service/src/main/java/privatekey/apiclient_key.pem diff --git a/hai-cweb/src/main/java/com/cweb/controller/pay/WechatPayController.java b/hai-cweb/src/main/java/com/cweb/controller/pay/WechatPayController.java index 633c89e5..65eebec9 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/pay/WechatPayController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/pay/WechatPayController.java @@ -163,12 +163,13 @@ public class WechatPayController { WxSharingReceiversVO receiversVO = new WxSharingReceiversVO(); receiversVO.setAccount("1609882817"); receiversVO.setType("MERCHANT_ID"); - receiversVO.setName("嗨加油"); + receiversVO.setName("个体户黎杨珍"); receiversVO.setRelation_type("SERVICE_PROVIDER"); Map map = new HashMap<>(); map.put("mch_id" , "1289663601"); + map.put("sub_mch_id" , "1609882817"); map.put("appid" , "wx637bd6f7314daa46"); map.put("nonce_str" , WxUtils.makeNonStr()); map.put("sign_type" , "HMAC-SHA256"); @@ -180,7 +181,7 @@ public class WechatPayController { String url = "https://api.mch.weixin.qq.com/pay/profitsharingaddreceiver"; String xm = WxUtils.mapToXml(map); - String rest = HttpsUtils.postData(url, xm); + String rest = String.valueOf(HttpsUtils.postData(url, xm)); System.out.println(rest); log.info("微信支付 -> 添加分账接收方:处理开始"); @@ -189,4 +190,6 @@ public class WechatPayController { } } + + } diff --git a/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java b/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java index 4be9e855..c36787c4 100644 --- a/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java +++ b/hai-service/src/main/java/com/hai/common/utils/HttpsUtils.java @@ -15,6 +15,7 @@ import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLContexts; import org.apache.http.conn.ssl.TrustStrategy; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; @@ -29,10 +30,13 @@ import org.slf4j.LoggerFactory; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; +import javax.net.ssl.SSLSocketFactory; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.Charset; import java.security.GeneralSecurityException; +import java.security.KeyStore; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.ArrayList; @@ -459,12 +463,13 @@ public class HttpsUtils { } /** - - * - * @param url API地址 - * @return - */ - public static String postData(String url, String str) { + * @Author Sum1Dream + * @Description // 不带证书请求xml + * @Date 11:42 2021/6/8 + * @Param [url, str] + * @return com.alibaba.fastjson.JSONObject + **/ + public static JSONObject postData(String url, String str) { CloseableHttpClient httpClient = null; if (url.startsWith("https")) { httpClient = HttpClients.custom().setSSLSocketFactory(createSSLConnSocketFactory()) @@ -480,13 +485,57 @@ public class HttpsUtils { httpPost.setConfig(requestConfig); StringEntity stringEntity = new StringEntity(str, "UTF-8");// 解决中文乱码问题 + stringEntity.setContentEncoding("UTF-8"); httpPost.addHeader("Content-Type", "text/xml"); httpPost.setEntity(stringEntity); + response = httpClient.execute(httpPost); + HttpEntity entity = response.getEntity(); + httpStr = EntityUtils.toString(entity, "UTF-8"); + return JSON.parseObject(httpStr); + } catch (Exception e) { + log.error(e.getMessage(),e); + } finally { + if (response != null) { + try { + EntityUtils.consume(response.getEntity()); + } catch (IOException e) { + log.error(e.getMessage(),e); + } + } + } + return null; + } - HttpResponse httpResponse = httpClient.execute(httpPost); - HttpEntity httpEntity = httpResponse.getEntity(); - return EntityUtils.toString(httpEntity, "UTF-8"); + /** + * @Author Sum1Dream + * @Description // 带证书请求 xml + * @Date 11:45 2021/6/8 + * @Param [url, mchId, str] + * @return com.alibaba.fastjson.JSONObject + **/ + public static JSONObject postData(String url, String mchId , String str) { + CloseableHttpClient httpClient = null; + if (url.startsWith("https")) { + httpClient = HttpClients.custom().setSSLSocketFactory(readCertificate(mchId)) + .setConnectionManager(connMgr).setDefaultRequestConfig(requestConfig).build(); + } else { + httpClient = HttpClients.createDefault(); + } + String httpStr = null; + CloseableHttpResponse response = null; + try { + HttpPost httpPost = new HttpPost(url); + httpPost.setConfig(requestConfig); + + StringEntity stringEntity = new StringEntity(str, "UTF-8");// 解决中文乱码问题 + stringEntity.setContentEncoding("UTF-8"); + httpPost.addHeader("Content-Type", "text/xml"); + httpPost.setEntity(stringEntity); + response = httpClient.execute(httpPost); + HttpEntity entity = response.getEntity(); + httpStr = EntityUtils.toString(entity, "UTF-8"); + return JSON.parseObject(httpStr); } catch (Exception e) { log.error(e.getMessage(),e); } finally { @@ -501,7 +550,23 @@ public class HttpsUtils { return null; } - + public static SSLConnectionSocketFactory readCertificate(String mchId) { + try { + KeyStore keyStore = KeyStore.getInstance("PKCS12"); + FileInputStream stream = new FileInputStream( "F:/mine/hai-server/hai-service/src/main/java/privatekey/apiclient_cert.p12"); + keyStore.load(stream , mchId.toCharArray()); + stream.close(); + SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mchId.toCharArray()).build(); + return new SSLConnectionSocketFactory( + sslcontext, + new String[]{"TLSv1"}, + null, + SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER); + } catch (Exception e) { + System.out.println("证书双向认证出现异常" + e); + } + return null; + } /** * 创建SSL安全连接 diff --git a/hai-service/src/main/java/com/hai/common/utils/WxUtils.java b/hai-service/src/main/java/com/hai/common/utils/WxUtils.java index 10e70a06..f2d62f68 100644 --- a/hai-service/src/main/java/com/hai/common/utils/WxUtils.java +++ b/hai-service/src/main/java/com/hai/common/utils/WxUtils.java @@ -1,5 +1,6 @@ package com.hai.common.utils; +import com.alibaba.fastjson.JSON; import com.google.common.collect.Maps; import com.hai.common.pay.util.sdk.WXPayConstants; import com.hai.common.pay.util.sdk.WXPayXmlUtil; @@ -7,15 +8,23 @@ import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.naming.NoNameCoder; import com.thoughtworks.xstream.io.xml.XppDriver; import net.sf.cglib.beans.BeanMap; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLContexts; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.SSLSocketFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; +import java.io.File; +import java.io.FileInputStream; import java.io.StringWriter; +import java.security.KeyStore; import java.security.MessageDigest; import java.security.SecureRandom; import java.util.*; @@ -292,4 +301,7 @@ public class WxUtils { return output; } + + + } diff --git a/hai-service/src/main/java/com/hai/service/pay/PayService.java b/hai-service/src/main/java/com/hai/service/pay/PayService.java index f3b47bf1..c9345b8a 100644 --- a/hai-service/src/main/java/com/hai/service/pay/PayService.java +++ b/hai-service/src/main/java/com/hai/service/pay/PayService.java @@ -18,4 +18,5 @@ public interface PayService { */ void paySuccess(Map map, String payType) throws Exception; + } diff --git a/hai-service/src/main/java/privatekey/apiclient_cert.p12 b/hai-service/src/main/java/privatekey/apiclient_cert.p12 new file mode 100644 index 0000000000000000000000000000000000000000..54e86e504919e57e505ef4689d664a94579b72a7 GIT binary patch literal 2694 zcmY+Ec{~#iAICSgvAM;_$T?TT94*HbjSNYSkfG#iY(nm6TF#Z3xeB?95FtlWNv`E6 zXQG6nTuE|YkKgO{JWtQ_$LIBZeczw&>-*;$O@&d}fb3{0>Ha1Q` z6cyxGe#?#H_t=ixm#&v(Ir`sO#ABf6(D{zO+_fHAe5-#ziw?b7?VQF8Jn1JAzLAwV zX=|cDp}>h*oJrtfoweXUPi-|Db{k@qIy%vOE;?49o4+ueMnc^*f(=Ez__IH2!}7L^ zr_?}o3r}i0KIV8&Qu#cp-^N>fYh3GjeeRe^KEEkf-GG< zZrHJQ<4JQaS5tDIowkf?$WV@ns`bo@JTPdoP9`G7j)we}wrRT7LZ}qgi>!y&zza>C zcw;6CLN4hsh6IFIA)FBufh_^y587 zIb9Y}sQnhCm~Pj>*T4}S)0{!yg;98?MQm;vz4;RqL!!Lh|GwL z*t6tNF}OpMqQbGB6+uYN$Jt!@8q>TKx*b%%#Zh=j>(zixJSstZQDmqU<3oboC_Vq) zh9D&{w$59{ja%^+%n;HcX%Y z?qLCE5sk*Qa<6P>1<$hNOf$(dX@i4~mPd})%}%nf=c47&xew59uM>yRf8v^5Q{{8= zMCd<8!NFW|!zK$YIa#iIm!2heGOwptiW7VXdVr@qrY2{SUOW9aA+bV-$>9sIJ_1g_fb{2&AR0m&V= zFP*!HDK)Awfydl(E|0WzX;B4)YkO_jLM+se1#jBW_JtZ^(>x`kh>HZqjGLM_-(8$& zQLX0~kJ%$Ny4MG|tR=~%abDPFvC91h!mKwn%2?;69@5%=Vs%NfQuulenhFm7A3^0& z!CoLL*zEvcJO}~8^&efqfNTd|`*|R=x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