Skip to content

Commit f8fcf6b

Browse files
committed
add ip address tool
1 parent 6193120 commit f8fcf6b

File tree

5 files changed

+87
-50
lines changed

5 files changed

+87
-50
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ https://mvnrepository.com/artifact/com.alipay.global.sdk/global-open-sdk-java
1212
<dependency>
1313
<groupId>com.alipay.global.sdk</groupId>
1414
<artifactId>global-open-sdk-java</artifactId>
15-
<version>2.0.24</version>
15+
<version>2.0.25</version>
1616
</dependency>
1717
```
1818

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.alipay.global.sdk</groupId>
55
<artifactId>global-open-sdk-java</artifactId>
66
<packaging>jar</packaging>
7-
<version>2.0.24</version>
7+
<version>2.0.25</version>
88
<name>global-open-sdk-java</name>
99
<url>https://github.com/alipay/global-open-sdk-java</url>
1010
<description>

src/main/java/com/alipay/global/api/BaseAlipayClient.java

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@
77
import com.alipay.global.api.request.AlipayRequest;
88
import com.alipay.global.api.response.AlipayResponse;
99
import com.alipay.global.api.tools.Constants;
10-
import com.alipay.global.api.tools.SignatureTool;
1110
import com.alipay.global.api.tools.DateTool;
11+
import com.alipay.global.api.tools.SignatureTool;
1212
import org.apache.commons.lang3.StringUtils;
1313

1414
import java.util.HashMap;
1515
import java.util.Map;
1616

17-
public abstract class BaseAlipayClient implements AlipayClient{
17+
public abstract class BaseAlipayClient implements AlipayClient {
1818

1919
private static final Integer DEFAULT_KEY_VERSION = 1;
2020
/**
21-
* eg:https://open-na.alipay.com
21+
* eg: https://open-na.alipay.com
2222
*/
23-
private String gatewayUrl;
23+
private String gatewayUrl;
2424
/**
2525
* merchants private key
2626
*/
@@ -30,10 +30,10 @@ public abstract class BaseAlipayClient implements AlipayClient{
3030
*/
3131
private String alipayPublicKey;
3232

33-
public BaseAlipayClient(){
33+
public BaseAlipayClient() {
3434
}
3535

36-
public BaseAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey ){
36+
public BaseAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey) {
3737
this.gatewayUrl = gatewayUrl;
3838
this.merchantPrivateKey = merchantPrivateKey;
3939
this.alipayPublicKey = alipayPublicKey;
@@ -43,12 +43,12 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
4343

4444
checkRequestParam(alipayRequest);
4545

46-
String clientId = alipayRequest.getClientId();
47-
String httpMethod = alipayRequest.getHttpMethod();
48-
String path = alipayRequest.getPath();
49-
Integer keyVersion = alipayRequest.getKeyVersion();
50-
String reqTime = DateTool.getCurrentTimeMillis();
51-
String reqBody = JSON.toJSONString(alipayRequest);
46+
String clientId = alipayRequest.getClientId();
47+
String httpMethod = alipayRequest.getHttpMethod();
48+
String path = alipayRequest.getPath();
49+
Integer keyVersion = alipayRequest.getKeyVersion();
50+
String reqTime = DateTool.getCurrentTimeMillis();
51+
String reqBody = JSON.toJSONString(alipayRequest);
5252

5353
/**
5454
* 对内容加签(Sign the content)
@@ -58,9 +58,9 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
5858
/**
5959
* 生成必要header(Generate required headers)
6060
*/
61-
Map<String, String> header = buildBaseHeader(reqTime, clientId, keyVersion, signValue);
61+
Map<String, String> header = buildBaseHeader(reqTime, clientId, keyVersion, signValue);
6262
Map<String, String> customHeader = buildCustomHeader();
63-
if(customHeader != null && customHeader.size() > 0){
63+
if (customHeader != null && !customHeader.isEmpty()) {
6464
header.putAll(customHeader);
6565
}
6666

@@ -70,33 +70,33 @@ public <T extends AlipayResponse> T execute(AlipayRequest<T> alipayRequest) thro
7070
*/
7171
HttpRpcResult rsp = sendRequest(requestUrl, httpMethod, header, reqBody);
7272

73-
if(rsp == null){
73+
if (rsp == null) {
7474
throw new AlipayApiException("HttpRpcResult is null.");
7575
}
7676

77-
int httpRespCode = rsp.getRspCode();
78-
String rspBody = rsp.getRspBody();
79-
if(httpRespCode != Constants.HTTP_SUCCESS_CODE){
77+
int httpRespCode = rsp.getRspCode();
78+
String rspBody = rsp.getRspBody();
79+
if (httpRespCode != Constants.HTTP_SUCCESS_CODE) {
8080
throw new AlipayApiException("Response data error, rspBody:" + rspBody);
8181
}
82-
Class<T> responseClass = alipayRequest.getResponseClass();
83-
T alipayResponse = JSON.parseObject(rspBody, responseClass);
84-
Result result = alipayResponse.getResult();
85-
if(result == null){
82+
Class<T> responseClass = alipayRequest.getResponseClass();
83+
T alipayResponse = JSON.parseObject(rspBody, responseClass);
84+
Result result = alipayResponse.getResult();
85+
if (result == null) {
8686
throw new AlipayApiException("Response data error, result field is null, rspBody:" + rspBody);
8787
}
8888

8989
String rspSignValue = rsp.getRspSign();
90-
String rspTime = rsp.getResponseTime();
91-
if(null == rspSignValue || rspSignValue.isEmpty() || null == rspTime || rspTime.isEmpty()){
90+
String rspTime = rsp.getResponseTime();
91+
if (null == rspSignValue || rspSignValue.isEmpty() || null == rspTime || rspTime.isEmpty()) {
9292
return alipayResponse;
9393
}
9494

9595
/**
9696
* 对返回结果验签(Verify the result signature)
9797
*/
9898
boolean isVerifySuccess = checkRspSign(httpMethod, path, clientId, rspTime, rspBody, rspSignValue);
99-
if(!isVerifySuccess){
99+
if (!isVerifySuccess) {
100100
throw new AlipayApiException("Response signature verify fail.");
101101
}
102102

@@ -113,84 +113,83 @@ private String genSignValue(String httpMethod, String path, String clientId, Str
113113
return signatureValue;
114114
}
115115

116-
private boolean checkRspSign(String httpMethod, String path, String clientId, String responseTime, String rspBody, String rspSignValue) throws AlipayApiException{
117-
try{
116+
private boolean checkRspSign(String httpMethod, String path, String clientId, String responseTime, String rspBody, String rspSignValue) throws AlipayApiException {
117+
try {
118118
boolean isVerify = SignatureTool.verify(httpMethod, path, clientId, responseTime, rspBody, rspSignValue, alipayPublicKey);
119119
return isVerify;
120-
} catch(Exception e){
120+
} catch (Exception e) {
121121
throw new AlipayApiException(e);
122122
}
123123

124124
}
125125

126-
private void checkRequestParam(AlipayRequest alipayRequest) throws AlipayApiException{
127-
if(alipayRequest == null){
126+
private void checkRequestParam(AlipayRequest alipayRequest) throws AlipayApiException {
127+
if (alipayRequest == null) {
128128
throw new AlipayApiException("alipayRequest can't null");
129129
}
130130

131131
String clientId = alipayRequest.getClientId();
132-
String httpMehod = alipayRequest.getHttpMethod();
132+
String httpMethod = alipayRequest.getHttpMethod();
133133
String path = alipayRequest.getPath();
134134

135-
if(StringUtils.isBlank(gatewayUrl)){
135+
if (StringUtils.isBlank(gatewayUrl)) {
136136
throw new AlipayApiException("gatewayUrl can't null");
137137
}
138138

139-
if(StringUtils.isBlank(clientId)){
139+
if (StringUtils.isBlank(clientId)) {
140140
throw new AlipayApiException("clientId can't null");
141141
}
142142

143-
if(StringUtils.isBlank(httpMehod)){
143+
if (StringUtils.isBlank(httpMethod)) {
144144
throw new AlipayApiException("httpMehod can't null");
145145
}
146146

147-
if(StringUtils.isBlank(path)){
147+
if (StringUtils.isBlank(path)) {
148148
throw new AlipayApiException("path can't null");
149149
}
150150

151-
if(!path.startsWith("/")){
151+
if (!path.startsWith("/")) {
152152
throw new AlipayApiException("path must start with /");
153153
}
154154

155155
}
156156

157-
private String genRequestUrl(String path){
158-
if(!gatewayUrl.startsWith("http://") && !gatewayUrl.startsWith("https://")){
157+
private String genRequestUrl(String path) {
158+
if (!gatewayUrl.startsWith("http://") && !gatewayUrl.startsWith("https://")) {
159159
gatewayUrl = "https://" + gatewayUrl;
160160
}
161-
if(gatewayUrl.endsWith("/")){
161+
if (gatewayUrl.endsWith("/")) {
162162
int len = gatewayUrl.length();
163163
gatewayUrl = gatewayUrl.substring(0, len - 1);
164164
}
165-
String requestUrl = gatewayUrl + path;
166-
return requestUrl;
165+
return gatewayUrl + path;
167166

168167
}
169168

170169
/**
171170
* Generate required headers
171+
*
172172
* @param requestTime
173173
* @param clientId
174174
* @param keyVersion
175175
* @param signatureValue
176176
* @return
177177
*/
178-
private Map<String,String> buildBaseHeader(String requestTime, String clientId, Integer keyVersion, String signatureValue){
178+
private Map<String, String> buildBaseHeader(String requestTime, String clientId, Integer keyVersion, String signatureValue) {
179179
Map<String, String> header = new HashMap<String, String>();
180180
header.put(Constants.CONTENT_TYPE_HEADER, "application/json; charset=UTF-8");
181181
header.put(Constants.REQ_TIME_HEADER, requestTime);
182182
header.put(Constants.CLIENT_ID_HEADER, clientId);
183-
if(keyVersion == null){
183+
if (keyVersion == null) {
184184
keyVersion = DEFAULT_KEY_VERSION;
185185
}
186186
String signatureHeader = "algorithm=RSA256,keyVersion=" + keyVersion + ",signature=" + signatureValue;
187187
header.put(Constants.REQ_SIGN_HEADER, signatureHeader);
188188
return header;
189189
}
190190

191-
public abstract Map<String,String> buildCustomHeader();
192-
193-
public abstract HttpRpcResult sendRequest(String requestUrl, String httpMethod, Map<String, String> header, String reqBody)throws AlipayApiException;
191+
public abstract Map<String, String> buildCustomHeader();
194192

193+
public abstract HttpRpcResult sendRequest(String requestUrl, String httpMethod, Map<String, String> header, String reqBody) throws AlipayApiException;
195194

196195
}

src/main/java/com/alipay/global/api/DefaultAlipayClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package com.alipay.global.api;
22

3-
import java.util.Map;
4-
53
import com.alipay.global.api.exception.AlipayApiException;
64
import com.alipay.global.api.net.DefaultHttpRPC;
75
import com.alipay.global.api.net.HttpRpcResult;
86

7+
import java.util.Map;
8+
99
public class DefaultAlipayClient extends BaseAlipayClient {
1010

1111
public DefaultAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey) {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.alipay.global.api.tools;
2+
3+
import java.net.InetAddress;
4+
import java.net.NetworkInterface;
5+
import java.net.SocketException;
6+
import java.util.Enumeration;
7+
8+
public class IpAddressTool {
9+
10+
public static String getIpAddress() {
11+
try {
12+
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface
13+
.getNetworkInterfaces();
14+
15+
while (networkInterfaces.hasMoreElements()) {
16+
NetworkInterface networkInterface = networkInterfaces.nextElement();
17+
18+
if (networkInterface.isLoopback() || networkInterface.isVirtual()
19+
|| !networkInterface.isUp()) {
20+
continue;
21+
}
22+
23+
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
24+
while (inetAddresses.hasMoreElements()) {
25+
InetAddress inetAddress = inetAddresses.nextElement();
26+
27+
if (inetAddress instanceof java.net.Inet4Address) {
28+
return inetAddress.getHostAddress();
29+
}
30+
}
31+
}
32+
} catch (SocketException e) {
33+
e.printStackTrace();
34+
}
35+
return null;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)