Day17JavaWeb [tourism project] development function: activation***

Introduction to UUID

(1) What is the
unique fixed-length random string of uuid in the world that will not repeat
25fd9bcf50ad4dc39aa38f084d1801c8
(2) Copy the UUI tool class
com\wzx\util\UuidUtil.java

public final class UuidUtil {
    
    
	private UuidUtil(){
    
    }
	public static String getUuid(){
    
    
		return UUID.randomUUID().toString().replace("-","");
	}

}

(3) Test

public class UuidUtilTest {
    
    

    @Test
    public void getUuid() {
    
    
        for (int i = 0; i < 10; i++) {
    
    
            String code  = UuidUtil.getUuid();
            System.out.println(code);
        }

    }
}

Code behind

Registered user setting status and activation code

 user.setStatus("N");//未激活
 user.setCode(UuidUtil.getUuid());//激活

Insert picture description here

Activation method test

   @Test
    public void test04() {
    
    
        UserService userService = new UserService();
        //根据 code -> status 改为Y
       int code = userService.active("bb26f0d05ea745c5986bc18ff7b4cef9");
        System.out.println(code);
    }

Activation method implementation

    public int active(String activeCode) {
    
    
        UserDao userDao = MySessionUtils2.getMapper(UserDao.class);
        int code =  userDao.updateStatus(activeCode); //1 表示成功
        MySessionUtils2.commitAndClose();
        return code;
    }

UserDao updateStatus method implementation

    //update tab_user set status ='Y' where code = 'bb26f0d05ea745c5986bc18ff7b4cef9';
    int updateStatus(String activeCode);

Mapping file modification

    <update id="updateStatus" parameterType="string" >
        update tab_user set status ='Y' where code = #{code};
    </update>

Write the test of the dao method

@Test
    public void update() {
    
    
        UserDao dao =    MySessionUtils2.getMapper(UserDao.class);
        int code =  dao.updateStatus("bb26f0d05ea745c5986bc18ff7b4cef9");
        System.out.println(code);
        MySessionUtils2.commitAndClose();
    }

Front desk code

http://localhost:8080/lvyou_war_exploded/activeServlet?activeCode=bb26f0d05ea745c5986bc18ff7b4cef9

Write 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>

Send activation link by email

Insert picture description here
Insert picture description here

  • MailUtil is a tool for sending mail
  • Copy directly from the data
  • Need to write a test
public final class MailUtils {
    
    
    private static final String USER = "[email protected]"; // 发件人称号,同邮箱地址
    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;
    }


}

Test code

public class MailUtilsTest {
    
    

    @Test
    public void sendMail() {
    
    
        //参1 收件人
        //参2 内容
        //参3 标题
        MailUtils.sendMail("[email protected]","<a href='http://localhost:8080/lvyou_war_exploded/activeServlet?activeCode=bb26f0d05ea745c5986bc18ff7b4cef9'>点击激活途牛旅游账户</a>","激活账户");
    }
}

Add email in the registration business method

 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;
        }

    }

Guess you like

Origin blog.csdn.net/u013621398/article/details/108869673