This commit is contained in:
dute7liang
2024-01-07 16:42:24 +08:00
parent 31af6fdc76
commit fad404fe58
52 changed files with 257 additions and 176 deletions

View File

@@ -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' };
}