SMS Verification Login
1, click Trigger, call the phone number for the parameter to send the authentication login SMS method
2. The default template is the validation template
Generate 6-Bit verification code
3, the generated verification code and mobile phone number into the cache, (has set the cache storage time)
4. Call the Send template SMS method to send SMS (set the time of validity of the verification code in the SMS)
5, click Trigger Login, call the corresponding authentication login function, with the phone number and verification code as parameters
6, check the cache of the corresponding reserved information
If consistent, landing success;
Login unsuccessful is the reason for the return (1, timeout 2, captcha input error)
Code implementation:
/**
* Send verification Code SMS
* Parameters: Mobile phone number
*/
public void Sendverifyloginsms (String to) {
Jedis cache = Sendsmscache.getresource ();
Generate six-bit verification code
String charvalue = "";
for (int i = 0; i < 6; i++) {
char C = (char) (Randomint (0, 9) + ' 0 ');
Charvalue + = string.valueof (c);
}
The generated six-bit verification code and incoming mobile phone number into the cache, time 90S
try{
Pipeline Pipeline = cache.pipelined ();
Pipeline.set ("CACHE" + to, charvalue);
Pipeline.expire ("CACHE", 90);
Pipeline.sync ();
}
Finally
{
if (cache! = null)
{
Cache.close ();
}
}
Verification code and display time
String[] Datas = {charvalue, "1.5"};
SMS Templates
String TemplateID = "1";
Smsclientbiz.sendsms (To, TemplateID, datas);
}
/**
* Generate random numbers
*
* */
public int Randomint (int. from, int. to) {
Random r = new Random ();
Return from + r.nextint (to-from);
}
/**
* Verify SMS Verification code Login
*
* */
public boolean verifysms (string to, string Verifycode) {
Jedis cache = Sendsmscache.getresource ();
Authentication code in cache
String Cacheverifycode;
try{
Cacheverifycode = Cache.get ("cache" + to);
}
Finally
{
if (cache! = null)
{
Cache.close ();
}
}
If the ransom comes with a verification code that matches the code in the cache, the validation succeeds
if (Verifycode ==cacheverifycode) {
return true;
}else
return false;
}
Java SMS Authentication Login