init
This commit is contained in:
@@ -8,7 +8,7 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "yunxin")
|
||||
public class YunxinProperties {
|
||||
private String baseUrl = "https://api.netease.im/nimserver";
|
||||
private String baseUrl = "https://api.netease.im";
|
||||
private String appKey;
|
||||
private String appSecret;
|
||||
|
||||
|
||||
@@ -13,6 +13,8 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class GlodonTokenInterceptor implements Interceptor {
|
||||
@@ -27,7 +29,7 @@ public class GlodonTokenInterceptor implements Interceptor {
|
||||
request.addHeader("AppKey",yunxinProperties.getAppKey());
|
||||
request.addHeader("Nonce", nonce);
|
||||
request.addHeader("CurTime", curTime);
|
||||
request.addHeader("CheckSum", yunxinProperties.getAppSecret()+nonce+curTime);
|
||||
request.addHeader("CheckSum", getCheckSum(yunxinProperties.getAppSecret(),nonce,curTime));
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -53,4 +55,36 @@ public class GlodonTokenInterceptor implements Interceptor {
|
||||
response.getContent());
|
||||
}
|
||||
|
||||
// 计算并获取CheckSum
|
||||
public static String getCheckSum(String appSecret, String nonce, String curTime) {
|
||||
return encode("sha1", appSecret + nonce + curTime);
|
||||
}
|
||||
|
||||
private static String encode(String algorithm, String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
MessageDigest messageDigest
|
||||
= MessageDigest.getInstance(algorithm);
|
||||
messageDigest.update(value.getBytes());
|
||||
return getFormattedText(messageDigest.digest());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getFormattedText(byte[] bytes) {
|
||||
int len = bytes.length;
|
||||
StringBuilder buf = new StringBuilder(len * 2);
|
||||
for (int j = 0; j < len; j++) {
|
||||
buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
|
||||
buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
|
||||
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user