Unverified Commit a4149a1b authored by Yang Libin's avatar Yang Libin Committed by GitHub

feat: auto renew user sig (#64)

for more detail, refer to #63
parent cbefe8d4
......@@ -22,7 +22,7 @@ Add the Maven dependency:
<dependency>
<groupId>io.github.doocs</groupId>
<artifactId>im-server-sdk-java</artifactId>
<version>0.2.8</version>
<version>0.2.9</version>
</dependency>
```
......
# 介绍
本文档基于腾讯云 IM Server SDK Java <Badge type="tip" text="v0.2.8" vertical="top" /> 编写。
本文档基于腾讯云 IM Server SDK Java <Badge type="tip" text="v0.2.9" vertical="top" /> 编写。
## 前提条件
......
......@@ -10,14 +10,14 @@
<dependency>
<groupId>io.github.doocs</groupId>
<artifactId>im-server-sdk-java</artifactId>
<version>0.2.8</version>
<version>0.2.9</version>
</dependency>
```
### Gradle
```gradle
implementation group: 'io.github.doocs', name: 'im-server-sdk-java', version: '0.2.8'
implementation group: 'io.github.doocs', name: 'im-server-sdk-java', version: '0.2.9'
```
### 下载 jar
......@@ -43,19 +43,22 @@ ImClient client = ImClient.getInstance(appId, identifier, key);
// create a custom ImClient instance
ClientConfiguration config = new ClientConfiguration();
config.setExpireTime(7 * 24 * 60 * 60L);
config.setAutoRenewSig(false);
ImClient client = ImClient.getInstance(appId, identifier, key, config);
```
`ClientConfiguration` 支持可配置参数如下:
| 字段 | 类型 | 说明 | 默认值 |
| -------------- | ------- | ---------------------------- | ------ |
| maxRetries | Integer | HTTP 最大重试次数 | 3 |
| connectTimeout | Long | HTTP 连接超时(毫秒) | 3000 |
| readTimeout | Long | HTTP 读超时(毫秒) | 3000 |
| writeTimeout | Long | HTTP 写超时(毫秒) | 3000 |
| expireTime | Long | UserSig 签名失效时间(毫秒) | 86400 |
| userAgent | String | User-Agent | |
| 字段 | 类型 | 说明 | 默认值 |
| ---------------- | ------- | ----------------------------- | ------ |
| `maxRetries` | int | HTTP 最大重试次数 | 3 |
| `connectTimeout` | long | HTTP 连接超时(毫秒) | 3000 |
| `readTimeout` | long | HTTP 读超时(毫秒) | 3000 |
| `writeTimeout` | long | HTTP 写超时(毫秒) | 3000 |
| `expireTime` | long | UserSig 签名失效时间(秒) | 86400 |
| `autoRenewSig` | boolean | 是否自动进行 UserSig 签名续期 | true |
| `userAgent` | String | User-Agent | |
## 使用示例
......
{
"name": "qcloud-im-server-sdk-java",
"version": "0.2.8",
"version": "0.2.9",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......@@ -4553,4 +4553,4 @@
"dev": true
}
}
}
}
\ No newline at end of file
{
"name": "qcloud-im-server-sdk-java",
"version": "0.2.8",
"version": "0.2.9",
"description": "腾讯云 IM 服务端 SDK API 文档 Java 版",
"main": "index.js",
"scripts": {
......@@ -21,4 +21,4 @@
"@vuepress/plugin-search": "^2.0.0-beta.27",
"vuepress": "^2.0.0-beta.27"
}
}
}
\ No newline at end of file
......@@ -6,7 +6,7 @@
<groupId>io.github.doocs</groupId>
<artifactId>im-server-sdk-java</artifactId>
<version>0.2.8</version>
<version>0.2.9</version>
<packaging>jar</packaging>
<name>qcloud-im-server-sdk-java</name>
......
......@@ -9,13 +9,17 @@ import io.github.doocs.im.util.VersionInfoUtil;
public class ClientConfiguration {
private static final String DEFAULT_USER_AGENT = VersionInfoUtil.getDefaultUserAgent();
private static final int DEFAULT_MAX_RETRIES = 3;
private static final boolean DEFAULT_RENEW_SIG = true;
/**
* default value, unit: milliseconds
* 默认超时时间(毫秒)
*/
private static final long DEFAULT_CONNECT_TIMEOUT = 3000L;
private static final long DEFAULT_READ_TIMEOUT = 3000L;
private static final long DEFAULT_WRITE_TIMEOUT = 3000L;
/**
* UserSig 签名默认失效时间(秒)
*/
private static final long DEFAULT_EXPIRE_TIME = 24 * 60 * 60L;
private int maxRetries = DEFAULT_MAX_RETRIES;
......@@ -23,18 +27,20 @@ public class ClientConfiguration {
private long readTimeout = DEFAULT_READ_TIMEOUT;
private long writeTimeout = DEFAULT_WRITE_TIMEOUT;
private long expireTime = DEFAULT_EXPIRE_TIME;
private boolean autoRenewSig = DEFAULT_RENEW_SIG;
private String userAgent = DEFAULT_USER_AGENT;
public ClientConfiguration() {
}
public ClientConfiguration(int maxRetries, long connectTimeout, long readTimeout,
long writeTimeout, long expireTime, String userAgent) {
public ClientConfiguration(int maxRetries, long connectTimeout, long readTimeout, long writeTimeout,
long expireTime, boolean autoRenewSig, String userAgent) {
this.maxRetries = maxRetries;
this.connectTimeout = connectTimeout;
this.readTimeout = readTimeout;
this.writeTimeout = writeTimeout;
this.expireTime = expireTime;
this.autoRenewSig = autoRenewSig;
this.userAgent = userAgent;
}
......@@ -78,6 +84,14 @@ public class ClientConfiguration {
this.expireTime = expireTime;
}
public boolean isAutoRenewSig() {
return autoRenewSig;
}
public void setAutoRenewSig(boolean autoRenewSig) {
this.autoRenewSig = autoRenewSig;
}
public String getUserAgent() {
return userAgent;
}
......
......@@ -12,10 +12,13 @@ import java.util.concurrent.ThreadLocalRandom;
*/
public class ImClient {
private final long sdkAppId;
private final String key;
private final String userId;
private final String userSig;
private final ClientConfiguration config;
private String userSig;
private long userSigExpireTs;
private static final String VERSION = "v4";
private static final String FORMAT_URL = "https://console.tim.qq.com/%s/%s/%s?sdkappid=%d&identifier=%s&usersig=%s&random=%d&contenttype=json";
......@@ -45,8 +48,10 @@ public class ImClient {
public ImClient(long sdkAppId, String userId, String key, ClientConfiguration config) {
this.sdkAppId = sdkAppId;
this.userId = userId;
this.key = key;
this.config = config;
this.userSig = SigUtil.genUserSig(sdkAppId, key, userId, config.getExpireTime());
this.userSigExpireTs = System.currentTimeMillis() / 1000 + config.getExpireTime() - 100;
account = new Account(this);
message = new Message(this);
......@@ -58,13 +63,29 @@ public class ImClient {
recentContact = new RecentContact(this);
}
private String getUserSig() {
if (config.isAutoRenewSig()) {
long currentTs = System.currentTimeMillis() / 1000;
if (currentTs >= userSigExpireTs) {
synchronized (this) {
if (currentTs >= userSigExpireTs) {
userSig = SigUtil.genUserSig(sdkAppId, key, userId, config.getExpireTime());
userSigExpireTs = currentTs + config.getExpireTime() - 100;
}
}
}
}
return userSig;
}
public ClientConfiguration getConfig() {
return config;
}
public String getUrl(String serviceName, String command) {
String sig = getUserSig();
long random = ThreadLocalRandom.current().nextLong(0, 0x100000000L);
return String.format(FORMAT_URL, VERSION, serviceName, command,
sdkAppId, userId, userSig, random);
sdkAppId, userId, sig, random);
}
}
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