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

 更新时间:2019年02月10日 10:35:11   作者:亲昵YY  
这篇文章主要为大家详细介绍了java实现通过绑定邮箱找回密码功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java实现通过绑定邮箱找回密码功能,供大家参考,具体内容如下

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

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

package com.utils;
 
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
 
/** 
 * @Title: GraphicsUtil.java
 * @copyright 
 * @Package com.utils
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author Mr.chen
 * @date 2016-11-2 下午03:31:30
 */
public class GraphicsUtil {
 
 private Font mFont = new Font("Times New Roman", Font.PLAIN, 17); 
 
 Color getRandColor(int fc,int bc){
 Random random = new Random(); 
 if(fc>255) fc=255; 
 if(bc>255) bc=255; 
 int r=fc+random.nextInt(bc-fc); 
 int g=fc+random.nextInt(bc-fc); 
 int b=fc+random.nextInt(bc-fc); 
 return new Color(r,g,b);
 }
 
 public Map<String, Object> getGraphics(){
 
 int width=100,height=18;
 BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
 Graphics g=image.getGraphics();
 Random random = new Random(); 
 g.setColor(getRandColor(200,250)); 
 g.fillRect(1, 1, width-1, height-1); 
 g.setColor(new Color(102,102,102)); 
 g.drawRect(0, 0, width-1, height-1); 
 g.setFont(mFont); 
 g.setColor(getRandColor(160,200)); 
 //画随机线 
 for (int i=0;i<155;i++){ 
 int x = random.nextInt(width - 1); 
 int y = random.nextInt(height - 1); 
 int xl = random.nextInt(6) + 1; 
 int yl = random.nextInt(12) + 1; 
 g.drawLine(x,y,x + xl,y + yl); 
 } 
 
 //从另一方向画随机线 
 for (int i = 0;i < 70;i++){ 
 int x = random.nextInt(width - 1); 
 int y = random.nextInt(height - 1); 
 int xl = random.nextInt(12) + 1; 
 int yl = random.nextInt(6) + 1; 
 g.drawLine(x,y,x - xl,y - yl); 
 } 
 
 //生成随机数,并将随机数字转换为字母 
 String sRand=""; 
 for (int i=0;i<6;i++){ 
 int itmp = random.nextInt(26) + 65; 
 char ctmp = (char)itmp; 
 sRand += String.valueOf(ctmp); 
 g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); 
 g.drawString(String.valueOf(ctmp),15*i+10,16); 
 } 
 
 g.dispose();
 
 Map<String, Object> map=new HashMap<String, Object>();
 map.put("rand", sRand);
 map.put("image", image);
 return map;
 }
 
}

(2).生成验证码action

/**
 * @Description: 获取验证码 
 * @author Mr.chen
 * @date 2016-11-2 下午03:45:28
 */
 public void getCode(){
 try {
 HttpServletResponse response = ServletActionContext.getResponse();
 
 HttpServletRequest request= ServletActionContext.getRequest();
 
 response.setHeader("Pragma","No-cache"); 
 response.setHeader("Cache-Control","no-cache"); 
 response.setDateHeader("Expires", 0); 
 //表明生成的响应是图片 
 response.setContentType("image/jpeg");
 
 Map<String, Object> map=new GraphicsUtil().getGraphics();
 System.out.println(map.get("rand"));
 request.getSession().setAttribute("rand", map.get("rand"));
 ImageIO.write((RenderedImage) map.get("image"), "JPEG", response.getOutputStream());
 } catch (IOException e) {
 e.printStackTrace();
 }
 }
(3).验证用户名是否存在
 /**
 * @Description: 检查用户名是否存在
 * @author Mr.chen
 * @date 2016-11-2 下午04:49:02
 */
 public void checkUsernumber(){
 try {
 HttpServletResponse response = ServletActionContext.getResponse();
 
 HttpServletRequest request= ServletActionContext.getRequest();
 String usernumber = request.getParameter("usernumber");
 Studentinfo stu= studentinfoService.getStudentinfoByUsernumber(usernumber);
 response.setContentType("text/plain; charset=utf-8");
 response.setCharacterEncoding("UTF-8");
 if(stu==null){
 response.getWriter().print("false");
 
 }else{
 if(!StringUtils.isBlank(stu.getEmail())){
 response.getWriter().print("true");
 }else{
 response.getWriter().print("notEmail");
 }
 
 }
 response.getWriter().flush();
 response.getWriter().close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 }

2.用户名验证通过后往绑定邮箱发送邮件

/**
 * @Description: 发送邮件
 * @author Mr.chen
 * @date 2016-11-2 下午05:06:24
 */
 @SuppressWarnings("static-access")
 public String toFindPassword2(){
 HttpServletRequest request= ServletActionContext.getRequest();
 String usernumber = request.getParameter("usernumber");
 Studentinfo stu= studentinfoService.getStudentinfoByUsernumber(usernumber);
 try {
 Properties prop = new Properties();
 prop.setProperty("mail.transport.protocol", "smtp");
 prop.setProperty("mail.smtp.host", "smtp.qq.com");
 prop.setProperty("mail.smtp.auth", "true");
 prop.put("mail.smtp.port","587");
 prop.setProperty("mail.debug", "true");
 //验证写信者邮箱,此处使用第三方授权码登陆,使用密码不知道为什么登录不上
 Authenticator authenticator = new PopAuthenticator("123456789@qq.com", "**************");
 //创建会话
 Session session = Session.getInstance(prop,authenticator);
 //填写信封写信
 Message msg = new MimeMessage(session);
 //设置发邮件的原地址
 msg.setFrom(new InternetAddress("123456789@qq.com"));
 //设置接收人
 msg.setRecipient(RecipientType.TO, new InternetAddress(stu.getEmail()));
 msg.setSubject("找回密码!");
 msg.setText(this.createLink(stu));
 //验证用户名密码发送邮件
 Transport transport = session.getTransport();
 transport.send(msg);
 request.setAttribute("stu", stu); 
 return SUCCESS;
 
 }catch(Exception e){
 e.printStackTrace();
 }
 return ERROR;
 }
/**
 * @Description: 生成邮箱链接地址
 * @author Mr.chen
 * @date 2016-11-3 下午01:50:14
 */
 public String createLink(Studentinfo stu){
 
 //生成密钥
 String secretKey=UUID.randomUUID().toString();
 //设置过期时间
 Date outDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);// 30分钟后过期
 System.out.println(System.currentTimeMillis());
 long date = outDate.getTime() / 1000 * 1000;// 忽略毫秒数 mySql 取出时间是忽略毫秒数的
 
 //此处应该更新Studentinfo表中的过期时间、密钥信息
 stu.setOutDate(date);
 stu.setValidataCode(secretKey);
 studentinfoService.updateStudentinfo(stu);
 //将用户名、过期时间、密钥生成链接密钥
 String key =stu.getUsernumber() + "$" + date + "$" + secretKey;
 
 String digitalSignature = MD5Util.getMd5(key);// 数字签名
 
 HttpServletRequest request= ServletActionContext.getRequest();
 
 String path=request.getContextPath();
 
 String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
 
 String resetPassHref = basePath + "/toFindPassword3.action?sid="+ digitalSignature +"&id="+stu.getId();
 
 String emailContent = "请勿回复本邮件.点击下面的链接,重设密码,本邮件超过30分钟,链接将会失效,需要重新申请找回密码." + resetPassHref;
 
 return emailContent;
 }

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

