发送短信验证码登录

这是前台代码

<!DOCTYPE html>

<html>

    <head>

        <meta charset="UTF-8">

        <meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,minimum-scale=1.0, ser-scalable=no">

        <link rel="stylesheet" type="text/css" href="css/reset.css"/>

        <script type="text/javascript" src="js/jquery-3.2.1.js"></script>

        <title></title>

        <style type="text/css">

            html { background: #efefef; }

            p { position: relative;height: 1rem;width: 90%; margin: 0 auto;border-bottom: 1px solid lightgray; }

            p input { outline: none;text-indent: 0.4rem;font-size: 0.3rem;height: 100%;width: 100%;background: rgba(0,0,0,0); }

            p span { display: inline-block;font-size: 0.3rem;color: gray;position: absolute;top: 0.35rem;right: 0.3rem; }

            div { width: 80%;height: 1rem;line-height: 1rem;text-align: center;background: #D81E06;color: #fff;font-size: 0.3rem;margin: 0.5rem auto 0;border-radius: 0.1rem; }

            i { width: 100%;text-align: center;display: inline-block;color: gray;margin-top: 0.4rem; }

            i wet { color: blue; }

        </style>

    </head>

    <body>

        <form id="form1">

        <p style="padding-top: 2rem;">

            <input type="number" value=""  id="phone" name="phone" placeholder="请输入手机号" />

        </p>

        <p>

            <input type="number" id="code"  name="code" placeholder="请输入验证码" />

            <span id="sendSms">获取验证码</span>

        </p>

        <div id="login">登录</div>

        <i>点击登录,即表示已阅读并同意<wet>《车友援服务协议》</wet></i>

        </form>

     

    </body>

    

    <script>

         $(function() {

            $("#sendSms").click(function() {

                $.ajax({

                    type: 'get',

                    url: 'http://localhost:8080/api/customer/sengCodeNum',

                    data: {

                        "phoneNum":$("#phone").val()

                    },

                    dataType: 'json',

                    success: function(res) {

                        if (res.code == 0) {

                            alert("验证码已发送到手机")

                            //这里写作业2

                        }else{

                            alert("发送失败")

                        }

                    },

                })

验证码会发送到redis库中

            })

         

            $("#login").click(function() {

                $.ajax({

                    type: 'post',

                    url: 'http://localhost:8080/api/customer/customerLogin',

                    data: {

                        "phoneNum":$("#phone").val(),

                        "codeNum":$("#code").val()

                    },

                    dataType: 'json',

                    success: function(res) {

                        if (res.code == 0) {

                            alert("登录成功")

                        }else{

                            alert("验证码不对")

                        }

                    },

                })

            })

        });

    </script>

</html>

这是后台的发送验证码和登录

@RestController
@CrossOrigin(origins = "*")//注意: *是来自于所有的域名请求
@RequestMapping("/api/customer")
public class CustomerController{
@Autowired(required = false)
private CustomerService customerService;

@Autowired
private JedisPool jedisPool;
        //发送验证码
    @RequestMapping("/sengCodeNum")
    public Map sendCodeNum(String phoneNum) {
        Map map = new HashMap();
        //1.在发送验证码之前,随机撞见一个6位数的验证码
        int randomNum = new Random().nextInt(999999);//0-999999
        if (randomNum < 100000) {
            randomNum = randomNum + 100000;
        }
        //1.1在发送验证码之前 需要向redis中查询该手机 有没有验证码存在,如果存在,就直接从redis中读取,而不从阿里云发送,可以节省成本
        //查询pcode这个key在不在
        Boolean b = jedisPool.getResource().exists("phoneNum");
        if (b) {
            map.put("code", 0);
            map.put("msg", "验证啊已存在,情趣短信中查询");
            return map;
        } else {
            //2.接受到前端传过来的手机号:phoneNum  对其调用 阿里云的发送短信的工具类,去发送验证码
            ALSMSUtil.sendMsg(phoneNum, randomNum);
            //3.再送之后,将手机号当做redis key,验证码当做redis value 存入到redis数据库中
            String setex = jedisPool.getResource().setex(phoneNum, 120, String.valueOf(randomNum));
            System.out.println("setex = " + setex);
            jedisPool.getResource().persist(phoneNum);//注意:这里设置成-1 在线上环境测试要删除
            //4.将验证码发送给前端
            if ("OK".equals(setex)) {
                map.put("code", 0);
                map.put("msg", "发送成功");
                //map.put("data",randomNum);//线上不能把验证码发送前端,容易被人利用
                return map;
            } else {
                map.put("code", 500);
                map.put("msg", "发送失败");
                return map;
            }
        }
    }

    //校验手机号和验证码
    @RequestMapping("/customerLogin") //  /api/customer/customerLogin
    public Map customerLogin(String phoneNum,String codeNum){
        Map map = new HashMap();
        //1.根据前端传来的手机号和验证码来和redis中的数据做对比
        String redisCodeNum = jedisPool.getResource().get(phoneNum);//redis中的验证码
        if(codeNum.equals(redisCodeNum)){
            map.put("code",0);
            map.put("msg","发送成功");
            return map;
        }else{
            map.put("code",500);
            map.put("msg","发送失败");
            return map;
        }
    }

}

  • 3
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
发送短信验证码可以使用阿里云的短信服务,以下是使用Spring Boot集成阿里云短信服务发送短信验证码的简单步骤: 1. 在阿里云控制台开通短信服务并获取AccessKey和AccessSecret。 2. 引入阿里云短信服务SDK依赖,例如: ``` <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.3</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dysmsapi</artifactId> <version>1.0.0</version> </dependency> ``` 3. 编写发送短信验证码的代码,例如: ``` @RestController public class SmsController { @Autowired private AliyunSmsConfig aliyunSmsConfig; // 阿里云短信服务配置 @PostMapping("/sms/send") public String sendSms(@RequestParam String mobile) throws ClientException { // 生成6位随机验证码 String code = String.valueOf((int)((Math.random()*9+1)*100000)); // 发送短信验证码 DefaultProfile profile = DefaultProfile.getProfile("default", aliyunSmsConfig.getAccessKey(), aliyunSmsConfig.getAccessSecret()); 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("PhoneNumbers", mobile); request.putQueryParameter("SignName", aliyunSmsConfig.getSignName()); request.putQueryParameter("TemplateCode", aliyunSmsConfig.getTemplateCode()); request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}"); CommonResponse response = client.getCommonResponse(request); // 输出发送结果 System.out.println(response.getData()); return code; } } ``` 其中,`AliyunSmsConfig`是阿里云短信服务的配置类,需要配置AccessKey、AccessSecret、短信签名和短信模板编号等信息。 4. 在前端页面中添加发送短信验证码的按钮,并调用发送短信验证码的接口。 以上就是使用Spring Boot集成阿里云短信服务发送短信验证码的简单步骤,希望对你有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值