Ali Cloud SMS verification

First, the opening of Ali cloud messaging service, and signed application templates, remember accessKeyId and accessKeySecret, the specific operation may be found to

Second, click on the SMS services help document, click on the old version of the Developer's Guide, (the new version is not a trial), download java version, visit dysms_java \ java \ api_demo \ alicom-dysms-api \ src \ main \ java \ com \ alicom \ dysms \ api

The smsdemo File, Import dysms_java \ java \ api_demo \ alicom-dysms-api \ libs package two, and then you can follow the comments in the demo modify access, signature, templates,

SendSms two parameters passed in process, respectively, the phone number and the verification code (to take a random number)

Third, create a msgutil tools, can be directly sendSms method and the method of random numbers that you have created in class. Use service codes generally used ajax, in the controller layer

@RequestMapping("sendCode")
    @ResponseBody
    public Map<String, Object> sendCode(String phone, HttpSession session) {
        String code = MsgUtil.randCode();
        Map<String, Object> result = new HashMap<>();
        try {
            MsgUtil.sendSms(phone, code);
            session.setAttribute("code", code);
            result.put("statu", 200);
            result.put("msg", "短信发送成功");
        } catch (ClientException e) {
            result.put("statu", 500 ); 
            result.put ( "MSG", "SMS failed" ); 
        } 
        return Result; 
        
    }

Sendsms method call, while the code stored in the session may be returned by way of map

When you log in or register, enter the verification code session in comparison to the verification code. (Provided there is no expiration date codes)

 

Guess you like

Origin www.cnblogs.com/psxfd4/p/11578442.html