SpringBoot项目集成短信服务实现登录

Springboot集成阿里云短信服务实现登录

    • 一.阿里云官网的操作步骤
    • 二. Idea的操作步骤

一.阿里云官网的操作步骤

1.进入阿里云官网(https://www.aliyun.com/)

2.在搜索框输入短信服务
SpringBoot项目集成短信服务实现登录_第1张图片
3.点击短信服务后,之后点击管理控制台
SpringBoot项目集成短信服务实现登录_第2张图片
4.进入短信服务主页后,点击AccessKey后,点击创建AccessKey,之后保存好对应的Id 和secret
SpringBoot项目集成短信服务实现登录_第3张图片
5.点击国内消息,创建对应的签名(企业的名称)和模板(短息内容)
SpringBoot项目集成短信服务实现登录_第4张图片
6.将签名名称和模板中的模板Code记住,后续要用。

7.可根据短信数量购买对应套餐,短信数量少进行测试的话可在账户内充值少量金额即可,否则在调用时会出现账户余额不足。

二. Idea的操作步骤

1.添加依赖

<!--阿里云短信包-->
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-core</artifactId>
			<version>4.4.0</version>
		</dependency>
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
			<version>1.0.0</version>
		</dependency>

2.创建发送短信的工具类


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.stereotype.Component;
@Component
public class Message {



    public static void messagePost(String mobile, String message){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "***AccessKey ID***", "***AccessKey Secret***");//自行添加
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers",mobile);
        request.putQueryParameter("SignName", "***签名名称***");//自行添加
        request.putQueryParameter("TemplateCode", "***模板Code***");//自行添加
        request.putQueryParameter("TemplateParam", "{\"code\":"+message+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

注:可将mobile修改为需要测试的手机号,运行此类即可收到验证码

3.在服务器里安装redis,之前的创作里已经写过,可进行参考

4.在之前的秒杀系统的Userkey中添加

public static UserKey getBymobile = new UserKey("mobile");

5.在controller中LoginController类里添加对应的代码,实现发送短信,并将对应的验证码存入到redis缓冲里去`

public Result<Boolean> authcode_get(LoginVo loginVo) {
        String mobile=loginVo.getMobile();
        String password = "1" + RandomStringUtils.randomNumeric(5);//生成随机数,我发现生成5位随机数时,如果开头为0,发送的短信只有4位,这里开头加个1,保证短信的正确性
        redisService.set(UserKey.getBymobile,mobile, password);//将验证码存入缓存
        Message.messagePost(mobile, password);//发送短息
        return Result.success(true);
    }
//获取前端输入的手机号和验证码,将验证码与redis缓冲的进行对比
  public Result<Boolean>  authcode_login(LoginVo loginVo) {
        String mobile=loginVo.getMobile();
        String password=loginVo.getPassword();
        String s1=redisService.get(UserKey.getBymobile,mobile,String.class);
        System.out.println(s1);
        if (s1.equals(password)) {
            return Result.success(true);
        } else {
            return Result.error(CodeMsg.PASSWORD_ERROR);
        }

    }

6.页面效果如下
SpringBoot项目集成短信服务实现登录_第5张图片
7.redis里的效果
SpringBoot项目集成短信服务实现登录_第6张图片

你可能感兴趣的:(秒杀系统)