diff --git a/pay-java-ali/README.md b/pay-java-ali/README.md index 84ce975..80d677d 100644 --- a/pay-java-ali/README.md +++ b/pay-java-ali/README.md @@ -150,7 +150,7 @@ ```java - Map result = service.query("支付宝单号", "我方系统单号"); + Map result = service..query("支付宝单号", "我方系统单号"); ``` diff --git a/pay-java-ali/pom.xml b/pay-java-ali/pom.xml index f1d2256..97f86f5 100644 --- a/pay-java-ali/pom.xml +++ b/pay-java-ali/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.0.7-SNAPSHOT + 2.0.6 4.0.0 pay-java-ali diff --git a/pay-java-common/pom.xml b/pay-java-common/pom.xml index 84efcdf..30aeb65 100644 --- a/pay-java-common/pom.xml +++ b/pay-java-common/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.0.7-SNAPSHOT + 2.0.6 4.0.0 jar diff --git a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpRequestTemplate.java b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpRequestTemplate.java index 96ed398..c73c696 100644 --- a/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpRequestTemplate.java +++ b/pay-java-common/src/main/java/com/egzosn/pay/common/http/HttpRequestTemplate.java @@ -1,18 +1,27 @@ package com.egzosn.pay.common.http; +import com.alibaba.fastjson.JSON; import com.egzosn.pay.common.bean.MethodType; import com.egzosn.pay.common.util.str.StringUtils; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.AuthCache; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.conn.ssl.DefaultHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; +import org.apache.http.util.EntityUtils; import javax.net.ssl.SSLContext; import java.io.*; @@ -104,11 +113,13 @@ public class HttpRequestTemplate { */ public CredentialsProvider createProxy(HttpConfigStorage configStorage){ - if (StringUtils.isBlank(configStorage.getHttpProxyHost())) { - return null; + if (StringUtils.isNotBlank(configStorage.getHttpProxyHost())) { + + URI uri = URI.create(configStorage.getHttpProxyHost()); + //http代理地址设置 + httpProxy = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());; } - //http代理地址设置 - httpProxy = new HttpHost(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort()); + if (StringUtils.isBlank(configStorage.getHttpProxyUsername())) { return null; @@ -117,13 +128,14 @@ public class HttpRequestTemplate { // 需要用户认证的代理服务器 CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( - new AuthScope(configStorage.getHttpProxyHost(), configStorage.getHttpProxyPort()), + new AuthScope(httpProxy.getHostName(), httpProxy.getPort()), new UsernamePasswordCredentials(configStorage.getHttpProxyUsername(), configStorage.getHttpProxyPassword())); return credsProvider; } + /** * 设置HTTP请求的配置 * @@ -166,6 +178,9 @@ public class HttpRequestTemplate { public T postForObject(String uri, Object request, Class responseType, Map uriVariables) { return doExecute(URI.create(UriVariables.getUri(uri, uriVariables)), request, responseType, MethodType.POST); } + public T postForObjectAndBasicAuth(String uri, Object request, Class responseType, Object... uriVariables) { + return doExecuteAndBasicAuth(URI.create(UriVariables.getUri(uri, uriVariables)), request, responseType, MethodType.POST); + } public T postForObject(URI uri, Object request, Class responseType){ return doExecute(uri, request, responseType, MethodType.POST); @@ -225,6 +240,7 @@ public class HttpRequestTemplate { public T doExecute(URI uri, Object request, Class responseType, MethodType method){ ClientHttpRequest httpRequest = new ClientHttpRequest(uri ,method, request); httpRequest.setProxy(httpProxy).setResponseType(responseType); + try (CloseableHttpResponse response = httpClient.execute(httpRequest)) { return httpRequest.handleResponse(response); }catch ( IOException e){ @@ -235,6 +251,50 @@ public class HttpRequestTemplate { return null; } + /** + * http 请求执行 + * @param uri 地址 + * @param request 请求数据 + * @param responseType 响应类型 + * @param method 请求方法 + * @param 响应类型 + * @return 类型对象 + */ + public T doExecuteAndBasicAuth(URI uri, Object request, Class responseType, MethodType method){ + //todo 研究研究 + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials( + new AuthScope(uri.getHost(), uri.getPort()), + new UsernamePasswordCredentials("Huodull6190", "12BkDT8152Zj")); + + CloseableHttpClient hc = HttpClients.custom() + .setDefaultCredentialsProvider(credsProvider).build(); + +// ClientHttpRequest httpRequest = new ClientHttpRequest(uri ,method, request); +// httpRequest.setProxy(httpProxy).setResponseType(responseType); + AuthCache authCache = new BasicAuthCache(); + BasicScheme basicAuth = new BasicScheme(); + HttpHost host = new HttpHost(uri.getHost(),uri.getPort(),uri.getScheme()); + authCache.put(host, basicAuth); + + HttpClientContext context = HttpClientContext.create(); +// context.setCredentialsProvider(credsProvider); + context.setAuthCache(authCache); + + HttpPost httpPost = new HttpPost(uri.toString()); + StringEntity entity = new StringEntity(JSON.toJSONString(request), ContentType.APPLICATION_JSON); + httpPost.setEntity(entity); + + try (CloseableHttpResponse response = hc.execute(host,httpPost,context)) { + + return (T)JSON.parseObject(EntityUtils.toString(response.getEntity())); + }catch ( IOException e){ + e.printStackTrace(); + }finally { + httpPost.releaseConnection(); + } + return null; + } /** * http 请求执行 @@ -249,4 +309,29 @@ public class HttpRequestTemplate { return doExecute(URI.create(uri), request, responseType, method); } + /** + * 创建Basic Auth + * @param uri +// * @param username +// * @param password + * @return + */ + private HttpClientContext createBasicAuthContext(URI uri) { +// CredentialsProvider credsProvider = new BasicCredentialsProvider(); +// Credentials defaultCreds = new UsernamePasswordCredentials(username, password); +// credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), defaultCreds); + + AuthCache authCache = new BasicAuthCache(); + BasicScheme basicAuth = new BasicScheme(); + HttpHost host = new HttpHost(uri.getHost(),uri.getPort(),uri.getScheme()); + authCache.put(host, basicAuth); + + HttpClientContext context = HttpClientContext.create(); +// context.setCredentialsProvider(credsProvider); + context.setAuthCache(authCache); + return context; + } + + + } diff --git a/pay-java-demo/pom.xml b/pay-java-demo/pom.xml index b82db34..c4830b4 100644 --- a/pay-java-demo/pom.xml +++ b/pay-java-demo/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.0.7-SNAPSHOT + 2.0.6 4.0.0 war diff --git a/pay-java-fuiou/pom.xml b/pay-java-fuiou/pom.xml index 70cf625..7d0448b 100644 --- a/pay-java-fuiou/pom.xml +++ b/pay-java-fuiou/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.0.7-SNAPSHOT + 2.0.6 4.0.0 pay-java-fuiou diff --git a/pay-java-payoneer/src/main/java/com/egzosn/pay/payoneer/Test.java b/pay-java-payoneer/src/main/java/com/egzosn/pay/payoneer/Test.java new file mode 100644 index 0000000..c35f0c3 --- /dev/null +++ b/pay-java-payoneer/src/main/java/com/egzosn/pay/payoneer/Test.java @@ -0,0 +1,76 @@ +package com.egzosn.pay.common.http; + +import com.alibaba.fastjson.JSON; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.AuthCache; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; + +import java.net.URI; + +/** + * An example of HttpClient can be customized to authenticate + * preemptively using BASIC scheme. + * + * Generally, preemptive authentication can be considered less + * secure than a response to an authentication challenge + * and therefore discouraged. + */ +public class Test { + + public static void main(String[] args) throws Exception { + URI uri = URI.create("https://api.sandbox.payoneer.com/v2/programs/100086190/payees/login-link"); + HttpHost target = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()); + CredentialsProvider credsProvider = new BasicCredentialsProvider(); + credsProvider.setCredentials( + new AuthScope(target.getHostName(), target.getPort()), + new UsernamePasswordCredentials("Huodull6190", "12BkDT8152Zj")); + CloseableHttpClient httpclient = HttpClients.custom() + .setDefaultCredentialsProvider(credsProvider).build(); + try { + + // Create AuthCache instance + AuthCache authCache = new BasicAuthCache(); + // Generate BASIC scheme object and add it to the local + // auth cache + BasicScheme basicAuth = new BasicScheme(); + authCache.put(target, basicAuth); + + // Add AuthCache to the execution context + HttpClientContext localContext = HttpClientContext.create(); + localContext.setAuthCache(authCache); + + HttpPost httpPost = new HttpPost("https://api.sandbox.payoneer.com/v2/programs/100086190/payees/login-link"); + StringEntity entity = new StringEntity(JSON.toJSONString(Pay), ContentType.APPLICATION_JSON); + + httpPost.setEntity(); + System.out.println("Executing request " + httpPost.getRequestLine() + " to target " + target); + for (int i = 0; i < 3; i++) { + CloseableHttpResponse response = httpclient.execute(target, httpPost, localContext); + try { + System.out.println("----------------------------------------"); + System.out.println(response.getStatusLine()); + System.out.println(EntityUtils.toString(response.getEntity())); + } finally { + response.close(); + } + } + } finally { + httpclient.close(); + } + } + +} \ No newline at end of file diff --git a/pay-java-union/pom.xml b/pay-java-union/pom.xml index 5eac662..b17afb9 100644 --- a/pay-java-union/pom.xml +++ b/pay-java-union/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.0.7-SNAPSHOT + 2.0.6 4.0.0 diff --git a/pay-java-wx-youdian/pom.xml b/pay-java-wx-youdian/pom.xml index efbe4a7..e6c1cea 100644 --- a/pay-java-wx-youdian/pom.xml +++ b/pay-java-wx-youdian/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.0.7-SNAPSHOT + 2.0.6 4.0.0 pay-java-wx-youdian diff --git a/pay-java-wx/pom.xml b/pay-java-wx/pom.xml index 32827f7..381e7dd 100644 --- a/pay-java-wx/pom.xml +++ b/pay-java-wx/pom.xml @@ -5,7 +5,7 @@ pay-java-parent com.egzosn - 2.0.7-SNAPSHOT + 2.0.6 4.0.0 pay-java-wx