下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
@RequestMapping(value = "/user/i_forget_password") @ResponseBody public Map forgetPass(HttpServletRequest request,String userName){ Users users = userService.findUserByName(userName); Map map = new HashMap<String,String >(); String msg = ""; if(users == null){ //用户名不存在 msg = "用户名不存在,你不会忘记用户名了吧?"; map.put("msg",msg); return map; } try{ String secretKey= UUID.randomUUID().toString(); //密钥 Timestamp outDate = new Timestamp(System.currentTimeMillis()+30*60*1000);//30分钟后过期 long date = outDate.getTime()/1000*1000; //忽略毫秒数 users.setValidataCode(secretKey); users.setRegisterDate(outDate); userService.update(users); //保存到数据库 String key = users.getUserName()+"$"+date+"$"+secretKey; String digitalSignature = MD5.MD5Encode(key); //数字签名 String emailTitle = "有方云密码找回"; String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String resetPassHref = basePath+"user/reset_password?sid="+digitalSignature+"&userName="+users.getUserName(); String emailContent = "请勿回复本邮件.点击下面的链接,重设密码<br/><a href="+resetPassHref +" target='_BLANK'>点击我重新设置密码</a>" + "<br/>tips:本邮件超过30分钟,链接将会失效,需要重新申请'找回密码'"+key+"\t"+digitalSignature; System.out.print(resetPassHref); SendMail.getInstatnce().sendHtmlMail(emailTitle,emailContent,users.getEmail()); msg = "操作成功,已经发送找回密码链接到您邮箱。请在30分钟内重置密码"; logInfo(request,userName,"申请找回密码"); }catch (Exception e){ e.printStackTrace(); msg="邮箱不存在?未知错误,联系管理员吧。"; } map.put("msg",msg); return map; }
@RequestMapping(value = "/user/reset_password",method = RequestMethod.GET) public ModelAndView checkResetLink(String sid,String userName){ ModelAndView model = new ModelAndView("error"); String msg = ""; if(sid.equals("") || userName.equals("")){ msg="链接不完整,请重新生成"; model.addObject("msg",msg) ; logInfo(userName,"找回密码链接失效"); return model; } Users users = userService.findUserByName(userName); if(users == null){ msg = "链接错误,无法找到匹配用户,请重新申请找回密码."; model.addObject("msg","找回密码链接失效"); return model; } Timestamp outDate = users.getRegisterDate(); if(outDate.getTime() <= System.currentTimeMillis()){ //表示已经过期 msg = "链接已经过期,"找回密码链接失效"); return model; } String key = users.getUserName()+"$"+outDate.getTime()/1000*1000+"$"+users.getValidataCode(); //数字签名 String digitalSignature = MD5.MD5Encode(key); System.out.println(key+"\t"+digitalSignature); if(!digitalSignature.equals(sid)) { msg = "链接不正确,是否已经过期了?重新申请吧"; model.addObject("msg","找回密码链接失效"); return model; } model.setViewName("user/reset_password"); //返回到修改密码的界面 model.addObject("userName",userName); return model; }
sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); mailMessage.setSubject(MimeUtility.encodeText(mailInfo.getSubject(),"UTF-8","B")); //解决linux邮件title乱码
补充3:怎么不直接把sid插入到user表呢。验证的时候直接比较sid就ok了。
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
原文链接:https://www.f2er.com/springmvc/461593.html