手机短信&邮箱验证

    <!--        redis-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--        阿里·反序列化redis-->
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.25</version>
        </dependency>
        <!--邮箱-->
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
            <version>1.6.0</version>
        </dependency>

1.UtilsController

@RequestMapping("/Utils")
public class UtilsController {
    //邮箱验证码类
    @Autowired
    private MailUtils mailUtils;
    //手机验证码类
    @Autowired
    private PhoneCode phoneCode;
    @Autowired
    private RedisTemplate redisTemplate;
    //请求头
    @Autowired
    private HttpHeaders httpHeaders;

    /**
     * @Description 获取邮箱验证码
     * @Param mail
     * @Return {@link R < String>}
     * @Author wenjun
     * @Date 2023/7/27 10:11
     */
    @ApiOperation("获取邮箱验证码")
    @GetMapping("/Mailcode/{mail}")
    public R<String> getMailcode(@PathVariable("mail") @ApiParam("邮箱号") String mail){
        //邮箱正则
        RegUtils.isEmail(mail);
        Object str = redisTemplate.opsForValue().get(mail);
        if (str != null) {
            return R.fail("请5分钟之后再重新获取");
        } else {
            mailUtils.toMail(mail, "注册验证码");
            return R.success("发送成功,请注意查收");
        }
    }

    /**
     * @Description 获取手机验证码
     * @Param phone
     * @Return {@link R< String>}
     * @Author wenjun
     * @Date 2023/7/27 10:11
     */
    @ApiOperation("获取手机验证码")
    @GetMapping("/phoneCode/{phone}")
    public R<String> getphoneMailcode(@PathVariable("phone") @ApiParam("手机号码") String phone){
        //手机号码
        RegUtils.isMoblie(phone);
        //手机号码发送验证码
        Object str = redisTemplate.opsForValue().get(phone);
        if (str != null) {
            return R.fail("请5分钟之后再重新获取");
        } else {
            phoneCode.Codes(phone);
            return R.success("发送成功,请注意查收");
        }
    }

    /**
     * @Description 获取手机或邮箱验证码
     * @Param number
     * @Return {@link R< String>}
     * @Author wenjun
     * @Date 2023/8/28 14:21
     */
    @ApiOperation("获取手机或邮箱验证码")
    @GetMapping("/code/{number}")
    public R<String> getnumber(@PathVariable("number") @ApiParam("手机号码或邮箱") String number){
        //手机号码
        boolean moblie = RegUtils.isMatch(RegUtils.REGEX_PHONE,number);
        //邮箱号
        boolean email = RegUtils.isMatch(RegUtils.REGEX_EMAIL,number);
        if (moblie){
            //手机号码发送验证码
            Object str = redisTemplate.opsForValue().get(number);
            if (str != null) {
                return R.fail("请5分钟之后再重新获取");
            } else {
                phoneCode.Codes(number);
                return R.success("发送成功,请注意查收");
            }
        }else if (email){
            Object str = redisTemplate.opsForValue().get(number);
            if (str != null) {
                return R.fail("请5分钟之后再重新获取");
            } else {
                mailUtils.toMail(number, "注册验证码");
                return R.success("发送成功,请注意查收");
            }
        }else {
            return R.fail("手机号码或邮箱账号格式不正确");
        }
    }
}

2.MailUtils类

2.1邮箱授权码自己通过邮箱获取

package com.appapi.comm;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;


import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

