通过邮件找回密码功能的Java实现

1、有个需求就是,忘记密码后通过邮箱找回。现在的系统在注册的时候都会强制输入邮箱,其一目的就是 通过邮件绑定找回,可以进行密码找回。通过java发送邮件的功能我就不说了,重点讲找回密码。

 

2、参考别人的思路:发送邮件→请求邮件里的URL→验证url→{验证成功修改密码,不成功跳转到失败页面}

重点就是如何生成这个url和如何解析这个url.
需要注意的是一个url只能修改一次密码,当同一帐号发送多封邮件,只有最后一封邮件的url


3、加密能防止伪造攻击,一次url只能验证一次,并且绑定了用户。生成url: 可以用UUID生成随机密钥。

数字签名 = MD5(用户名+'$'+过期时间+‘$’+密钥key)
数据库字段(用户名(主键),密钥key,过期时间)
url参数(用户名,数字签名) ,密钥key的生成:在每一个用户找回密码时候为这个用户生成一个密钥key ,

url example: http://localhost:8080/user/reset_password?sid=D622D6A23FBF86FFE696B593D55351A54AEAEA77&userName=test4

生成过期时间,生成数字签名,生成url,发送邮件.saveOrUpdate(用户名,密钥key,过期时间)

以下为springMvc代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@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;
}



找回链接已经发到邮箱了。进入邮箱点开链接

以下为链接检验代码,验证通过 跳转到修改密码界面,否则跳转到失败界面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
@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" ,msg) ;
            logInfo(userName, "找回密码链接失效" );
            return model;
        }
        Timestamp outDate = users.getRegisterDate();
        if (outDate.getTime() <= System.currentTimeMillis()){          //表示已经过期
            msg =  "链接已经过期,请重新申请找回密码." ;
            model.addObject( "msg" ,msg) ;
            logInfo(userName, "找回密码链接失效" );
            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" ,msg) ;
            logInfo(userName, "找回密码链接失效" );
            return model;
        }
        model.setViewName( "user/reset_password" );   //返回到修改密码的界面
        model.addObject( "userName" ,userName);
        return model;
    }


补充1:Timestamp类型对象在保存到数据的时候 毫秒精度会丢失。比如:2013-10-08 10:29:10.234 存到mysql数据库的时候 变成 2013-10-08 10:29:10.0。时间变得不相同了,sid 匹配的时候不会相等。 所以我做了忽略精度的操作。

补充2:解决linux下面title中文乱码

?
1
2
sun.misc.BASE64Encoder enc =  new sun.misc.BASE64Encoder();
mailMessage.setSubject(MimeUtility.encodeText(mailInfo.getSubject(),  "UTF-8" "B" ));       //解决linux邮件title乱码

补充3:怎么不直接把sid插入到user表呢。验证的时候直接比较sid就ok了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值