短信发送验证码代码

package com.ithema.reggie.controller;

import com.ithema.reggie.common.R;
import com.ithema.reggie.entity.User;
import com.ithema.reggie.service.UserService;
import com.ithema.reggie.utils.SMSUtils;
import com.ithema.reggie.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 发送手机短信验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession session){
        //获取手机号
        String phone = user.getPhone();

        if (StringUtils.isNotEmpty(phone)){
            //生成随机的4位验证码
            String code = ValidateCodeUtils.generateValidateCode(4).toString();

            log.info("code= {}",code);
            //调用阿里云提供的短信服务API完成发送短信
           // SMSUtils.sendMessage("瑞吉外卖","",phone,code);

            //需要将生成的验证码保存到Session
            session.setAttribute(phone,code);

            return R.success("手机验证码短信发送成功");
        }

        return R.error("短信发送失败");
    }
}
在Java中发送短信验证码可以通过使用第三方短信服务提供商的API来实现。以下是一个简单的示例代码,演示如何使用阿里云短信服务发送短信验证码: ```java import com.aliyun.dysmsapi20170525.models.SendSmsRequest; import com.aliyun.dysmsapi20170525.models.SendSmsResponse; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teaopenapi.modules.AcsClient; import com.aliyun.teaopenapi.modules.exceptions.ServerException; import com.aliyun.teaopenapi.modules.exceptions.ClientException; public class SmsSender { public static void main(String[] args) { // 配置AK、SK和短信签名等信息 Config config = new Config() .setAccessKeyId("yourAccessKeyId") .setAccessKeySecret("yourAccessKeySecret") .setEndpoint("dysmsapi.aliyuncs.com") .setRegionId("yourRegionId"); // 创建AcsClient实例 AcsClient acsClient = new AcsClient(config); // 构造请求对象 SendSmsRequest request = new SendSmsRequest() .setPhoneNumbers("yourPhoneNumber") .setSignName("yourSignName") .setTemplateCode("yourTemplateCode") .setTemplateParam("{\"code\":\"123456\"}"); try { // 发送短信 SendSmsResponse response = acsClient.sendSms(request); System.out.println("短信发送成功,请求ID:" + response.getRequestId()); } catch (ServerException e) { System.out.println("短信发送失败,错误码:" + e.getErrCode() + ",错误信息:" + e.getErrMsg()); } catch (ClientException e) { System.out.println("短信发送失败,错误码:" + e.getErrCode() + ",错误信息:" + e.getErrMsg()); } } } ``` 请注意,上述代码中的`yourAccessKeyId`、`yourAccessKeySecret`、`yourRegionId`、`yourPhoneNumber`、`yourSignName`和`yourTemplateCode`需要替换为你自己的实际值。其中,`yourAccessKeyId`和`yourAccessKeySecret`是阿里云账号的AccessKey ID和AccessKey Secret,`yourRegionId`是短信服务所在的地域ID,`yourPhoneNumber`是接收验证码的手机号码,`yourSignName`是短信签名,`yourTemplateCode`是短信模板ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值