Java SMS Authentication Login

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.