SpringBoot实现短信验证码发送功能

1.pom.xml文件需要导入的依赖:

<!--阿里云短信服务-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.0</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.61</version>
        </dependency>

2.SmsService类

package ydj.plugins.pojo;
public interface SmsService {
    boolean sendSms(String phoneNumber); // 接收验证码手机号
}

3.SmsServiceImpl 实现类

package ydj.plugins.pojo;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Random;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService{
	
	//todo 此处四个参数也可以在 application.yml 中配置
    private String accessKeyId ="LTAISdYBrOBuwGSp"; //todo key
    private String accessSecret = "VdFEqmy88uesZEQPjj8BhB8LodAEeX"; //todo 密钥
    private String signName = "网星商城"; //todo 签名名称
    private String templateCode = "SMS_161360134"; //todo 模板

    @Override
    public boolean sendSms(String phoneNumber) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-xian", accessKeyId, accessSecret);
        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-xian");
        request.putQueryParameter("PhoneNumbers", "18562224222");
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        JSONObject object=new JSONObject();
        String randCode=getRandCode(6);
        log.info("验证码为:{}",randCode);
        System.out.println("验证码为:"+randCode);
        object.put("code",randCode);
        request.putQueryParameter("TemplateParam", object.toJSONString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info(response.getData());
            return true;
        } catch (Exception e) {
            log.error("{}",e);
        }
        return false;
    }

    //todo 生成随机验证码
    public static String getRandCode(int digits) {
        StringBuilder sBuilder = new StringBuilder();
        Random rd = new Random((new Date()).getTime());
        for(int i = 0; i < digits; ++i) {
            sBuilder.append((rd.nextInt(9)));
        }
        return sBuilder.toString();
    }
}

上面必须传递的四个参数也可以在 application.yml 中配置

在这里插入图片描述

 4.SmsController 控制器类

package ydj.plugins.pojo;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ydj.commons.pojo.CommonResultPojo;
import javax.annotation.Resource;

@RestController
@RequestMapping("/smsController")
public class SmsController {

    @Resource
    private SmsService smsService;

    @GetMapping("/sendSms")
    public CommonResultPojo sendSms(@Param(value = "userName") String userName){
        boolean i=smsService.sendSms(userName); //todo 接收验证码手机号
        if(i == true){
            return new CommonResultPojo(200,"发送成功",i);
        }else {
            return new CommonResultPojo(444,"发送失败",i);
        }
    }
}

postman测试:

在这里插入图片描述

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值