@Slf4j
@Configuration
public class MailUtils {
    @Autowired
    private RedisTemplate redisTemplate;
    public void toMail(String email,String msg){
        //验证码
        int code= (int) (Math.random()*9000+1000);
        log.info(String.valueOf(code));
        redisTemplate.opsForValue().set(email,code,5, TimeUnit.MINUTES);
        // 收件人电子邮箱
        String to = email;

        // 发件人电子邮箱
        String from = "XXXXX@qq.com";

        // 指定发送邮件的主机为 smtp.qq.com
        String host = "smtp.qq.com";  //QQ 邮件服务器

        //授权码
        String key="XXXXX";

        // 获取系统属性
        Properties properties = System.getProperties();

        // 设置邮件服务器
        properties.setProperty("mail.smtp.host", host);

        properties.put("mail.smtp.auth", "true");

        // 2.4设置邮件内容
        String content = "<head>\n" +
                "    <base target=\"_blank\" />\n" +
                "    <style type=\"text/css\">::-webkit-scrollbar{ display: none; }</style>\n" +
                "    <style id=\"cloudAttachStyle\" type=\"text/css\">#divNeteaseBigAttach, #divNeteaseBigAttach_bak{display:none;}</style>\n" +
                "    <style id=\"blockquoteStyle\" type=\"text/css\">blockquote{display:none;}</style>\n" +
                "    <style type=\"text/css\">\n" +
                "        body{font-size:14px;font-family:arial,verdana,sans-serif;line-height:1.666;padding:0;margin:0;overflow:auto;white-space:normal;word-wrap:break-word;min-height:100px}\n" +
                "        td, input, button, select, body{font-family:Helvetica, 'Microsoft Yahei', verdana}\n" +
                "        pre {white-space:pre-wrap;white-space:-moz-pre-wrap;white-space:-pre-wrap;white-space:-o-pre-wrap;word-wrap:break-word;width:95%}\n" +
                "        th,td{font-family:arial,verdana,sans-serif;line-height:1.666}\n" +
                "        img{ border:0}\n" +
                "        header,footer,section,aside,article,nav,hgroup,figure,figcaption{display:block}\n" +
                "        blockquote{margin-right:0px}\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body tabindex=\"0\" role=\"listitem\">\n" +
                "<table width=\"700\" border=\"0\" align=\"center\" cellspacing=\"0\" style=\"width:700px;\">\n" +
                "    <tbody>\n" +
                "    <tr>\n" +
                "        <td>\n" +
                "            <div style=\"width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;\">\n" +
                "                <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"700\" height=\"39\" style=\"font:12px Tahoma, Arial, 宋体;\">\n" +
                "                    <tbody><tr><td width=\"210\"></td></tr></tbody>\n" +
                "                </table>\n" +
                "            </div>\n" +
                "            <div style=\"width:680px;padding:0 10px;margin:0 auto;\">\n" +
                "                <div style=\"line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;\">\n" +
                "                    <strong style=\"display:block;margin-bottom:15px;\">尊敬的用户:<span style=\"color:#f60;font-size: 16px;\"></span>您好!</strong>\n" +
                "                    <strong style=\"display:block;margin-bottom:15px;\">\n" +
                "                       您的验证码是:<span style=\"color:#f60;font-size: 24px\">"+code+"</span>,以完成操作。\n" +
                "                    </strong>\n" +
                "                </div>\n" +
                "                <div style=\"margin-bottom:30px;\">\n" +
                "                    <small style=\"display:block;margin-bottom:20px;font-size:12px;\">\n" +
                "                        <p style=\"color:#747474;\">\n" +
                "                            注意:此验证码5分钟内有效。如非本人操作,请及时登录并修改密码以保证帐户安全\n" +
                "                            <br>(工作人员不会向你索取此验证码,请勿泄漏!)\n" +
                "                        </p>\n" +
                "                    </small>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "            <div style=\"width:700px;margin:0 auto;\">\n" +
                "                <div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#747474;margin-bottom:20px;line-height:1.3em;font-size:12px;\">\n" +
                "                    <p>此为系统邮件,请勿回复<br>\n" +
                "                        请保管好您的邮箱,避免账号被他人盗用\n" +
                "                    </p>\n" +
                "                    <p>XXXX有限公司</p>\n" +
                "                </div>\n" +
                "            </div>\n" +
                "        </td>\n" +
                "    </tr>\n" +
                "    </tbody>\n" +
                "</table>\n" +
                "</body>";
        // 获取默认session对象
        Session session = Session.getDefaultInstance(properties,new Authenticator(){
            @Override
            public PasswordAuthentication getPasswordAuthentication() {

                // 发件人邮件用户名、授权码
                // 我的授权码gbuoutlxeriqjeae(写你自己)
                return new PasswordAuthentication(from, key);
            }
        });

        try{
            // 创建默认的 MimeMessage 对象
            MimeMessage message = new MimeMessage(session);

            // Set From: 头部头字段
            message.setFrom(new InternetAddress(from));

            // Set To: 头部头字段
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));

            // Set Subject: 头部头字段
            message.setSubject(msg);

            // 设置消息体
            //message.setText("This is actual message");
            message.setContent(content,"text/html;charset=UTF-8");

            // 发送消息
            Transport.send(message);
            System.out.println("Sent message successfully....Harmony");
        }catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }
}

3.PhoneCode 

3.1该类来源短信宝官方

