JAVA实现通过绑定邮箱找回密码功能

1.输入用户名及验证码,验证用户名是否存在


(1).生成验证码工具类


[java]  view plain  copy
  1.   
[java]  view plain  copy
  1. package com.utils;    
  2.     
  3. import java.awt.Color;    
  4. import java.awt.Font;    
  5. import java.awt.Graphics;    
  6. import java.awt.image.BufferedImage;    
  7. import java.util.HashMap;    
  8. import java.util.Map;    
  9. import java.util.Random;    
  10.     
  11. /**    
  12.  * @Title: GraphicsUtil.java  
  13.  * @copyright   
  14.  * @Package com.utils  
  15.  * @Description: TODO(这里用一句话描述这个类的作用)  
  16.  * @author Mr.chen  
  17.  * @date 2016-11-2 下午03:31:30  
  18.  */    
  19. public class GraphicsUtil {    
  20.         
  21.     private Font mFont = new Font("Times New Roman", Font.PLAIN, 17);     
  22.         
  23.     Color getRandColor(int fc,int bc){    
  24.         Random random = new Random();         
  25.         if(fc>255) fc=255;         
  26.         if(bc>255) bc=255;         
  27.         int r=fc+random.nextInt(bc-fc);         
  28.         int g=fc+random.nextInt(bc-fc);         
  29.         int b=fc+random.nextInt(bc-fc);         
  30.         return new Color(r,g,b);    
  31.     }    
  32.          
  33.     public Map<String, Object> getGraphics(){    
  34.              
  35.         int width=100,height=18;    
  36.         BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);    
  37.         Graphics g=image.getGraphics();    
  38.         Random random = new Random();         
  39.         g.setColor(getRandColor(200,250));         
  40.         g.fillRect(11, width-1, height-1);         
  41.         g.setColor(new Color(102,102,102));         
  42.         g.drawRect(00, width-1, height-1);         
  43.         g.setFont(mFont);         
  44.         g.setColor(getRandColor(160,200));         
  45.         //画随机线         
  46.         for (int i=0;i<155;i++){         
  47.             int x = random.nextInt(width - 1);         
  48.             int y = random.nextInt(height - 1);         
  49.             int xl = random.nextInt(6) + 1;         
  50.             int yl = random.nextInt(12) + 1;         
  51.             g.drawLine(x,y,x + xl,y + yl);         
  52.         }         
  53.             
  54.         //从另一方向画随机线         
  55.         for (int i = 0;i < 70;i++){         
  56.             int x = random.nextInt(width - 1);         
  57.             int y = random.nextInt(height - 1);         
  58.             int xl = random.nextInt(12) + 1;         
  59.             int yl = random.nextInt(6) + 1;         
  60.             g.drawLine(x,y,x - xl,y - yl);         
  61.         }         
  62.             
  63.         //生成随机数,并将随机数字转换为字母         
  64.         String sRand="";         
  65.         for (int i=0;i<6;i++){         
  66.             int itmp = random.nextInt(26) + 65;         
  67.             char ctmp = (char)itmp;         
  68.             sRand += String.valueOf(ctmp);         
  69.             g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));         
  70.             g.drawString(String.valueOf(ctmp),15*i+10,16);         
  71.         }         
  72.             
  73.         g.dispose();    
  74.                 
  75.         Map<String, Object> map=new HashMap<String, Object>();    
  76.         map.put("rand", sRand);    
  77.         map.put("image", image);    
  78.         return map;    
  79.      }    
  80.     
  81. }  


(2).生成验证码action

[java]  view plain  copy
  1.    /**  
  2. * @Description: 获取验证码   
  3. * @author Mr.chen  
  4. * @date 2016-11-2 下午03:45:28  
  5.  */    
  6. public void getCode(){    
  7.     try {    
  8.         HttpServletResponse response = ServletActionContext.getResponse();    
  9.             
  10.         HttpServletRequest request= ServletActionContext.getRequest();    
  11.             
  12.         response.setHeader("Pragma","No-cache");         
  13.         response.setHeader("Cache-Control","no-cache");         
  14.         response.setDateHeader("Expires"0);         
  15.         //表明生成的响应是图片         
  16.         response.setContentType("image/jpeg");    
  17.             
  18.         Map<String, Object> map=new GraphicsUtil().getGraphics();    
  19.         System.out.println(map.get("rand"));    
  20.         request.getSession().setAttribute("rand", map.get("rand"));    
  21.         ImageIO.write((RenderedImage) map.get("image"), "JPEG", response.getOutputStream());    
  22.     } catch (IOException e) {    
  23.         e.printStackTrace();    
  24.     }    
  25. }   

(3).验证用户名是否存在


