阿里云服务 发送短信验证码

Posted Ideas!

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云服务 发送短信验证码相关的知识,希望对你有一定的参考价值。

Controller

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import com.alibaba.fastjson.JSONObject;
	/**
     * 发送短信验证码
     * @param map 接收手机号码
     */
    @PostMapping ("/sendSms")
    public HttpResult sendSms(HttpServletRequest request,@RequestBody Map<String, Object> map) {
        try {
            //发送短信
            JSONObject teljson = null;
            String code = String.valueOf(new Random().nextInt(899999) + 100000);
            System.out.println("code::"+code);
            String phone = (String)map.get("phone");
            SendSms sendSmes = new SendSms();
            CommonResponse sendSmsResponse = sendSmes.sendSms(phone,code);
            if(sendSmsResponse != null && sendSmsResponse.getData().endsWith("\\"Code\\":\\"OK\\"}")) {

                redisUtils.set(phone, code, 5l, TimeUnit.MINUTES);
                return HttpResult.ok("验证码发送成功");
            }else {
                Map<String,String> resultMap = new HashMap<String,String>();
                return HttpResult.error("fail");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
    /**
     * 手机号验证码登录接口
     */
    @PostMapping(value = "/smsLogin")
    public HttpResult smsLogin(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        String mobileNumber = (String) map.get("phone");
        if(mobileNumber == null) {
            return HttpResult.error("手机号不能为空!");
        }
        String verifyCode = (String) map.get("code");
//        String uuid = (String) map.get("uuid");
//        Integer codeType = (Integer) map.get("codeType");
        String code = (String) redisUtils.get(mobileNumber);
        if(code == null) {
            return HttpResult.error("请获取验证码!");
        }
        else {
            if (!code.equals(verifyCode) ) {
                return HttpResult.error("验证码错误!");
            }
            if(codeType != SysConstants.CODE_TYPE_LOGIN){
                return HttpResult.error("验证码错误!");
            }
            SysUser user = sysUserService.getUserByField("phone", mobileNumber);
            // 账号不存在、密码错误
            if (user == null) {
                return HttpResult.error("账号不存在!");
            }
            // 账号锁定
            if (user.getStatus() == 0) {
                return HttpResult.error("账号已被锁定,请联系管理员!");
            }
            // 系统登录认证
            JwtAuthenticatioToken token1 = SecurityUtil.login(request, user.getUsername(), user.getPassword(), authenticationManager);
            // 记录登录日志
            //sysLoginLogService.writeLoginLog(user.getUser_name(), IPUtils.getIpAddr(request));

            return HttpResult.ok(token1);
        }
    }

SendSms

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
/**
 * @Description: 短信验证码发送
 */
public class SendSms {
    public static CommonResponse sendSms(String telephone,String code) throws ClientException {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GFbgiZdVxBzmqJ69Xts", "F90yx8QOYkmxQymQjJEwoNiS0xiEwp");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", "朗斯雅特");
        request.putQueryParameter("TemplateCode", "SMS_211720321");
        request.putQueryParameter("TemplateParam", "{\\"code\\":\\"" + code + "\\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response;
        } catch (ServerException e) {
            e.printStackTrace();
            return  null;
        } catch (ClientException e) {
            e.printStackTrace();
            return  null;
        }
    }
}

以上是关于阿里云服务 发送短信验证码的主要内容,如果未能解决你的问题,请参考以下文章

go 发送短信研究

阿里云服务 发送短信验证码

新版阿里云短信服务之发送验证码接口快速实现

新版阿里云短信服务之发送验证码接口快速实现

django之集成阿里云通信(发送手机短信验证码)

JAVA实现《阿里云发送短信验证码以及短信通知》