spring-boot/spring-mvc aliyun 短信发送验证码

直奔主题:

maven地址:

<!--aliyun 短信发送-->
<dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <version>3.7.0</version>
</dependency>
<dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-ecs</artifactId>
   <version>4.11.0</version>
</dependency>

util 类:

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;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;

import java.io.IOException;
import java.util.Properties;

/*
pom.xml
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>aliyun-java-sdk-core</artifactId>
  <version>4.0.3</version>
</dependency>
*/
public class SendSms {
    public static String RunSend(String Telstr,String Codestr) {
        ResourceLoader loader = new DefaultResourceLoader();
        Resource resource = loader.getResource("classpath:oss.properties");
        Properties ossProps = new Properties();
        try {
            ossProps.load(resource.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        String accessKeyId = ossProps.getProperty("oss.access_key_id");
        String accessKeySecret = ossProps.getProperty("oss.access_key_secret");

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", Telstr);
        request.putQueryParameter("SignName", "阿里云短信测试专用");
        request.putQueryParameter("TemplateCode", "SMS_109685164");
        request.putQueryParameter("TemplateParam", "{code:"+Codestr+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return "";
    }
}

java接口 验证码发送后需要存起来验证 这里存的是 数据库:

/**
     * 登录时发送验证码
     *
     * @param mbMob
     * @param session
     * @return
     */
    @RequestMapping("/loginSendCode")
    @ResponseBody
    public Map<String, Object> loginSendCode(String mbMob) {
        //6位短信验证码
        int number = (int) ((Math.random() * 9 + 1) * 100000);
        int valnum = (int) ((Math.random() * 9 + 1) * 10);

        //手机号是否存在
        Member meb = iMemberService.ValTel(mbMob);
        if (meb == null) {
            return resultMap(Type.error, "手机号不存在,请注册!", "", null, "", "404");
        }
        Member valmb = new Member();
        valmb.setMemberMob(mbMob);
        List<Member> meblist = iMemberService.selectMemberList(valmb);
        if (StringUtils.isEmpty(meblist)) {
            return resultMap(Type.error, "手机号不存在,请注册!", "", null, "", "405");
        } else {
            //如果存在update
            valmb.setId(meblist.get(0).getId());
            valmb.setMemberPin(String.valueOf(number));
            valmb.setMemberLot(Integer.valueOf(valnum));
            iMemberService.updateMember(valmb);
        }

        //发送验证码
        SendSms.RunSend(mbMob, String.valueOf(number));
        return resultMap(Type.success, "验证码已发送,请注意查收", "", valmb.getMemberLot(), "", "101");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhopq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值