mirror of
https://github.com/yanlongqi/jhinno-openapi-java-sdk.git
synced 2026-03-22 06:15:10 +08:00
fix(获取Token): 优化Token获取时间戳,支持自定义获取
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
package com.jhinno.sdk.openapi;
|
package com.jhinno.sdk.openapi;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.util.StdDateFormat;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yanlongqi
|
* @author yanlongqi
|
||||||
* @date 2024/1/31 10:17
|
* @date 2024/1/31 10:17
|
||||||
@@ -83,10 +87,10 @@ public class CommonConstant {
|
|||||||
*/
|
*/
|
||||||
public static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
public static final String NORM_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Rest请求JSON编码
|
* 时间转换
|
||||||
*/
|
*/
|
||||||
public static final String CONTENT_TYPE_APPLICATION_JSON = "application/json";
|
public static final DateFormat HTTP_DATETIME_FORMAT = new StdDateFormat();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,8 +161,7 @@ public class JHApiExecution {
|
|||||||
if (authType == AuthType.ACCESS_SECRET_MODE || !isUsedServerTime) {
|
if (authType == AuthType.ACCESS_SECRET_MODE || !isUsedServerTime) {
|
||||||
return String.valueOf(System.currentTimeMillis());
|
return String.valueOf(System.currentTimeMillis());
|
||||||
}
|
}
|
||||||
// todo 获取服务器的时间
|
return jhApiClient.getAppformServerCurrentTimeMillis();
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import com.jhinno.sdk.openapi.ClientErrorCode;
|
|||||||
import com.jhinno.sdk.openapi.ClientException;
|
import com.jhinno.sdk.openapi.ClientException;
|
||||||
import com.jhinno.sdk.openapi.CommonConstant;
|
import com.jhinno.sdk.openapi.CommonConstant;
|
||||||
import com.jhinno.sdk.openapi.api.ResponseResult;
|
import com.jhinno.sdk.openapi.api.ResponseResult;
|
||||||
|
import com.jhinno.sdk.openapi.api.auth.AuthPathConstant;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
@@ -90,7 +91,7 @@ public class JHApiClient {
|
|||||||
InputStream content = apiHttpClient.get(getUrl(path), headers);
|
InputStream content = apiHttpClient.get(getUrl(path), headers);
|
||||||
return mapper.readValue(content, type);
|
return mapper.readValue(content, type);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ClientException(e.getMessage());
|
throw new ClientException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,7 +182,7 @@ public class JHApiClient {
|
|||||||
InputStream content = apiHttpClient.post(getUrl(path), bodyStr, headers);
|
InputStream content = apiHttpClient.post(getUrl(path), bodyStr, headers);
|
||||||
return mapper.readValue(content, type);
|
return mapper.readValue(content, type);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ClientException(e.getMessage());
|
throw new ClientException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -210,7 +211,7 @@ public class JHApiClient {
|
|||||||
InputStream content = apiHttpClient.put(getUrl(path), bodyStr, headers);
|
InputStream content = apiHttpClient.put(getUrl(path), bodyStr, headers);
|
||||||
return mapper.readValue(content, type);
|
return mapper.readValue(content, type);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ClientException(e.getMessage());
|
throw new ClientException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,7 +276,7 @@ public class JHApiClient {
|
|||||||
InputStream content = apiHttpClient.delete(getUrl(path), headers);
|
InputStream content = apiHttpClient.delete(getUrl(path), headers);
|
||||||
return mapper.readValue(content, type);
|
return mapper.readValue(content, type);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ClientException(e.getMessage());
|
throw new ClientException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -316,7 +317,16 @@ public class JHApiClient {
|
|||||||
InputStream content = apiHttpClient.upload(getUrl(path), keyName, fileName, is, body, headers);
|
InputStream content = apiHttpClient.upload(getUrl(path), keyName, fileName, is, body, headers);
|
||||||
return mapper.readValue(content, type);
|
return mapper.readValue(content, type);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new ClientException(e.getMessage());
|
throw new ClientException(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAppformServerCurrentTimeMillis() {
|
||||||
|
try {
|
||||||
|
long currentTimeMillis = apiHttpClient.getAppformServerCurrentTimeMillis(getUrl(AuthPathConstant.PING));
|
||||||
|
return String.valueOf(currentTimeMillis);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ClientException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.jhinno.sdk.openapi.client;
|
package com.jhinno.sdk.openapi.client;
|
||||||
|
|
||||||
|
|
||||||
|
import com.jhinno.sdk.openapi.AuthType;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -68,4 +70,15 @@ public interface JHApiHttpClient {
|
|||||||
InputStream upload(String url, String keyName, String fileName, InputStream is, Map<String, Object> body, Map<String, Object> headers) throws IOException;
|
InputStream upload(String url, String keyName, String fileName, InputStream is, Map<String, Object> body, Map<String, Object> headers) throws IOException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取Appform服务器当前的时间
|
||||||
|
* <p/>
|
||||||
|
* {@link AuthType#TOKEN_MODE}模式,并且打开了获取服务器时间的开关需要实现改方法
|
||||||
|
*
|
||||||
|
* @return 服务器的时间
|
||||||
|
*/
|
||||||
|
default long getAppformServerCurrentTimeMillis(String url) throws IOException {
|
||||||
|
return System.currentTimeMillis();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ package com.jhinno.sdk.openapi.client;
|
|||||||
|
|
||||||
import com.jhinno.sdk.openapi.ClientErrorCode;
|
import com.jhinno.sdk.openapi.ClientErrorCode;
|
||||||
import com.jhinno.sdk.openapi.ClientException;
|
import com.jhinno.sdk.openapi.ClientException;
|
||||||
|
import com.jhinno.sdk.openapi.CommonConstant;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.http.Header;
|
||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
@@ -31,6 +33,7 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.security.KeyManagementException;
|
import java.security.KeyManagementException;
|
||||||
import java.security.KeyStoreException;
|
import java.security.KeyStoreException;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@@ -204,4 +207,27 @@ public class JHApiHttpClientImpl implements JHApiHttpClient {
|
|||||||
httpPost.setEntity(builder.build());
|
httpPost.setEntity(builder.build());
|
||||||
return request(httpPost, headers).getContent();
|
return request(httpPost, headers).getContent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getAppformServerCurrentTimeMillis(String url) throws IOException {
|
||||||
|
HttpGet httpGet = new HttpGet(url);
|
||||||
|
httpGet.setConfig(requestConfig);
|
||||||
|
try {
|
||||||
|
HttpResponse response = closeableHttpClient.execute(httpGet);
|
||||||
|
Header header = response.getFirstHeader("Date");
|
||||||
|
if (header == null) {
|
||||||
|
throw new ClientException("获取时间戳响应为空!");
|
||||||
|
}
|
||||||
|
String value = header.getValue();
|
||||||
|
if (StringUtils.isBlank(value)) {
|
||||||
|
throw new ClientException("获取时间戳响应头为空!");
|
||||||
|
}
|
||||||
|
return CommonConstant.HTTP_DATETIME_FORMAT.parse(value).getTime();
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new ClientException("时间格式获取失败,失败原因:" + e.getMessage(), e);
|
||||||
|
}finally {
|
||||||
|
httpGet.releaseConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user