提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.signature.core.redis; |
H |
2 |
|
|
3 |
import lombok.AllArgsConstructor; |
|
4 |
import org.springframework.data.redis.core.StringRedisTemplate; |
|
5 |
|
|
6 |
import java.util.concurrent.TimeUnit; |
|
7 |
|
|
8 |
/** |
|
9 |
* HTTP API 签名 Redis DAO |
|
10 |
* |
|
11 |
* @author Zhougang |
|
12 |
*/ |
|
13 |
@AllArgsConstructor |
|
14 |
public class ApiSignatureRedisDAO { |
|
15 |
|
|
16 |
private final StringRedisTemplate stringRedisTemplate; |
|
17 |
|
|
18 |
/** |
|
19 |
* 验签随机数 |
|
20 |
* |
|
21 |
* KEY 格式:signature_nonce:%s // 参数为 随机数 |
|
22 |
* VALUE 格式:String |
|
23 |
* 过期时间:不固定 |
|
24 |
*/ |
|
25 |
private static final String SIGNATURE_NONCE = "api_signature_nonce:%s"; |
|
26 |
|
|
27 |
/** |
|
28 |
* 签名密钥 |
|
29 |
* |
|
30 |
* HASH 结构 |
|
31 |
* KEY 格式:%s // 参数为 appid |
|
32 |
* VALUE 格式:String |
|
33 |
* 过期时间:永不过期(预加载到 Redis) |
|
34 |
*/ |
|
35 |
private static final String SIGNATURE_APPID = "api_signature_app"; |
|
36 |
|
|
37 |
// ========== 验签随机数 ========== |
|
38 |
|
|
39 |
public String getNonce(String nonce) { |
|
40 |
return stringRedisTemplate.opsForValue().get(formatNonceKey(nonce)); |
|
41 |
} |
|
42 |
|
|
43 |
public void setNonce(String nonce, int time, TimeUnit timeUnit) { |
|
44 |
stringRedisTemplate.opsForValue().set(formatNonceKey(nonce), "", time, timeUnit); |
|
45 |
} |
|
46 |
|
|
47 |
private static String formatNonceKey(String key) { |
|
48 |
return String.format(SIGNATURE_NONCE, key); |
|
49 |
} |
|
50 |
|
|
51 |
// ========== 签名密钥 ========== |
|
52 |
|
|
53 |
public String getAppSecret(String appId) { |
|
54 |
return (String) stringRedisTemplate.opsForHash().get(SIGNATURE_APPID, appId); |
|
55 |
} |
|
56 |
|
|
57 |
} |