Commit 19982643 authored by 周健威's avatar 周健威

Merge remote-tracking branch 'origin/base-modify' into base-modify

parents 1f830a15 bdf55076
package com.github.wxiaoqi.security.common.util;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
/**
* @Author vitoHuang
* @Time 2015年8月14日
* @Mark HTTPS请求工具
*/
@Slf4j
public class HTTPSUtils{
/**
* HTTPS json 请求
* @param requestUrl 请求地址
* @param requestMethod 请求方式 POST/GET
* @param msg json方式的请求参数
* @return 返回相应的字符串 异常会输出null
*/
public static String httpRequest(String requestUrl, String requestMethod, String msg){
OutputStream outputStream = null;
InputStream inputStream = null;
try {
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = {new MyX509TrustManager()};
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection();
httpUrlConn.setSSLSocketFactory(ssf);
httpUrlConn.setDoOutput(true);
httpUrlConn.setDoInput(true);
httpUrlConn.setUseCaches(false);
// 设置请求方式(GET/POST)
httpUrlConn.setRequestMethod(requestMethod);
if ("GET".equalsIgnoreCase(requestMethod))
httpUrlConn.connect();
// 当有数据需要提交时
if (null != msg) {
outputStream = httpUrlConn.getOutputStream();
// 注意编码格式,防止中文乱码
outputStream.write(msg.getBytes("UTF-8"));
}
// 将返回的输入流转换成字符串
inputStream = httpUrlConn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer buffer = new StringBuffer();
String str = null;
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
httpUrlConn.disconnect();
return buffer.toString();
} catch (ConnectException ce) {
log.error("Weixin server connection timed out.");
} catch (Exception e) {
log.error("https request error:"+e.getMessage());
}finally{
if(outputStream != null)try{outputStream.close();}catch (Exception e) {}
if(inputStream != null)try{inputStream.close();;}catch (Exception e) {}
}
return null;
}
/**
* HTTPS json 请求
* @param requestUrl
* @param requestMethod
* @param msg
* @return 对字符串进行封装成JSON
*/
public static JSONObject httpRequestToJSON(String requestUrl, String requestMethod, String msg){
String json = httpRequest(requestUrl, requestMethod, msg);
JSONObject jsonObject = null;
if(StringUtils.isNotBlank(json)) jsonObject = JSON.parseObject(json);
return jsonObject;
}
}
package com.github.wxiaoqi.security.common.util;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
public class MyX509TrustManager implements X509TrustManager {
public void checkClientTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] chain, String authType)
throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}
...@@ -2,6 +2,7 @@ package com.xxfc.platform.universal.biz; ...@@ -2,6 +2,7 @@ package com.xxfc.platform.universal.biz;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.wxiaoqi.security.common.util.HTTPSUtils;
import com.github.wxiaoqi.security.common.util.OrderUtil; import com.github.wxiaoqi.security.common.util.OrderUtil;
import com.github.wxiaoqi.security.common.util.process.ResultCode; import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.github.wxiaoqi.security.common.util.process.SystemConfig; import com.github.wxiaoqi.security.common.util.process.SystemConfig;
...@@ -17,10 +18,7 @@ import com.xxfc.platform.universal.entity.OrderPay; ...@@ -17,10 +18,7 @@ import com.xxfc.platform.universal.entity.OrderPay;
import com.xxfc.platform.universal.mapper.OrderPayMapper; import com.xxfc.platform.universal.mapper.OrderPayMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz; import com.github.wxiaoqi.security.common.biz.BaseBiz;
import tk.mybatis.mapper.entity.Example; import tk.mybatis.mapper.entity.Example;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 订单支付 * 订单支付
...@@ -98,15 +96,25 @@ public class OrderPayBiz extends BaseBiz<OrderPayMapper,OrderPay> { ...@@ -98,15 +96,25 @@ public class OrderPayBiz extends BaseBiz<OrderPayMapper,OrderPay> {
String url=pay.getNotifyUrl(); String url=pay.getNotifyUrl();
url+="&tradeNo="+orderNo; url+="&tradeNo="+orderNo;
log.error("---支付回调处理---orderNo======="+orderNo+"----notifyUrl===="+url); log.error("---支付回调处理---orderNo======="+orderNo+"----notifyUrl===="+url);
String result=HTTPUtils.doGet(url); String result="";
if(url.contains("https")||url.contains("HTTPS")){
result= HTTPSUtils.httpRequest(url, "GET",null);
}else{
result= HTTPUtils.doGet(url);
}
log.error("---支付回调处理---orderNo======="+orderNo+"---result==="+result); log.error("---支付回调处理---orderNo======="+orderNo+"---result==="+result);
} }
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
String url="http://10.1.37.248:8765/api/order/baseOrder/app/unauth/notifyUrl?orderNo=20190603111125010022&tradeNo=20190603111125000003"; String url="https://xxtest.upyuns.com/api/order/baseOrder/app/unauth/notifyUrl?orderNo=20190603141137010007&tradeNo=20190603141137000002";
String result=HTTPUtils.doGet(url); String result="";
if(url.contains("https")||url.contains("HTTPS")){
result= HTTPSUtils.httpRequest(url, "GET",null);
}else{
result= HTTPUtils.doGet(url);
}
System.out.println(result); System.out.println(result);
} }
} }
\ No newline at end of file
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