Whether it's spring MVC or struts, you can add interceptors before the controller or Aciton executes.
With the logic control in the interceptor, the access frequency limit can be achieved.
First construct the access frequency data class
classFrequencydata {//using Ip_methodnameString Key; //Record start time LongStartTime; //Record End Time LongEndTime; //Access Frequency limit length of time intTime ; //frequency of access limit times intlimit; //record access point in timeList<long> accesspoints =NewArraylist<long>(); Public voidResetLongTime ) {StartTime= EndTime =Time ; Accesspoints.clear (); Accesspoints.add (time); }}
In the Spring MVC interceptor, you can get the method name to intercept and get the IP address of the client via request.
@Override Public Boolean throws Exception { (Handlermethod) handler). GetMethod (). GetName (); ((Handlermethod) handler). Getbean (). GetClass (). GetName ();
Request.getremoteaddr () ...
Create a map data in the Interceptor class that holds each client access record
Private Static Map<string, frequencydata> freqdatas new hashmap<string, frequencydata> (max_size);
The access frequency limit execution process is as follows:
1, calculates the key value, namely is Ip_methodname, obtains the corresponding frequencydata.
2, if the Frequencydata data does not exist, new and saved to Freqdatas.
3. The time and limit in the Frequencydata data can be fixed to write dead, can also be obtained by annotations (add custom annotations on the called method, specify two parameters in the note).
4, add a record in the accesspoints of Frequencydata data, and update Endtime time.
5. Calculate whether the endtime-starttime is greater than the time value. If it is greater than, perform frequencydata reset ().
6, calculates whether accesspoints.size () is greater than the limit value. If it is less than, continue to execute the calling method, or , if it is greater than, do not proceed with the call .
At this point, a simple access frequency control function can be completed.
Web system access Frequency limits