Environment:
Java-EE based web application
Problem:
Need to restrict user to make more than 5(for example) request within same Second (BOTs mainly)
Solution :
As a basic design I am planning to have 2 synchronized Map
in application scope
Map<String, Map<Long, Integer>>
String
is for sessionId of request
Long
is for current second representation
Integer
is to hold request count
Process:
Step 0:
Configuring a Filter
to intercept each request
Step 1:
determine the map
I will see if current minute
is odd then I will add data on mapOne
and I will clear the mapTwo
Step 2:
process map
int requestNoForThisSecond = mapXX.get(request.getSession().getId()).get(currentSecondRepresantationInLong);
if(requestNoForThisSecond <= 5){
requestNoForThisSecond++;
mapXX.get(request.getSession().getId()).put(currentSecondRepresantationInLong, requestNoForThisSecond);
}else{
response.sendRedirect();// redirect to some captcha page
}
Step 4:
also remove the session entry if session expires / user logs out
This is very basic design for the problem
Any one of you have any better idea/suggestion ?