亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java實現(xiàn)通過綁定郵箱找回密碼功能

 更新時間:2019年02月10日 10:35:11   作者:親昵YY  
這篇文章主要為大家詳細介紹了java實現(xiàn)通過綁定郵箱找回密碼功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java實現(xiàn)通過綁定郵箱找回密碼功能,供大家參考,具體內(nèi)容如下

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); 
 } 
 
 //生成隨機數(shù),并將隨機數(shù)字轉(zhuǎn)換為字母 
 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); 
 //表明生成的響應(yīng)是圖片 
 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.用戶名驗證通過后往綁定郵箱發(fā)送郵件

/**
 * @Description: 發(fā)送郵件
 * @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");
 //驗證寫信者郵箱,此處使用第三方授權(quán)碼登陸,使用密碼不知道為什么登錄不上
 Authenticator authenticator = new PopAuthenticator("123456789@qq.com", "**************");
 //創(chuàng)建會話
 Session session = Session.getInstance(prop,authenticator);
 //填寫信封寫信
 Message msg = new MimeMessage(session);
 //設(shè)置發(fā)郵件的原地址
 msg.setFrom(new InternetAddress("123456789@qq.com"));
 //設(shè)置接收人
 msg.setRecipient(RecipientType.TO, new InternetAddress(stu.getEmail()));
 msg.setSubject("找回密碼!");
 msg.setText(this.createLink(stu));
 //驗證用戶名密碼發(fā)送郵件
 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();
 //設(shè)置過期時間
 Date outDate = new Date(System.currentTimeMillis() + 30 * 60 * 1000);// 30分鐘后過期
 System.out.println(System.currentTimeMillis());
 long date = outDate.getTime() / 1000 * 1000;// 忽略毫秒數(shù) mySql 取出時間是忽略毫秒數(shù)的
 
 //此處應(yīng)該更新Studentinfo表中的過期時間、密鑰信息
 stu.setOutDate(date);
 stu.setValidataCode(secretKey);
 studentinfoService.updateStudentinfo(stu);
 //將用戶名、過期時間、密鑰生成鏈接密鑰
 String key =stu.getUsernumber() + "$" + date + "$" + secretKey;
 
 String digitalSignature = MD5Util.getMd5(key);// 數(shù)字簽名
 
 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 = "請勿回復(fù)本郵件.點擊下面的鏈接,重設(shè)密碼,本郵件超過30分鐘,鏈接將會失效,需要重新申請找回密碼." + resetPassHref;
 
 return emailContent;
 }

3.郵件發(fā)送成功后進入郵箱,通過該鏈接進入修改密碼請求,鏈接驗證通過后進入修改密碼頁面

/**
 * @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){
 //獲取當前用戶申請找回密碼的過期時間
 //找回密碼鏈接已經(jīng)過期
 if(stu.getOutDate()<=System.currentTimeMillis()){
 System.out.println("鏈接已經(jīng)過期");
 message="鏈接已經(jīng)過期";
 }
 //獲取當前登陸人的加密碼
 String key = stu.getUsernumber()+"$"+stu.getOutDate()/1000*1000+"$"+stu.getValidataCode();//數(shù)字簽名
 
 String digitalSignature = MD5Util.getMd5(key);// 數(shù)字簽名
 
 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.輸入新密碼,驗證成功后即修改成功

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java正則表達式之Pattern和Matcher的使用

    Java正則表達式之Pattern和Matcher的使用

    本文詳細介紹了Java中處理正則表達式的Pattern和Matcher類的使用方法和實例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-09-09
  • 詳解Java的設(shè)計模式編程中的原型模式

    詳解Java的設(shè)計模式編程中的原型模式

    這篇文章主要介紹了Java的設(shè)計模式編程中的原型模式,處理對象復(fù)制時要特別注意淺拷貝和深拷貝的問題,需要的朋友可以參考下
    2016-02-02
  • java中Vector類的常用方法詳解

    java中Vector類的常用方法詳解

    這篇文章主要為大家詳細介紹了java中Vector類的常用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Java多線程中的CyclicBarrier使用方法詳解

    Java多線程中的CyclicBarrier使用方法詳解

    這篇文章主要介紹了Java多線程中的CyclicBarrier使用方法詳解,CyclicBarrier是一種同步輔助工具,它允許一組線程都等待對方到達公共障礙點,在涉及固定大小的線程的程序中,CyclicBarriers非常有用,這些線程間必須相互等待,需要的朋友可以參考下
    2023-12-12
  • Mac下設(shè)置Java默認版本的方法

    Mac下設(shè)置Java默認版本的方法

    今天工作的時候發(fā)現(xiàn)了一個錯誤,提示java版本太低,無法啟動!想起自己裝過高版本的Java,但是卻沒有默認啟動,從網(wǎng)上找了一些資料,整理下現(xiàn)在分享給大家,有需要的可以參考借鑒。
    2016-10-10
  • Springboot整合JwtHelper實現(xiàn)非對稱加密

    Springboot整合JwtHelper實現(xiàn)非對稱加密

    本文主要介紹了Springboot整合JwtHelper實現(xiàn)非對稱加密,主要介紹兩種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-03-03
  • JAVA下單接口優(yōu)化實戰(zhàn)TPS性能提高10倍

    JAVA下單接口優(yōu)化實戰(zhàn)TPS性能提高10倍

    今天小編就為大家分享一篇關(guān)于JAVA下單接口優(yōu)化實戰(zhàn)TPS性能提高10倍,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • Java 深入探討設(shè)計模式之原型模式篇

    Java 深入探討設(shè)計模式之原型模式篇

    設(shè)計模式(Design pattern)是一套被反復(fù)使用、多數(shù)人知曉的、經(jīng)過分類編目的、代碼設(shè)計經(jīng)驗的總結(jié)。使用設(shè)計模式是為了可重用代碼、讓代碼更容易被他人理解、保證代碼可靠性
    2021-10-10
  • Jmail發(fā)送郵件工具類分享

    Jmail發(fā)送郵件工具類分享

    這篇文章主要為大家分享了Jmail發(fā)送郵件工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • idea設(shè)置@Author文件頭注釋的實現(xiàn)步驟

    idea設(shè)置@Author文件頭注釋的實現(xiàn)步驟

    本文主要介紹了idea設(shè)置@Author文件頭注釋的實現(xiàn)步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07

最新評論