Unverified Commit 4fb1d062 authored by Yang Libin's avatar Yang Libin Committed by GitHub

fix: user sig in IMClient (#35)

* fix: user sig in IMClient * fix: synchronized block * fix #34
parent 4c203690
...@@ -22,7 +22,7 @@ Add the Maven dependency: ...@@ -22,7 +22,7 @@ Add the Maven dependency:
<dependency> <dependency>
<groupId>io.github.doocs</groupId> <groupId>io.github.doocs</groupId>
<artifactId>im-server-sdk-java</artifactId> <artifactId>im-server-sdk-java</artifactId>
<version>0.0.8</version> <version>0.0.9</version>
</dependency> </dependency>
``` ```
......
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
<groupId>io.github.doocs</groupId> <groupId>io.github.doocs</groupId>
<artifactId>im-server-sdk-java</artifactId> <artifactId>im-server-sdk-java</artifactId>
<version>0.0.8</version> <version>0.0.9</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>qcloud-im-server-sdk-java</name> <name>qcloud-im-server-sdk-java</name>
......
...@@ -17,42 +17,40 @@ public class IMClient { ...@@ -17,42 +17,40 @@ public class IMClient {
/** /**
* core methods * core methods
*/ */
public Account account; public final Account account;
public Message message; public final Message message;
public Member member; public final Member member;
public Profile profile; public final Profile profile;
public Group group; public final Group group;
public SNS sns; public final SNS sns;
public Operation operation; public final Operation operation;
public RecentContact recentContact; public final RecentContact recentContact;
/** /**
* init property * init property
*/ */
private final String version = "v4"; private String userSig;
private long userSigExpireTs;
private final Long sdkAppId; private final Long sdkAppId;
private final String userId; private final String userId;
private final String userSig; private final String key;
public static IMClient getInstance(Long sdkAppId, String userId, String key, Long expire) { private static final String VERSION = "v4";
String identify = sdkAppId + "_" + userId; private static final long EXPIRE_TIME = 24 * 60 * 60L;
if (IM_CLIENT.get(identify) == null) {
IM_CLIENT.putIfAbsent(identify, new IMClient(sdkAppId, userId, key, expire));
}
return IM_CLIENT.get(identify);
}
public static IMClient getInstance(Long sdkAppId, String userId, String key) { public static IMClient getInstance(Long sdkAppId, String userId, String key) {
String identify = sdkAppId + "_" + userId; String identify = sdkAppId + "_" + userId;
if (IM_CLIENT.get(identify) == null) { if (!IM_CLIENT.containsKey(identify)) {
IM_CLIENT.putIfAbsent(identify, new IMClient(sdkAppId, userId, key, 24 * 60 * 60L)); IM_CLIENT.putIfAbsent(identify, new IMClient(sdkAppId, userId, key));
} }
return IM_CLIENT.get(identify); return IM_CLIENT.get(identify);
} }
public IMClient(Long sdkAppId, String userId, String key, Long expire) { public IMClient(Long sdkAppId, String userId, String key) {
this.sdkAppId = sdkAppId; this.sdkAppId = sdkAppId;
this.userId = userId; this.userId = userId;
this.userSig = SigUtil.genUserSig(sdkAppId, key, userId, expire); this.key = key;
this.userSig = SigUtil.genUserSig(sdkAppId, key, userId, EXPIRE_TIME);
this.userSigExpireTs = System.currentTimeMillis() / 1000 + EXPIRE_TIME - 100;
account = new Account(this); account = new Account(this);
message = new Message(this); message = new Message(this);
member = new Member(this); member = new Member(this);
...@@ -66,8 +64,22 @@ public class IMClient { ...@@ -66,8 +64,22 @@ public class IMClient {
public String getUrl(String serviceName, String command) { public String getUrl(String serviceName, String command) {
// 随机生成32位无符号整数 // 随机生成32位无符号整数
long random = ThreadLocalRandom.current().nextLong(0, 0x100000000L); long random = ThreadLocalRandom.current().nextLong(0, 0x100000000L);
return String.format(FORMAT_URL, this.version, serviceName, command, String sig = getUserSig();
this.sdkAppId, this.userId, this.userSig, random); return String.format(FORMAT_URL, VERSION, serviceName, command,
sdkAppId, userId, sig, random);
}
private String getUserSig() {
long currentTs = System.currentTimeMillis() / 1000;
if (currentTs >= userSigExpireTs) {
synchronized (this) {
if (currentTs >= userSigExpireTs) {
userSig = SigUtil.genUserSig(sdkAppId, key, userId, EXPIRE_TIME);
userSigExpireTs = currentTs + EXPIRE_TIME - 100;
}
}
}
return userSig;
} }
@Override @Override
......
...@@ -51,7 +51,7 @@ public abstract class GenericResult { ...@@ -51,7 +51,7 @@ public abstract class GenericResult {
@Override @Override
public String toString() { public String toString() {
return "GenericResult{" + return "Result{" +
"actionStatus='" + actionStatus + '\'' + "actionStatus='" + actionStatus + '\'' +
", errorInfo='" + errorInfo + '\'' + ", errorInfo='" + errorInfo + '\'' +
", errorCode='" + errorCode + '\'' + ", errorCode='" + errorCode + '\'' +
......
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