最近负责公司的登陆与注册模块,大部分已经实现了,这找回密码功能实现的思路是这样的
1、用户忘记密码,需要找回密码
2、后台通过一系列的加密处理,将通过后台邮件将重置密码的页面发送到该用户的邮箱
3、用户通过邮箱里烦人链接就可以重置密码。
以下是代码
/* ------------------------------------------忘记密码,发送邮件------------------------------------------------- */ //找回密码发送邮件功能 @RequestMapping(value="getPwd.action") public String getPwd(HttpServletRequest request) throws Exception{ User model = userDao.check(request.getParameter("name")); if(model != null && !model.equals("")){ //秘钥 String secretKey = UUID.randomUUID().toString(); //30分钟后过期 Timestamp outDate = new Timestamp(System.currentTimeMillis()+30*60*1000); //忽略毫秒数 long date = outDate.getTime()/1000*1000; String key = model.getName()+"$"+date+"$"+secretKey; String digitalSignature = md5.getMD5ofStr(key); //数字签名 String emailTitle = "找回密码"; String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String resetPassHref = basePath+"fupdatePwd.action?sid="+digitalSignature+"&name="+model.getName(); String emailContent = "请勿回复本邮件,点击下面链接,重置密码<br><a href="+resetPassHref+"&target=‘_BLANK‘>点击我重新设置密码</a>"+ "<br/>tips:本邮件超过30分钟,链接将会失效,需要重新申请‘找回密码‘"+key+"\t"+digitalSignature; String name = model.getName(); String to = model.getEmail(); //发送邮件 Mail.sendAndCc(to, emailContent);//Mail.java上文已经提到,是用来发送邮件的工具类 request.setAttribute("message", "提交成功,请注意查看邮箱!"); return "forward:/getPwd.jsp"; }else{ request.setAttribute("error", "查无此用户!"); return "forward:/getPwd.jsp"; } } //跳转到忘记密码,提交用户界面 @RequestMapping(value="fgetPwd.action") public String fGetPwd(){ return "forward:/getPwd.jsp"; } //跳转到重置密码的页面 @RequestMapping(value="fupdatePwd.action") public String fupdatePwd(HttpServletRequest request) throws Exception{ String name = request.getParameter("name"); request.setAttribute("user", userDao.check(name)); return "forgetPwd"; } //重置密码 @RequestMapping(value="updatePwd.action") public String updatePwd(HttpServletRequest request) throws Exception{ User user = new User(); user.setId(Integer.parseInt(request.getParameter("id"))); String password = request.getParameter("password"); user.setPassword(md5.getMD5ofStr(password));//MD5也是一个工具类,其作用是对密码进行加密 userDao.updatePwd(user); return "redirect:index.jsp"; }
找回密码功能的实现(考虑安全性,加密方面)
时间: 2024-10-10 08:29:43