spring boot结合redis实现阿里云短信验证码

登录阿里云控制台

阿里云地址:https://www.aliyun.com/

在搜索栏目搜索短信服务,然后点击;

spring boot结合redis实现阿里云短信验证码_第1张图片

在控制台首页点击我的-->AccessKey管理,然后点击

spring boot结合redis实现阿里云短信验证码_第2张图片

 

点击开始使用子用户

spring boot结合redis实现阿里云短信验证码_第3张图片

然后创建用户组并且给用户组添加短信权限(在搜索栏搜索sms),然后创建用户,用户添加至用户组,此处设计AccessKey没有截图

然后在进入短信服务中,申请签名与模板

spring boot结合redis实现阿里云短信验证码_第4张图片

等待两个小时,申请通过后就可以使用了;

springboot结合redis实现短信登陆

导入依赖


            org.springframework.boot
            spring-boot-starter-web
        

        
            com.aliyun
            aliyun-java-sdk-core
            4.5.1
        

        
            com.alibaba
            fastjson
            1.2.62
        

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

测试短信(test)

DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeyId", "生成accessKeys所对应的密码");
        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("PhoneNumbers", "电话号码");
        request.putQueryParameter("SignName", "签名名称");
        request.putQueryParameter("TemplateCode", "模板代码");
        Map map = new HashMap<>();
        map.put("code",666666);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

测试成功,然后结合redis,实现短信登陆验证码

新建service及service实现类,此处省略service类

@Service
public class SendSmsImpl implements SendSms {
    @Override
    public Boolean sendSms(String phoneNum, String templateCode, Map code) {

        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "accessKeysId", "生成accessKeys所对应的密码");
        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("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "签名名称");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
        try {
            CommonResponse response = client.getCommonResponse(request);
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}

新建controller实现登陆

@RestController
public class SendSmsController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SendSms sendSms;

    @GetMapping("/send/{phone}")
    public String code(@PathVariable("phone") String phone){
        String code = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){
            return phone + ": " + code + "该验证尚未过期!";
        }
        //生成验证码
        code = UUID.randomUUID().toString().substring(0, 4);
        Map map = new HashMap<>();
        map.put("code",code);
        Boolean aBoolean = sendSms.sendSms(phone, "SMS_200721357", map);
        if (aBoolean){
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.SECONDS);
            return phone + ":" + code + "发送成功!";
        }else {
            return  phone + ":" + "发送失败!";
        }
    }
}

测试成功!

你可能感兴趣的:(springboot,redis,JAVA)