package com.appapi.comm;

/**
 * @Author: 文君
 * @Date: 2023-06-29-11:12
 * @Description:短信认证
 */
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.TimeUnit;

@Configuration
public class PhoneCode {
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String testUsername ="xxxx"; //在短信宝注册的用户名
    private static final String testPassword ="xxxx"; //在短信宝注册的密码
    //String testPhone = "13000000000";
    private static final String httpUrl = "http://api.smsbao.com/sms";
    public Object Codes(String testPhone){
        int code= (int) (Math.random()*9000+1000);
        // 注意测试时,也请带上公司简称或网站签名,发送正规内容短信。千万不要发送无意义的内容:例如 测一下、您好。否则可能会收不到
        String testContent = "【xxxx】您的验证码是"+code+",5分钟内有效。若非本人操作请忽略此消息。";
        redisTemplate.opsForValue().set(testPhone,code,5,TimeUnit.MINUTES);
        System.out.println(testContent);
        StringBuffer httpArg = new StringBuffer();
        httpArg.append("u=").append(testUsername).append("&");
        httpArg.append("p=").append(md5(testPassword)).append("&");
        httpArg.append("m=").append(testPhone).append("&");
        httpArg.append("c=").append(encodeUrlString(testContent, "UTF-8"));
        String result = request(httpUrl, httpArg.toString());
        System.out.println(result);
        return result;
    }

    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;

        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = reader.readLine();
            if (strRead != null) {
                sbf.append(strRead);
                while ((strRead = reader.readLine()) != null) {
                    sbf.append("\n");
                    sbf.append(strRead);
                }
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String md5(String plainText) {
        StringBuffer buf = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }

    public static String encodeUrlString(String str, String charset) {
        String strret = null;
        if (str == null){
            return str;
        }
        try {
            strret = java.net.URLEncoder.encode(str, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return strret;
    }
}

4.正则RegUtils 

package com.appapi.comm;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: 文君
 * @Date: 2023-07-26-8:33
 * @Description:正则表达式
 */
public final class RegUtils {

    /*------------------ 正则表达式 ---------------------*/
    /**
     * 邮箱
     */
    public static final String REGEX_EMAIL = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
    /**
     * 手机号码
     */
    public static final String REGEX_PHONE = "^13[0-9]{9}|15[012356789][0-9]{8}|18[0-9]{9}|(14[57][0-9]{8})|(17[015678][0-9]{8})$";
    /**
     * 仅中文
     */
    public static final String REGEX_CHINESE = "^[\\u4E00-\\u9FA5\\uF900-\\uFA2D]+$";
    /**
     * 整数
     */
    public static final String REGEX_INTEGER = "^-?[1-9]\\d*$";
    /**
     * 数字
     */
    public static final String REGEX_NUMBER = "^([+-]?)\\d*\\.?\\d+$";
    /**
     * 正整数
     */
    public static final String REGEX_INTEGER_POS = "^[1-9]\\d*$";
    /**
     * 浮点数
     */
    public static final String REGEX_FLOAT = "^([+-]?)\\d*\\.\\d+$";
    /**
     * 正浮点数
     */
    public static final String REGEX_FLOAT_POS = "^[1-9]\\d*.\\d*|0.\\d*[1-9]\\d*$";
    /**
     * 字母
     */
    public static final String REGEX_LETTER = "^[A-Za-z]+$";
    /**
     * 大写字母
     */
    public static final String REGEX_LETTER_UPPERCASE = "^[A-Z]+$";
    /**
     * 小写字母
     */
    public static final String REGEX_LETTER_LOWERCASE = "^[a-z]+$";
    /**
     * 邮编
     */
    public static final String REGEX_ZIPCODE = "^\\d{6}$";
    /**
     * ip v4地址
     */
    public static final String REGEX_IP4 = "^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$";
    /**
     * 图片
     */
    public static final String REGEX_PICTURE = "(.*)\\.(jpg|bmp|gif|ico|pcx|jpeg|tif|png|raw|tga)$";/**
     /**
     * 压缩文件
     */
    public static final String REGEX_RAR = "(.*)\\.(rar|zip|7zip|tgz)$";
    /**
     * QQ号码,最短5位,最长15位数字
     */
    public static final String REGEX_QQ = "^[1-9]\\d{4,14}$";
    /**
     * 日期(yyyy-MM-dd)
     */
    public static final String REGEX_DATE = "^\\d{4}\\D+\\d{2}\\D+\\d{2}$";
    /**
     * 日期(yyyy-MM-dd),精确,能检查到2月及31号
     */
    public static final String REGEX_DATE_PRECISE = "^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
    /**
     * 时间(HH:mm:ss或HH:mm)
     */
    public static final String REGEX_TIME = "^((([0-1][0-9])|2[0-3]):[0-5][0-9])(:[0-5][0-9])?$";

    /**
     * 校验手机号码
     * @param mobile
     * @return
     * @author lqyao
     */
    public static final boolean isMoblie(String mobile){
        boolean flag = false;
        if (null != mobile && !mobile.trim().equals("") && mobile.trim().length() == 11) {
            Pattern pattern = Pattern.compile(REGEX_PHONE);
            Matcher matcher = pattern.matcher(mobile.trim());
            if (matcher.matches()){
                return matcher.matches();
            }else {
                throw new RuntimeException("手机号码格式不正确!");
            }
        }
        throw new RuntimeException("手机号码格式不正确!");
    }

    /**
     * 校验邮箱
     * @param value
     * @return
     * @author lqyao
     */
    public static final boolean isEmail(String value){
        boolean flag = false;
        if (null != value && !value.trim().equals("")) {
            Pattern pattern = Pattern.compile(REGEX_EMAIL);
            Matcher matcher = pattern.matcher(value.trim());
            if (matcher.matches()){
                return matcher.matches();
            }else {
                throw new RuntimeException("邮箱格式不正确!");
            }
        }
        throw new RuntimeException("邮箱格式不正确!");
    }

    /**
     * 校验密码
     * @param password
     * @return 长度符合返回true,否则为false
     * @author lqyao
     * @since 2015-09-24
     */
    public static final boolean isPassword(String password){
        boolean flag = false;
        if (null != password && !password.trim().equals("")) {
            password = password.trim();
            if(password.length() >= 6 && password.length() <= 30){
                return true;
            }
        }
        throw new RuntimeException("密码格式不正确!");
    }

    /**
     * 校验手机验证码
     * @param value
     * @return 符合正则表达式返回true,否则返回false
     * @author lqyao
     * @since 2015-09-24
     */
    public static final boolean isPhoneValidateCode(String value){
        boolean flag = false;
        if (null != value && !value.trim().equals("")) {
            Pattern pattern = Pattern.compile("^8\\d{5}$");
            Matcher matcher = pattern.matcher(value.trim());
            return matcher.matches();
        }
        throw new RuntimeException("手机验证码格式不正确!");
    }

    /**
     * 正则表达式校验,符合返回True
     * @param regex 正则表达式
     * @param content 校验的内容
     * @return
     * @author lqy
     */
    public static boolean isMatch(String regex, CharSequence content){
        return Pattern.matches(regex, content);
    }

    public static boolean isUpperCase(String str){
        if(StringUtils.isEmpty(str)){
            return false;
        }
        String reg = "^[A-Z]$";
        return isMatch(reg,str);
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当我们注册Gmail邮箱时,通常需要提供一个手机号来进行验证。然而,有时候可能会遇到手机号无法验证的问题。出现这种问题可能有以下几个原因: 首先,可能是手机号码输入错误。我们需要确保输入的手机号码是准确的,没有错漏。在输入手机号码时,要注意不要加入无关的符号或者空格。 其次,可能是网络问题。验证手机号码需要通过网络发送短信或者电话来获取确认码。如果我们的网络连接有问题,那么可能无法正常进行验证过程。此时,我们可以尝试连接其他可靠的网络进行验证,或者稍后再尝试。 另外,可能是手机服务商或者运营商的问题。有时候,我们使用的手机号码可能存在一些问题,例如网络信号不稳定、号码被封锁、欠费等。如果我们怀疑是这个原因导致手机号无法验证,可以联系手机服务商或者运营商,咨询并解决问题。 最后,也可能是Gmail系统本身的问题。有时候,Gmail系统可能出现故障或者其他技术问题,导致手机号无法验证。这种情况下,我们可以尝试稍后再次进行验证,或者联系Gmail客服获取帮助。 总之,当遇到Gmail注册中手机号无法验证的问题时,我们可以先检查手机号输入是否准确,然后尝试使用其他网络进行验证。如果问题依然存在,可以咨询手机服务商或者运营商,或者联系Gmail客服以获得更多帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值