/**
 * @Description: 该方法用于处理从邮箱链接过来的修改密码请求
 * @author Mr.chen
 * @date 2016-11-3 下午02:24:17
 */
 public String toFindPassword3(){
 String message="";
 HttpServletRequest request= ServletActionContext.getRequest();
 //获取链接中的加密字符串
 String sid=request.getParameter("sid");
 //获取链接中的用户名
 String id=request.getParameter("id");
 if(StringUtils.isBlank(sid)||StringUtils.isBlank(id)){
 System.out.println("请求的链接不正确,请重新操作.");
 message="请求的链接不正确,请重新操作.";
 }
 Studentinfo stu=studentinfoService.getStudentinfoById(Long.parseLong(id));
 
 if(stu!=null){
 //获取当前用户申请找回密码的过期时间
 //找回密码链接已经过期
 if(stu.getOutDate()<=System.currentTimeMillis()){
 System.out.println("链接已经过期");
 message="链接已经过期";
 }
 //获取当前登陆人的加密码
 String key = stu.getUsernumber()+"$"+stu.getOutDate()/1000*1000+"$"+stu.getValidataCode();//数字签名
 
 String digitalSignature = MD5Util.getMd5(key);// 数字签名
 
 if(!digitalSignature.equals(sid)){
 System.out.println("链接加密密码不正确");
 message="链接加密密码不正确";
 }else{
 //验证成功,跳入到修改密码界面
 request.setAttribute("stu", stu);
 }
 
 }else{
 System.out.println("用户信息不存在");
 message="用户信息不存在";
 request.setAttribute("message", message);
 }
 return SUCCESS;
 }

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java正则表达式之Pattern和Matcher的使用

    Java正则表达式之Pattern和Matcher的使用

    本文详细介绍了Java中处理正则表达式的Pattern和Matcher类的使用方法和实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-09-09
  • 详解Java的设计模式编程中的原型模式

    详解Java的设计模式编程中的原型模式

    这篇文章主要介绍了Java的设计模式编程中的原型模式,处理对象复制时要特别注意浅拷贝和深拷贝的问题,需要的朋友可以参考下
    2016-02-02
  • java中Vector类的常用方法详解

    java中Vector类的常用方法详解

    这篇文章主要为大家详细介绍了java中Vector类的常用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-02-02
  • Java多线程中的CyclicBarrier使用方法详解

    Java多线程中的CyclicBarrier使用方法详解

    这篇文章主要介绍了Java多线程中的CyclicBarrier使用方法详解,CyclicBarrier是一种同步辅助工具,它允许一组线程都等待对方到达公共障碍点,在涉及固定大小的线程的程序中,CyclicBarriers非常有用,这些线程间必须相互等待,需要的朋友可以参考下
    2023-12-12
  • Mac下设置Java默认版本的方法

    Mac下设置Java默认版本的方法

    今天工作的时候发现了一个错误,提示java版本太低,无法启动!想起自己装过高版本的Java,但是却没有默认启动,从网上找了一些资料,整理下现在分享给大家,有需要的可以参考借鉴。
    2016-10-10
  • Springboot整合JwtHelper实现非对称加密

    Springboot整合JwtHelper实现非对称加密

    本文主要介绍了Springboot整合JwtHelper实现非对称加密,主要介绍两种方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-03-03
  • JAVA下单接口优化实战TPS性能提高10倍

    JAVA下单接口优化实战TPS性能提高10倍

    今天小编就为大家分享一篇关于JAVA下单接口优化实战TPS性能提高10倍,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-12-12
  • Java 深入探讨设计模式之原型模式篇

    Java 深入探讨设计模式之原型模式篇

    设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性
    2021-10-10
  • Jmail发送邮件工具类分享

    Jmail发送邮件工具类分享

    这篇文章主要为大家分享了Jmail发送邮件工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • idea设置@Author文件头注释的实现步骤

    idea设置@Author文件头注释的实现步骤

    本文主要介绍了idea设置@Author文件头注释的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07

最新评论