SpringBoot整合阿里云短信

一、前言

短信验证码被广泛应用在网站、app应用中的用户注册、密码找回等场景,那么如何实现发送短信验证码?下面以java开发语言为例,秒赛平台小编为大家分享想调用短信接口的代码。

二、快速开始

2.1、pom.xml依赖引入

        <!--短信提醒-->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.1.1</version>
        </dependency>

2.2、短信工具类

import com.alibaba.fastjson.JSONObject;
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.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author wno5974
 * @create 2023-06-13 12:14
 * @Deprecated 短信配置
 */
public class MsmUtil {
    /**
     * 发送短信
     * @param phone
     * @param code
     * @param profile
     * @return
     */
    public static boolean send(String phone, String code,DefaultProfile profile) {
        //判断手机号是否为空
        if(StringUtils.isEmpty(phone)) {
            return false;
        }
        //整合阿里云短信服务
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("xxx.xxx.xxx");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");

        //手机号
        request.putQueryParameter("PhoneNumbers", phone);
        //签名名称
        request.putQueryParameter("SignName", "XXXXX系统验证码");
        //模板code
        request.putQueryParameter("TemplateCode", "SMS_XXXXXXXXX");
        //验证码  使用json格式   {"code":"123456"}
        Map<String,Object> param = new HashMap();
        param.put("code",code);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));

        //调用方法进行短信发送
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}

三、短息发送测试

Random randObj = new Random();
    RedisTemplate redisTemplate = ApplicationContextHolder.getBean("redisTemplate");
    // 生成6位随机验证码
    public String generateCode() {
        return Integer.toString(100000 + randObj.nextInt(900000));
    }
    @Resource
    private DefaultProfile defaultProfile;
    @PreventSubmit(time = 6000)
    @RequestMapping(value = "verificationCode",method = RequestMethod.POST)
    public Result getVerificationCode(@RequestBody Object obj){
        Map<String, Object> map = (Map<String, Object>) obj;
        String phone = map.get("phone").toString();
        String code = generateCode();
        SysUser sysUser = sysUserService.getLoginUserByPhone(phone);
        if(sysUser==null){
            map.put("msgCode","请输入有效的手机号");
            return Result.ok(map);
        }
        boolean send = MsmUtil.send(phone,code,defaultProfile);
//        boolean send = true;
        if(send){
            redisTemplate.opsForValue().set("msgCode:"+phone,code, Duration.ofMinutes(1));
            map.put("msgCode","验证码发送成功");
            return Result.ok(map);
        }else{
            return Result.fail();
        }
    }

posted on 2026-08-10 15:35  爱河  阅读(2)  评论(0)    收藏  举报

导航