-
Notifications
You must be signed in to change notification settings - Fork 0
/
RedisProvider.java
84 lines (74 loc) · 2.33 KB
/
RedisProvider.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package org.cardanofoundation.authentication.provider;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.cardanofoundation.authentication.constant.RedisConstant;
import org.cardanofoundation.explorer.common.exceptions.BusinessException;
import org.cardanofoundation.explorer.common.exceptions.enums.CommonErrorCode;
import org.cardanofoundation.explorer.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Component
@Log4j2
@RequiredArgsConstructor
public class RedisProvider {
@Value("${timeToLiveRedisSignOut}")
private int timeToLiveRedisSignOut;
private final RedisTemplate<String, Object> redisTemplate;
/*
* @since: 06/12/2022
* description: Add access token + accountId to redis after logout or refresh token
* @update:
*/
public void blacklistJwt(String token, String accountId) {
if (!isTokenBlacklisted(token)) {
redisTemplate.opsForValue()
.set(RedisConstant.JWT + token, accountId, timeToLiveRedisSignOut, TimeUnit.HOURS);
}
}
/*
* @since: 06/12/2022
* description: Check exist access token + accountId from redis
* @update:
*/
public boolean isTokenBlacklisted(String token) {
if (Boolean.TRUE.equals(StringUtils.isNullOrEmpty(token))) {
throw new BusinessException(CommonErrorCode.INVALID_TOKEN);
}
return Boolean.TRUE.equals(redisTemplate.hasKey(RedisConstant.JWT + token));
}
/*
* @since: 25/09/2023
* description: get all key using prefix key
* @update:
*/
public Set<String> getKeys(String pattern) {
return redisTemplate.keys(pattern);
}
/*
* @since: 25/09/2023
* description: get value from key
* @update:
*/
public String getValue(String key) {
return (String) redisTemplate.opsForValue().get(key);
}
/*
* @since: 25/09/2023
* description: set key + value redis
* @update:
*/
public void setValue(String key, String val) {
redisTemplate.opsForValue().set(key, val, timeToLiveRedisSignOut, TimeUnit.HOURS);
}
/*
* @since: 25/09/2023
* description: delete key + value redis
* @update:
*/
public void remove(String key) {
redisTemplate.delete(key);
}
}