"Reference blog: http://www.cnblogs.com/hdwpdx/archive/2016/03/29/5333943.html"
Springmvc with interceptors +token prevent duplicate submissions
First of all, to prevent users from repeating the submission there are many ways, the overall divided into the front-end JS limit and back-end restrictions, I personally think the back-end limit is more appropriate (in line with the idea of a better, put the front-end JS Limit repeated submission of the idea).
Have not done before to prevent users to repeat the submission, so direct Baidu a lot of, unexpectedly found can be classified as 2 to 3 really different implementation of the code, although the article has many, but most of the code is almost the same person, the original URL: http://blog.icoolxue.com/ Submitted-by-spring-mvc-to-prevent-data-duplication/, think of so many people with code should be no problem, so the copy copy, the hand built hand-built, finally work, but the test time found a very important problem, Always in duplicate commit state, I have to God, this is very embarrassing, so hurriedly interrupted point, f6f6f6, finally suspected a line of code, paste code:
Private Boolean Isrepeatsubmit (HttpServletRequest request) { string servertoken = (string) request.getsession (True ). getattribute ("token"); if (Servertoken = = null) { return true; } String Clinettoken = Request.getparameter ("token");//This line is NULL, always null. if (Clinettoken = = null) { return true; } if (!servertoken.equals (Clinettoken)) { return true; } return false; }
Hey, I'm just bored, how can I not receive the value? Must be the page has a problem, the page is only a word
<input type= "hidden" name= "token" value= "${token}"/>
I suddenly a flash of the brain, through the value of the name value must be in the form form, I am all Ajax ah, so hurriedly in the transfer parameters with token, the problem will be smooth results!
But I would like to think that so many people copy the original code, and then sent to their own blog, is not there this problem?
Problem solved, by the way to summarize the token to use the method, remember that the code is not a problem, copy should be cautious (change to remember to change!) )
First, customize an annotation:
Package Com.dinfo.interceptor;import Java.lang.annotation.elementtype;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Target (Elementtype.method) @ Retention (retentionpolicy.runtime) public @interface Token { boolean save () default false; Boolean remove () default false;}
Then implement an interceptor excuse:
Package Com.dinfo.interceptor;import Java.lang.reflect.method;import Java.util.uuid;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Org.apache.log4j.logger;import Org.springframework.web.method.handlermethod;import Org.springframework.web.servlet.handler.handlerinterceptoradapter;public class Tokeninterceptor extends Handlerinterceptoradapter {private static final Logger LOG = Logger.getlogger (Token.class); @Override public boolean prehandle (HttpServletRequest request, httpservletresponse response, Object handler) throws Exc eption {if (handler instanceof Handlermethod) {Handlermethod Handlermethod = (handlermethod) handler; Method method = Handlermethod.getmethod (); Token annotation = method.getannotation (Token.class); if (annotation! = null) {Boolean needsavesession = Annotation.save (); if (needsavesession) {request.getsessIon (True). SetAttribute ("token", Uuid.randomuuid (). toString ()); } Boolean needremovesession = Annotation.remove (); if (needremovesession) {if (Isrepeatsubmit (Request)) {Log.warn ("Please don ' t Repeat Submit,url: "+ Request.getservletpath ()); return false; } request.getsession (True). RemoveAttribute ("token"); }} return true; } else {return super.prehandle (request, response, handler); }} Private Boolean isrepeatsubmit (HttpServletRequest request) {String servertoken = (string) request.getses Sion (True). GetAttribute ("token"); if (Servertoken = = null) {return true; } String Clinettoken = Request.getparameter ("token"); if (Clinettoken = = null) {return true; } if (!servertoken.equals (Clinettoken)) { return true; } return false; }}
Finally, the configuration file:
<!--Interceptor Configuration--><mvc:interceptors> <!--Configure token blocker to prevent users from repeating data--and <mvc:interceptor> <mvc:mapping path= "/**"/><!--this place when you have to intercept the path I mean, intercept all url--> <bean . Com.dinfo.interceptor.TokenInterceptor "/><!--class file path to your own interceptor path!! - </mvc:interceptor></mvc:interceptors>
The most important to be sure to ensure that the page and background token to pass the normal!!!
In the controller that needs to generate tokens (usually to click on the submitted page), we've just customized the annotations and posted the code:
@SuppressWarnings ({"Unchecked", "finally", "Rawtypes"}) @RequestMapping ("/savedatacontroller/show") @ Token (save=true) public String saveData (httpservletrequest request,httpservletresponse response,string task_id) { Map map = new HashMap (); System.out.println (task_id); try { map = Savedataservice.querybytaskid (task_id); } catch (Exception e) { e.printstacktrace (); Map.put ("task_id", task_id); Map.put ("Savetype", "" "); Map.put ("Memorycodes", "1"); Map.put ("tablename", "" "); Map.put ("Trowkey", "" "); Map.put ("Columns", "" "); Map.put ("Indextablename", "" "); Map.put ("Irowkey", "" "); Map.put ("Icolumns", ""); } finally { Request.setattribute ("map", map); return "Savedata/index"; } }
As long as this method jumps to the page is randomly generated a token, and then the actual re-commit trigger method to add @token (remove=true), Paste code:
@RequestMapping ("/savedatacontroller/savedata") @ResponseBody @Token (remove=true) public void SaveData (httpservletrequest request,httpservletresponse response, String tablename,string trowkey,string Columns, string indextablename,string irowkey,string icolumns, string task_id,string savetype,string Memorycodes) { System.out.println (task_id); Savedataservice.savedata (Task_id,savetype,memorycodes,tablename, Trowkey, columns, Indextablename, Irowkey, icolumns); }
OK, when you're done here, you can see there's no way to repeat the data! If you have any questions, you can talk to me anytime.
Interceptor Springmvc prevent form repeat commit "1"