Claim:
1, the input phone number, when clicked, transmits the random digital code generated 6, 2 minutes effective
2, enter the verification code, click Verify to return success or failure
3, each phone number can only enter 3 times a day
Import redis.clients.jedis.Jedis; Import java.util.Random; public class ValidationTest { public static void main (String [] args) { // getValidation ( "15,005,076,571"); // checkValidation ( "769 897", "15,005,076,571 "); } static void getValidation (Tel String) { // host port jedis jedis = new new jedis (" myhost ", 6379 ); // password jedis.auth (" mypassword " ); the try { // Get the telephone number PhoneNo = String tel; // I tested with a library jedis.select (1 ); String countKey = PhoneNo + ": COUNT" ; String codeKey = PhoneNo + ": code" ; // get verification specified phone number sent code number String CNT = jedis.get (countKey); // number of times the determination IF (CNT == null ) { // does not send codes jedis.setex (countKey, 60 * 60 * 24, ". 1" ); // send code, hypothesis verification code generated by the StringBuffer code = new newThe StringBuffer (); for ( int I = 0; I <. 6; I ++ ) { code.append ( new new the Random () the nextInt (10. )); } System.out.println ( "code:" + code); // Add cache codes jedis.setex (codeKey, 60 * 2 , code.toString ()); } the else { IF (the Integer.parseInt (CNT) <. 3 ) { // send a verification code, the verification code generated assuming StringBuffer code = new new the StringBuffer (); for ( int I = 0; I <. 6; I ++ ) { code.append ( new new the Random () the nextInt (10. )); } System.out.println ( "code:" + code); // add validation cache code jedis.setex (codeKey, 60 * 2 , code.toString ()); // increment the number of mobile phone to send jedis.incr (countKey); } the else { // return exceeds 3 times, prohibits transmission System.out.println ( " exceeds 3, the transmission prohibition " ); } } }the catch (Exception E) { // here actually need to return rolls down Redis e.printStackTrace (); } the finally { // Close Redis IF (jedis =! null ) { jedis.close (); } } } static void checkValidation (code String, String Tel) { jedis jedis = null ; the try { jedis = new new jedis ( "myhost", 6379 ); // password jedis.auth ( "mypassword"); Jedis.select ( . 1 ); String codeKey = Tel + ": code" ; String Validation = jedis.get (codeKey); IF (Validation == null ) { System.out.println ( "failure authentication code is not transmitted, or " ); } the else { IF (validation.equals (code)) { System.out.println ( " authentication successful " ); } the else { System.out.println ( " authentication failed " ); } } } catch (Exception e) { e.printStackTrace(); } finally { if (jedis != null) { jedis.close(); } } } }