-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathTokenDenylist.java
More file actions
40 lines (34 loc) · 1.28 KB
/
TokenDenylist.java
File metadata and controls
40 lines (34 loc) · 1.28 KB
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
package com.iemr.tm.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class TokenDenylist {
private static final String PREFIX = "denied_";
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public void addTokenToDenylist(String jti, Long expirationTime) {
if (jti != null && expirationTime != null && expirationTime > 0) {
try {
String key = PREFIX + jti;
redisTemplate.opsForValue().set(key, true);
redisTemplate.expire(key, expirationTime, TimeUnit.MILLISECONDS);
} catch (Exception e) {
throw new RuntimeException("Failed to add token to denylist", e);
}
}
}
public boolean isTokenDenylisted(String jti) {
if (jti == null) {
return false;
}
try {
Object value = redisTemplate.opsForValue().get(PREFIX + jti);
return value != null;
} catch (Exception e) {
// If Redis is down, assume token is valid to prevent service disruption
return false;
}
}
}