Springboot集合阿里云短息服务登录

第一步:添加maven依赖

<!--阿里云短信包-->
		<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>

第二步:发送短信工具类

这里有一些要自己修改的数据,我在代码中已标出



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 com.example.demo.redis.RedisService;
import com.example.demo.redis.UserKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
public class SendNoteUtil {
    @Autowired
    private RedisService redisService;

    //验证平台信息 开发者无需任何更改
    private static final String dysmsapi = "dysmsapi.aliyuncs.com";

    DefaultProfile profile = DefaultProfile.getProfile("default", , "你的accessKeyId ", "你得accessKeySecret");
        IAcsClient client = new DefaultAcsClient(profile);
    //这一步的两个参数,一个是要发送验证码的手机号 一个是模板Code用来区分登录注册  模板code要用自己的
    public String sendNoteMessgae(String PhoneNumbers,String template){
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for(int i=0;i<6;i++){
            sb.append(random.nextInt(10));
        }
        CommonRequest request = new CommonRequest();
        //request.setSysProtocol(ProtocolType.HTTPS);
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(dysmsapi);
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("PhoneNumbers", PhoneNumbers);//接受验证码的手机号
        request.putQueryParameter("SignName", "公式名称");//签名 工资名称要用自己的签名的公司名称
        //模板代码,用自己的
        request.putQueryParameter("TemplateCode", template);
        //用户定义的验证码内容
        request.putQueryParameter("TemplateParam","{code:"+sb.toString()+"}");
        //验证码绑定手机号并存储到redis
        UserKey userKey = new UserKey("oo");

        redisService.set(userKey,PhoneNumbers,"00");
        try {
            CommonResponse response = client.getCommonResponse(request);
            String returnStr = response.getData();
            System.out.println(returnStr);
            JSONObject jsonObject = JSONObject.parseObject(returnStr);
            //返回的信息
            return jsonObject.getString("Message");
        } catch (ServerException e) {
            return e.getErrMsg();
        } catch (ClientException e) {
            return e.getErrMsg();
        }
    };

}


第三步:发送短信controller类

模板code写自己的


import com.example.demo.pojo.LoginVo;
import com.example.demo.redis.RedisService;
import com.example.demo.redis.UserKey;
import com.example.demo.result.Result;
import com.example.demo.service.MiaoshaUserService;
import com.example.demo.util.SendNoteUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping(value = "/api/note")
public class SendNoteController {

    @Autowired
    private SendNoteUtil sendNoteUtil;

    @Autowired
    private RedisService redisService;

    @Autowired
    MiaoshaUserService miaoshaUserService;

    private static Logger log = LoggerFactory.getLogger(LoginController.class);


    @RequestMapping("/phonelogin")
    public String toPhoneLogin() {
        return "phonelogin";
    }

    @RequestMapping(value = "/sendNote",method = RequestMethod.POST)
    @ResponseBody
    public void sendNote(String phone, HttpServletResponse response){
        System.out.println(phone);
        String template = "模板code";//模板code写自己的
        try {
            response.getWriter().write(sendNoteUtil.sendNoteMessgae(phone,template));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Result<Boolean> login(HttpServletResponse response,String phone,String authCode){
        //验证验证码
        if(!verifyAuthCode(authCode,phone)){
            return Result.success(false);
        }
        LoginVo loginVo = new LoginVo();
        loginVo.setMobile(phone);
        log.info(loginVo.toString());
        //登录
        miaoshaUserService.login(response, loginVo);

        return Result.success(true);
    }

    //对输入的验证码进行校验
    private boolean verifyAuthCode(String authCode, String telephone){
        if(StringUtils.isEmpty(authCode)){
            return false;
        }
        UserKey userKey = new UserKey("oo");
        String realAuthCode = redisService.get(userKey,telephone,String.class);
        return authCode.equals(realAuthCode);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值