[java]  view plain  copy
  1.   /**  
  2. * @Description: 发送邮件  
  3. * @author Mr.chen  
  4. * @date 2016-11-2 下午05:06:24  
  5.  */    
  6. @SuppressWarnings("static-access")    
  7. public String toFindPassword2(){    
  8.     HttpServletRequest request= ServletActionContext.getRequest();    
  9.     String usernumber = request.getParameter("usernumber");    
  10.     Studentinfo stu= studentinfoService.getStudentinfoByUsernumber(usernumber);    
  11.     try {    
  12.         Properties prop = new Properties();    
  13.         prop.setProperty("mail.transport.protocol""smtp");    
  14.         prop.setProperty("mail.smtp.host""smtp.qq.com");    
  15.         prop.setProperty("mail.smtp.auth""true");    
  16.         prop.put("mail.smtp.port","587");    
  17.         prop.setProperty("mail.debug""true");    
  18.         //验证写信者邮箱,此处使用第三方授权码登陆,使用密码不知道为什么登录不上    
  19.         Authenticator authenticator = new PopAuthenticator("123456789@qq.com""**************");    
  20.         //创建会话    
  21.         Session session = Session.getInstance(prop,authenticator);    
  22.         //填写信封写信    
  23.         Message msg = new MimeMessage(session);    
  24.         //设置发邮件的原地址    
  25.         msg.setFrom(new InternetAddress("123456789@qq.com"));    
  26.         //设置接收人    
  27.         msg.setRecipient(RecipientType.TO, new InternetAddress(stu.getEmail()));    
  28.         msg.setSubject("找回密码!");    
  29.         msg.setText(this.createLink(stu));    
  30.         //验证用户名密码发送邮件    
  31.         Transport transport = session.getTransport();    
  32.         transport.send(msg);    
  33.         request.setAttribute("stu", stu);       
  34.         return SUCCESS;    
  35.                
  36.        }catch(Exception e){    
  37.         e.printStackTrace();    
  38.        }    
  39.           return ERROR;    
  40. }   

[java]  view plain  copy
  1.    /**  
  2. * @Description: 生成邮箱链接地址  
  3. * @author Mr.chen  
  4. * @date 2016-11-3 下午01:50:14  
  5.  */    
  6. public String createLink(Studentinfo stu){    
  7.         
  8.     //生成密钥    
  9.     String secretKey=UUID.randomUUID().toString();    
  10.     //设置过期时间    
  11.     Date outDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);// 30分钟后过期    
  12.     System.out.println(System.currentTimeMillis());    
  13.     long date = outDate.getTime() / 1000 * 1000;// 忽略毫秒数  mySql 取出时间是忽略毫秒数的    
  14.         
  15.     //此处应该更新Studentinfo表中的过期时间、密钥信息    
  16.     stu.setOutDate(date);    
  17.     stu.setValidataCode(secretKey);    
  18.     studentinfoService.updateStudentinfo(stu);    
  19.     //将用户名、过期时间、密钥生成链接密钥    
  20.     String key =stu.getUsernumber() + "$" + date + "$" + secretKey;    
  21.         
  22.     String digitalSignature = MD5Util.getMd5(key);// 数字签名    
  23.         
  24.     HttpServletRequest request= ServletActionContext.getRequest();    
  25.         
  26.     String path=request.getContextPath();    
  27.         
  28.     String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;    
  29.         
  30.     String resetPassHref = basePath + "/toFindPassword3.action?sid="+ digitalSignature +"&id="+stu.getId();    
  31.         
  32.     String emailContent = "请勿回复本邮件.点击下面的链接,重设密码,本邮件超过30分钟,链接将会失效,需要重新申请找回密码." + resetPassHref;    
  33.         
  34.     return emailContent;    
  35. }   

3.邮件发送成功后进入邮箱,通过该链接进入修改密码请求,链接验证通过后进入修改密码页面


[java]  view plain  copy
  1. /**  
  2.     * @Description: 该方法用于处理从邮箱链接过来的修改密码请求  
  3.     * @author Mr.chen  
  4.     * @date 2016-11-3 下午02:24:17  
  5.      */    
  6.     public String toFindPassword3(){    
  7.         String message="";    
  8.         HttpServletRequest request= ServletActionContext.getRequest();    
  9.         //获取链接中的加密字符串    
  10.         String sid=request.getParameter("sid");    
  11.         //获取链接中的用户名    
  12.         String id=request.getParameter("id");    
  13.         if(StringUtils.isBlank(sid)||StringUtils.isBlank(id)){    
  14.             System.out.println("请求的链接不正确,请重新操作.");    
  15.             message="请求的链接不正确,请重新操作.";    
  16.         }    
  17.         Studentinfo stu=studentinfoService.getStudentinfoById(Long.parseLong(id));    
  18.             
  19.         if(stu!=null){    
  20.             //获取当前用户申请找回密码的过期时间    
  21.             //找回密码链接已经过期    
  22.             if(stu.getOutDate()<=System.currentTimeMillis()){    
  23.                 System.out.println("链接已经过期");    
  24.                 message="链接已经过期";    
  25.             }    
  26.             //获取当前登陆人的加密码    
  27.             String key = stu.getUsernumber()+"$"+stu.getOutDate()/1000*1000+"$"+stu.getValidataCode();//数字签名    
  28.                 
  29.             String digitalSignature = MD5Util.getMd5(key);// 数字签名    
  30.                 
  31.             if(!digitalSignature.equals(sid)){    
  32.                 System.out.println("链接加密密码不正确");    
  33.                 message="链接加密密码不正确";    
  34.             }else{    
  35.                 //验证成功,跳入到修改密码界面    
  36.                 request.setAttribute("stu", stu);    
  37.             }    
  38.                 
  39.         }else{    
  40.             System.out.println("用户信息不存在");    
  41.             message="用户信息不存在";    
  42.             request.setAttribute("message", message);    
  43.         }    
  44.         return SUCCESS;    
  45.     }   



4.输入新密码,验证成功后即修改成功




原文

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值