Does jwt auth need to be verified for every request? #5996
chenws1012
started this conversation in
General
Replies: 2 comments 1 reply
-
Any idea about the enhancement after applying such a cache? |
Beta Was this translation helpful? Give feedback.
1 reply
-
Use guava's bloomFilter to avoid repeated verification of tokens to improve performance @Component
@Scope("prototype")
public class CircleBloomFilter {
private CopyOnWriteArrayList<BloomFilter<CharSequence>> filters;
private static final long expectedInsertions = 10000 * 1000L;
private static final double fpp = 0.000001;
public CircleBloomFilter() {
this.filters = new CopyOnWriteArrayList<>();
filters.add(BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8), expectedInsertions, fpp));
doCircle();
}
// cache for 5 minutes
private void doCircle() {
new ScheduledThreadPoolExecutor(1, runnable -> {
Thread thread = new Thread(runnable, "CircleBloomFilter");
thread.setDaemon(true);
return thread;
}).scheduleAtFixedRate(() -> {
this.filters.add(0, BloomFilter.create(Funnels.stringFunnel(StandardCharsets.UTF_8), expectedInsertions, fpp));
if(filters.size() > 5){
this.filters.remove(filters.size() -1);
}
}, 1, 1, TimeUnit.MINUTES);
}
public void put(String key){
this.filters.get(0).put(key);
}
public Boolean exists(String key){
Iterator<BloomFilter<CharSequence>> iterator = this.filters.iterator();
while ( iterator.hasNext() ){
if (iterator.next().mightContain(key)){
return true;
}
}
return false;
}
} Fake code if (passedCircleBloomFilter.exists(token)){
//pass
return chain.filter(request, response);
}else{
//do check
if ( checkToken(token) ){
//pass
passedCircleBloomFilter.put(token);
return chain.filter(request, response);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I think verifying every request will affect performance. may be cache the verified jwt for a period of time, such as not repeating the verification within 5 minutes.
Beta Was this translation helpful? Give feedback.
All reactions