码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2021-05-24 07:14:01      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:iot   hash   mapping   etc   main   sap   value   getc   nts   

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;
        }
    }
}

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

标签:iot   hash   mapping   etc   main   sap   value   getc   nts   

原文地址:https://www.cnblogs.com/ideaAI/p/14762311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!