一、验证码实现(数字+字母)
说明:引用https://www.jb51.net/article/282809.htm#_lab2_1_0。
1、添加依赖
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
2、编写配置类
验证码的形式可以在下面改,这里是生成四位数字+字母的形式。
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
/**
* 生成验证码的配置
*/
@Configuration
public class KaptchaConfig {
@Bean
public Producer kaptchaProducer() {
Properties properties = new Properties();
//设置验证码的宽度
properties.setProperty("kaptcha.image.width", "100");
//设置宽度
properties.setProperty("kaptcha.image.height", "40");
//设置字体大小
properties.setProperty("kaptcha.textproducer.font.size", "32");
//设置字体颜色
properties.setProperty("kaptcha.textproducer.font.color", "0,0,0");
//限定验证码中的字符
properties.setProperty("kaptcha.textproducer.char.string", "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
//设置验证码的长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
//设置添加噪声与否
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
//将配置装载到一个实例中
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
//将配置传入实例
defaultKaptcha.setConfig(new Config(properties));
return defaultKaptcha;
}
}
3、使用
@Autowired
private Producer checkCode;
@Test
void contextLoads() {
String text = checkCode.createText();
System.out.println(true);
}
二、集成发送邮件功能(使用QQ邮箱来发送和接收验证码)
说明:引用https://www.jb51.net/article/282809.htm#_lab2_1_0。
1、说明
在系统中使用到的邮箱发送邮件属于第三方登录,需要登录QQ邮箱配置第三方登录。
1)个人邮箱:需要邮箱地址、授权码。
授权码获取:登录QQ邮箱,点击设置,跳转后找到账户。开启POP3/SMTP服务。绑定邮箱的手机验证时可以获取授权码。
2)企业邮箱:腾讯企业邮箱需要邮箱地址、密码。
2、添加依赖
Spring提供了非常好用的JavaMailSender接口实现邮件发送。由于SpringBoot的Starter模块也为此提供了自动化配置,所以在引入了spring-boot-starter-mail依赖之后,会根据配置文件中的内容去创建JavaMailSender实例,因此可以直接在需要使用的地方直接@Autowired来引入邮件发送对象。
3、配置yml
4、写一个发送邮件的工具类
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
/**
* 发邮件工具类
*/
@Component
public final class MailUtils {
@Value("${mail.user}")
private String USER; // 发件人邮箱地址
@Value("${mail.password}")
private String PASSWORD; // 如果是qq邮箱可以使户端授权码
/**
* 发送邮件
* @param to 收件人邮箱
* @param text 邮件正文
* @param title 标题
*/
public 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);
// 构建授权信息,用于进行SMTP进行身份验证
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;
}
}
工具类使用了**@Component**注解将这个工具类放入IOC容器中,需要使用的时候方便取。这个工具类只有一个方法——sendMail(String to, String text, String title),就是用来发邮件的方法。