Springboot实现短信验证登陆

导读:本篇文章讲解 Springboot实现短信验证登陆,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

1:导入依赖:

   <!--    发送邮件-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-email</artifactId>
            <version>1.5</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.2</version>
        </dependency>

2:配置application.yml:username就是qq邮箱,密码是那个授权码,在qq邮箱设置里面打开

请添加图片描述

3:控制层:

请添加图片描述

4:MailServiceImpl:

package com.example.service.impl;


import com.example.service.MailService;
import com.example.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Random;

@Service
public class MailServiceImpl implements MailService {

    @Autowired
    private JavaMailSender mailSender;//一定要用@Autowired

    @Resource
    private RedisService redisService;  //redis依赖

    //application.properties中已配置的值
    @Value("${spring.mail.username}")
    private String from;

    /**
     * 给前端输入的邮箱,发送验证码
     * @param email
     * @return
     */
    public boolean sendMimeMail( String email) {
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setSubject("验证码邮件");//主题
            //生成随机数
            String code = randomCode();
            //将随机数放置到Redis中
            redisService.set(email,code,60);
            mailMessage.setText("您收到的验证码是:"+code);//内容
            mailMessage.setTo(email);//发给谁
            mailMessage.setFrom(from);//你自己的邮箱
            mailSender.send(mailMessage);//发送
            return  true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 随机生成6位数的验证码
     * @return String code
     */
    public String randomCode(){
        StringBuilder str = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            str.append(random.nextInt(10));
        }
        return str.toString();
    }


}

请添加图片描述

5:验证码校验的接口:

请添加图片描述

 /**
     * 验证码校验
     */
    @PostMapping("/CodeVerify")
    public Result<User> Code(@RequestParam String code,@RequestParam String emile){
        String redisCode = redisService.get(emile).toString();
        if (redisCode.equals(code)){
            LambdaQueryWrapper queryWrapper = Wrappers.<User>lambdaQuery().eq(User::getEmail,emile);
            User res = userService.getOne(queryWrapper);
            // 生成token
            String token = JWT.create()
                    .withAudience(res.getUsername())
                    .sign(Algorithm.HMAC256(res.getPassword()));
            res.setToken(token);
            logService.log(res.getUsername(), StrUtil.format("用户 {} 登录系统", res.getUsername()));
            //将token存入redis缓存中:过期时间半小时
            redisService.set(res.getUsername(),token,1800);
            return Result.success(res);
        }
        return Result.error("401","验证码错误");

    }

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/80963.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!