栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【springboot手机验证码案例】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【springboot手机验证码案例】

提示:以下是本篇文章正文内容,下面案例可供参考

一、准备事项(以下为目录结构)

注意:config可以不用新建,里面配置类可有可无

 

二、使用步骤 1.引入库

pom.xml依赖:

  
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.projectlombok
            lombok
        




        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
            com.alibaba
            druid
            1.1.16
        
    

Controller代码如下(示例):

package com.example.controller;

import com.example.Service.SMSCService;
import com.example.dao.SMSCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sms")
public class SMSCodeController {

@Autowired
private SMSCService smscService;

@ApiOperation("发验证码")
@GetMapping
    public String getCode(String tele){
        String code = smscService.sendCodeToSMS(tele);
    System.out.println(code);
        return  code;


    }
    @PostMapping("/checkCode")
    public boolean checkCode(SMSCode smsCode){
        return  smscService.checkCode(smsCode);
    }
    @ApiOperation("HELLO")
    @RequestMapping("/hello")
    public String hello(){
    return "hello";
    }
}

dao层代码

package com.example.dao;

import lombok.Data;

@Data
public class SMSCode {

private  String tele;
private String  code;
}

Service接口代码:

package com.example.Service;

import com.example.dao.SMSCode;


public interface SMSCService {
   String  sendCodeToSMS(String tele);
   boolean checkCode(SMSCode smsCode);


}

ServiceImpl接口实现类代码:

package com.example.Service.impl;

import com.example.Service.SMSCService;
import com.example.dao.SMSCode;
import com.example.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.stereotype.Service;

@Service
public class SMSCServiceImpl implements SMSCService {

    @Autowired
   private CodeUtils codeUtils;
    @Override
    @CachePut(value = "smscode",key = "#tele")
    public String sendCodeToSMS(String tele) {
        String c = codeUtils.GenerCode(tele);
        return c;
    }

    @Override
    public boolean checkCode(SMSCode smsCode) {
        return false;
    }
}
CodeUtils代码:
package com.example.utils;

import org.springframework.stereotype.Component;

@Component
public class CodeUtils {
    private String[] num={"00000","0000","000","00","0",""};

    public String GenerCode(String number){
        int hash = number.hashCode();
        int encry=12464374;
        long result = hash ^ encry;
        long nowTime=System.currentTimeMillis();
        result=result^nowTime;
        long code = result % 10000;
        code= code<0 ?-code:code;
        String resultStr=code+"";
        int length = resultStr.length();

     return num[length]+resultStr;



    }

}

Application代码:

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

@SpringBootApplication

@EnableCaching
public class PhonenumberApplication {

    public static void main(String[] args) {
        SpringApplication.run(PhonenumberApplication.class, args);
    }

}

2.测试

浏览器输入:localhost:8080/sms?tele=123456789 

注意:123456789为测试数据

3.结果


总结

当作一个小玩意玩玩,原链接为B站视频第109集 :

黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)_哔哩哔哩_bilibili

如果想要源码的小伙伴吗,可以私信我,免费白嫖的哦; 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/855508.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号