Public String Generatetoken () throws Exception { String token = system.currenttimemillis () + new Random (). Nextint () + ""; MessageDigest MD = new MessageDigest ("MD5");//Can get digest fingerprint of data, length consistent byte[] MD5 = Md.digest (Token.getbytes ());// The length is uniformly 16 bytes Base64encoder encoder = new Base64encoder ();//You can convert any byte array to a normal string return Encoder.encode (MD5);}
Base64encoder is not found in the API documentation for JSE, it is not an API that is officially released by Sun, and can be viewed after use in eclipse
The Base64encoder encoding automatically converts 3 bytes, or 24 bits of binary code, into 4 bytes or 32 bits of binary code, it divides the original 24 bits by 6 bits per copy, and then the 4 bits in front of each copy 2, so the data range for each byte generated by the encoding is: 0- 00111111 is 0 to 63, a total of 64 data, each corresponding to a unique data information.
Can be used to prevent a form from repeating a commit:
When a user accesses a page, the server randomly generates an identifier token that is saved to the session and is written back to the form as a hidden element, and the token attribute in the session is deleted after the form is processed
After the user submits the form, the server authenticates:
1, whether there is a token element, there is the next step, do not process the form
2, if there is a token attribute in the session, proceed to the next step, do not process the form
3, the token attribute value in the session is the same as the user commits, the next step is the same, the form is not processed differently
4, process the form, and remove the token attribute from the session
This is the server side to prevent the form of repeated submission method, the client to be controlled by the JS code.
Fixed-length random number generator