UUID介绍
(1)什么是uuid
全球唯一的,不会重复的 固定长度的随机字符串
25fd9bcf50ad4dc39aa38f084d1801c8
(2)复制UUI工具类
com\wzx\util\UuidUtil.java
public final class UuidUtil {
private UuidUtil(){}
public static String getUuid(){
return UUID.randomUUID().toString().replace("-","");
}
}
(3)测试
public class UuidUtilTest {
@Test
public void getUuid() {
for (int i = 0; i < 10; i++) {
String code = UuidUtil.getUuid();
System.out.println(code);
}
}
}
后台代码
注册用户设置状态与激活码
user.setStatus("N");//未激活
user.setCode(UuidUtil.getUuid());//激活
激活方法测试
@Test
public void test04() {
UserService userService = new UserService();
//根据 code -> status 改为Y
int code = userService.active("bb26f0d05ea745c5986bc18ff7b4cef9");
System.out.println(code);
}
激活方法实现
public int active(String activeCode) {
UserDao userDao = MySessionUtils2.getMapper(UserDao.class);
int code = userDao.updateStatus(activeCode); //1 表示成功
MySessionUtils2.commitAndClose();
return code;
}
UserDao updateStatus 方法实现
//update tab_user set status ='Y' where code = 'bb26f0d05ea745c5986bc18ff7b4cef9';
int updateStatus(String activeCode);
映射文件修改
<update id="updateStatus" parameterType="string" >
update tab_user set status ='Y' where code = #{code};
</update>
编写dao方法的测试
@Test
public void update() {
UserDao dao = MySessionUtils2.getMapper(UserDao.class);
int code = dao.updateStatus("bb26f0d05ea745c5986bc18ff7b4cef9");
System.out.println(code);
MySessionUtils2.commitAndClose();
}
前台代码
http://localhost:8080/lvyou_war_exploded/activeServlet?activeCode=bb26f0d05ea745c5986bc18ff7b4cef9
编写ActiveServlet
com\wzx\web\servlet\ActiveServlet.java
@WebServlet("/activeServlet")
public class ActiveServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取参数 activeCode //lvyou/activeServlet?activeCode=bb26f0d05ea745c5986bc18ff7b4cef9
String activeCode = request.getParameter("activeCode");
//处理参数
UserService userService = new UserService();
int code = userService.active(activeCode);
System.out.println(activeCode);
//响应给浏览器
String msg = "";
if(code==1){
msg="激活成功可以使用";
}else{
msg="激活失败";
}
request.setAttribute("msg",msg);
request.getRequestDispatcher("message.jsp").forward(request,response);
}
}
src\main\webapp\message.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>Title</title>
</head>
<body>
${msg}
</body>
</html>
使用邮件发送激活链接
- MailUtil是发送邮件的工具类
- 直接从资料复制过来
- 需要编写 个测试
public final class MailUtils {
private static final String USER = "289393698@qq.com"; // 发件人称号,同邮箱地址
private static final String PASSWORD = "aghemvlpcgpubjaa"; // 如果是qq邮箱可以使户端授权码,或者登录密码
/**
*
* @param to 收件人邮箱
* @param text 邮件正文
* @param title 标题
*/
/* 发送验证信息的邮件 */
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);
// 构建授权信息,用于进行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;
}
}
测试代码
public class MailUtilsTest {
@Test
public void sendMail() {
//参1 收件人
//参2 内容
//参3 标题
MailUtils.sendMail("hadoop101@126.com","<a href='http://localhost:8080/lvyou_war_exploded/activeServlet?activeCode=bb26f0d05ea745c5986bc18ff7b4cef9'>点击激活途牛旅游账户</a>","激活账户");
}
}
在注册业务方法中添加发邮件
public int register(User user) {
UserDao userDao = MySessionUtils2.getMapper(UserDao.class);
//判断 用户的账号是否存在
User user2 = userDao.findByUserName(user.getUsername());
//不存在,调用保存 返回 1
if(user2 == null){
user.setStatus("N");//未激活
String activeCode = UuidUtil.getUuid();
user.setCode(activeCode);//激活
userDao.save(user);
MySessionUtils2.commitAndClose();
//参1 收件人
//参2 内容
//参3 标题
MailUtils.sendMail(user.getEmail(),"<a href='http://localhost:8080/lvyou_war_exploded/activeServlet?activeCode="+activeCode+"'>点击激活途牛旅游账户</a>","激活账户");
return 1;
}else{
//存在,返回-1
return -1;
}
}