1.使用邮件发送验证码
1.1 引入依赖
坑点:有时候遇到验证码发不出去的情况,要调整依赖的版本,更新为高版本的依赖
1 2 3 4 5 6 7 8 9 10 11 12 13
| <dependencies> <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.5.6</version> </dependency> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.5.3</version> </dependency> </dependencies>
|
1.2 导入发送验证码的工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
static { try { FileInputStream fileInputStream = new FileInputStream("src/main/resources/application.properties"); Properties properties = new Properties(); properties.load(fileInputStream); user = properties.getProperty("guli.user"); password = properties.getProperty("guli.password"); } catch (IOException e) { throw new RuntimeException(e); }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties;
public class MailUtils { private static String USER = "xxxxxxxx"; private static String PASSWORD = "xxxxxxxx";
public static boolean sendMail(String to, String text, String title) { try { final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.qq.com");
props.put("mail.user", USER); props.put("mail.password", PASSWORD);
Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; Session mailSession = Session.getInstance(props, authenticator); MimeMessage message = new MimeMessage(mailSession); String username = props.getProperty("mail.user"); InternetAddress form = new InternetAddress(username); message.setFrom(form);
InternetAddress toAddress = new InternetAddress(to); message.setRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(title);
message.setContent(text, "text/html;charset=UTF-8"); Transport.send(message); return true; } catch (Exception e) { e.printStackTrace(); } return false; }
}
|
1.3 导入随机生成验证码的工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import java.util.Random;
public class ValidateCodeUtils {
public static Integer generateValidateCode(int length) { Integer code = null; if (length == 4) { code = new Random().nextInt(9999); if (code < 1000) { code = code + 1000; } } else if (length == 6) { code = new Random().nextInt(999999); if (code < 100000) { code = code + 100000; } } else { throw new RuntimeException("只能生成4位或6位数字验证码"); } return code; }
public static String generateValidateCode4String(int length) { Random rdm = new Random(); String hash1 = Integer.toHexString(rdm.nextInt()); String capstr = hash1.substring(0, length); return capstr; } }
|
1.4 测试使用
1 2 3
| 1.设置发送邮件的账号和授权码信息 2.编写测试类测试邮件是否可以正常的发送 3.添加到业务代码中,实现验证码的发送功能
|
2.使用阿里云的短信服务
阿里云的短信服务不面向个人用户,无法使用,但是使用的基本逻辑和使用邮箱类似
3.Token字符串生成的工具类(JWT)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package com.atguigu.commonutils;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jws; import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest; import java.util.Date;
public class JwtUtils {
public static final long EXPIRE = 1000 * 60 * 60 * 24;
public static final String APP_SECRET = "ukc8BDbRigUDaY6pZFfWus2jZWLPHO";
public static String getJwtToken(String id, String nickname){
String JwtToken = Jwts.builder() .setHeaderParam("typ", "JWT") .setHeaderParam("alg", "HS256") .setSubject("guli-user") .setIssuedAt(new Date()) .setExpiration(new Date(System.currentTimeMillis() + EXPIRE)) .claim("id", id) .claim("nickname", nickname) .signWith(SignatureAlgorithm.HS256, APP_SECRET) .compact();
return JwtToken; }
public static boolean checkToken(String jwtToken) { if(StringUtils.isEmpty(jwtToken)) return false; try { Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
public static boolean checkToken(HttpServletRequest request) { try { String jwtToken = request.getHeader("token"); if(StringUtils.isEmpty(jwtToken)) return false; Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken); } catch (Exception e) { e.printStackTrace(); return false; } return true; }
public static String getMemberIdByJwtToken(HttpServletRequest request) { String jwtToken = request.getHeader("token"); if(StringUtils.isEmpty(jwtToken)) return ""; Jws<Claims> claimsJws = Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken); Claims claims = claimsJws.getBody(); return (String)claims.get("id"); } }
|