Created
January 30, 2022 20:07
-
-
Save GaetanoPiazzolla/a96d5aa8cdc6d509e6f136ecf2eff3e7 to your computer and use it in GitHub Desktop.
RedisCacheConfiguration with TTL for the blacklist cache.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@AutoConfigureAfter(RedisAutoConfiguration.class) | |
public class CacheConfig { | |
public final static String BLACKLIST_CACHE_NAME = "jwt-black-list"; | |
@Value("${jwt.duration:0}") | |
private int tokenDuration; | |
@Value("${spring.redis.host:localhost}") | |
private String redisHost; | |
@Value("${spring.redis.port:7001}") | |
private int redisPort; | |
@Bean | |
public LettuceConnectionFactory redisConnectionFactory() { | |
return new LettuceConnectionFactory(new RedisStandaloneConfiguration(redisHost, redisPort)); | |
} | |
@Bean | |
RedisCacheManagerBuilderCustomizer redisCacheManagerBuilderCustomizer() { | |
return (builder) -> { | |
Map<String, RedisCacheConfiguration> configurationMap = new HashMap<>(); | |
configurationMap.put(BLACKLIST_CACHE_NAME, RedisCacheConfiguration.defaultCacheConfig().entryTtl( | |
Duration.ofSeconds(tokenDuration))); | |
builder.withInitialCacheConfigurations(configurationMap); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Related to medium article: https://blog.devgenius.io/fixing-jwt-insecure-session-termination-by-blacklisting-tokens-36d783adfd67