<main>
<div class="container">
<form class="ma" action="forgetpwd.do" method="post" >
<div class="form_header">
<div class="form_title">
<h2>忘记密码h2>
<span>通过注册邮箱重设密码span>
div>
<div class="form_back">
<a href="index.do">返回立即登录a>
div>
div>
<div class="form_body">
<input type="email" placeholder="请输入登录邮箱" name="email" id="email">
<input type="text" placeholder="请输入验证码" name="captcha"><input type="button" value="发邮件获取验证码" onclick="submitEmail();">
<input type="submit" value="提交">
div>
<div class="form_footer">
<div class="FAQ">
<span>收不到邮件?查看span><a href="#">常见问题a>
div>
div>
form>
div>
main>
<%@include file="../include/script.html"%>
<script type="text/javascript">
function submitEmail(){
var email = $('#email').val();
//改为ajax提交邮箱
if(email!=null&&email!=''){
$.post('sendemail.do',{email:email},function(data){
console.log(data);
if(data.success){
alert('验证码已发送到邮箱,请注意查收');
}else{
alert('验证码发送失败:'+data.message);
}
},'json');
}
}
script>
其中抽出来了需要引入的js
文档结构:(/video/WebContent/WEB-INF/jsp/front/include/script.html)
<script src="static/js/jquery-1.12.4.min.js">script>
<script src="static/js/jquery.validate.min.js">script>
<script src="static/js/jquery-confirm.min.js">script>
<script src="static/js/common.js">script>
<main>
<div class="container">
<form class="ma" action="resetpwd.do" method="post">
<input type="hidden" name="email" value="${email}"/>
<input type="hidden" name="captcha" value="${captcha}"/>
<div class="form_header">
<div class="form_title">
<h2>重置密码h2>
div>
div>
<div class="form_body">
<input type="password" placeholder="请输入新密码" id="password" name="password">
<input type="password" style="width:100%" placeholder="再次输入新密码" id="password02" name="pwdAgain">
<input type="submit" style="margin:0px;width:100%" value="提交">
div>
form>
div>
main>
/**
* Created By gf on 2017年7月18日
* Descr: 负责前台首页的跳转
* */
@Controller
public class FrontHomeController extends FrontBaseController{
@Autowired
IFrontUserService userService;
/**
* 首页跳转
* Created by gf on 2017年7月19日
*
* @return
*/
@RequestMapping("/index.do")
public String index(){
return "front/index";
}
/**
* 跳到找回密码页面,包括发邮件获取验证码、重置密码
* Created by gf on 2017年7月19日
*
* @return
*/
@RequestMapping(value="forgetpwd.do",method=RequestMethod.GET)
public String findPassword(){
return "front/user/forget_pwd";
}
/**
* 请求发送邮件,目前来说只有找回密码有发送邮件功能,如果多个功能都有,就要考虑复用性和url名字可识性
* Created by gf on 2017年7月19日
* 改为ajax方式 2017-07-25
* @param email
* @return
*/
@ResponseBody
@RequestMapping(value="sendemail.do",method=RequestMethod.POST)
public Map sendEmail(String email){
HashMap map = new HashMap();
//根据给定的邮箱发送邮件
UserModel user = userService.queryUserByEmail(email);
if(user==null){
//用户为空,说明填写的邮箱就不对
map.put("success", false);
map.put("message","邮箱账号不存在");
return map;
}
//先生成验证码,先发送邮件,发送成功后保存验证码
String captcha = RandomCode.getRandomCode(CAPTCHA_LENGTH);
//先发送验证码,发送成功了再保存到数据库
boolean b = SendEmailUtil.sendPasswordCaptchaEmail(email,captcha);
if(b){
//发送成功,保存
map.put("success", true);
map.put("message", "验证码已发送到您的邮箱,请注意查收");
userService.updateUserCaptcha(email,captcha);
}else{
//发送失败,提醒
map.put("success", false);
map.put("message","验证码发送失败,请检查邮箱账号");
}
return map;
}
/**
* 提交邮箱和收到的验证码信息
* Created by gf on 2017年7月19日
*
* @return
*/
@RequestMapping(value="forgetpwd.do",method=RequestMethod.POST)
public String findPasswordPost(String email,String captcha,Model model){
//校验验证码和邮箱信息是否有效
UserModel user = userService.queryUserByEmail(email);
if(user==null){
//用户为空,说明填写的邮箱就不对
model.addAttribute("message","邮箱账号不存在");
}else{
if(captcha!=null&&!captcha.equals("")&&captcha.equals(user.getCaptcha())){
//验证成功了,返回重置密码的页面
model.addAttribute("email", email);
model.addAttribute("captcha", captcha);
return "front/user/reset_pwd";
}else{
//验证码不对或验证码不存在
model.addAttribute("message","验证码不正确或不存在");
}
}
return "front/user/forget_pwd";
}
/**
* 提交重置密码请求,包括新密码、邮箱接收的验证码等
* Created by gf on 2017年7月19日
*
* @return
*/
@RequestMapping(value="resetpwd.do",method=RequestMethod.POST)
public String resetPassword(ResetPwdInfo info,Model model){
//重置密码
//先检查提交的邮箱和验证码信息是否有效,和上一步的验证一样,然后再去更新此邮箱的用户密码
//这里都省略了数据校验
//校验验证码和邮箱信息是否有效
UserModel user = userService.queryUserByEmail(info.getEmail());
if(user==null){
//用户为空,说明填写的邮箱就不对
model.addAttribute("message","邮箱账号不存在");
}else{
String captcha = info.getCaptcha();
if(captcha!=null&&!captcha.equals("")&&captcha.equals(user.getCaptcha())){
//验证成功了,返回重置密码的页面
//验证成功了,更新密码,然后跳转到首页,重新让用户登录
userService.updateUserPwd(user.getId(),MD5Utils.getMd5Simple(info.getPassword()));
//删除验证码
userService.cleanCaptcha(user.getEmail());
model.addAttribute("message", "密码更新成功了,请重新登录");
// return "front/message";
//跳到首页了,其实应该用ajax,返回个更新成功的消息的
return "redirect:/index.do";
}else{
//验证码不对或验证码不存在
model.addAttribute("message","验证码不正确或不存在");
}
}
return "front/user/forget_pwd";
}
}
package com.xxxxxx.video.controller;
import javax.servlet.http.HttpSession;
import com.xxxxxx.video.model.UserModel;
/**
* Created By gf on 2017年7月19日
* Descr: 放置一些前台控制器公用的功能
*
*/
public class FrontBaseController {
static final String SESSION_USER = "_front_user";
static final int CAPTCHA_LENGTH = 5;//验证码长度
/**
* 用户登陆后,将用户信息放到session中,但是哪些信息要放,可以在这里统一管理
* Created by gf on 2017年7月19日
*
* @param session
* @param user
*/
public void loginSession(HttpSession session,UserModel user){
session.setAttribute(SESSION_USER, user);
}
/**
* 获取当前已登录用户信息
* Created by gf on 2017年7月20日
*
* @param session
* @return
*/
public UserModel getCurrentUser(HttpSession session){
return (UserModel)session.getAttribute(SESSION_USER);
}
/**
* 用户退出登录时,需要从session中移除哪些信息,也可以统一在这里管理
* Created by gf on 2017年7月19日
*
* @param session
*/
public void logoutSession(HttpSession session){
session.removeAttribute(SESSION_USER);
}
}
package com.xxxxxx.video.util;
import org.springframework.util.DigestUtils;
/**
* MD5加密工具类
* @author Administrator
*
*/
public class MD5Utils {
/*
* 将一个字符串MD5加密,方式很多,我们使用的是Spring包下
*/
public static String getMd5Simple(String password){
String md502 =DigestUtils.md5DigestAsHex(password.getBytes());
return md502;
}
public static void main(String[] args){
System.out.println(getMd5Simple("111111"));
}
}
package com.xxxxxx.video.util;
import java.util.Date;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* Created By gf on 2017年7月19日
* Descr: 最终进行发送邮件的代码部分,如果对过程不了解,参考MailTest.java文件
*
*/
public class Mail {
private static String myEmailAccount = "@qq.com";//修改为自己的qq邮箱账号
private static String myEmailPassword = "";//修改为自己的邮箱授权码
private static String myEmailSMTPHost = "smtp.qq.com";//如果用qq邮箱不用修改,如果用163邮箱,修改为smtp.163.com
private static String sendUser = "Video视频系统";
public static void send(String receive,String title,String body) throws Exception {
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", myEmailSMTPHost);
props.setProperty("mail.smtp.auth", "true");
final String smtpPort = "465";
props.setProperty("mail.smtp.port", smtpPort);
props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
Session session = Session.getDefaultInstance(props);
session.setDebug(true);
MimeMessage message = createMimeMessage(session, myEmailAccount, receive,title,body);
Transport transport = session.getTransport();
transport.connect(myEmailAccount, myEmailPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
* 创建邮件对象
* Created by gf on 2017年7月19日
*
* @param session
* @param sendMail
* @param receiveMail
* @param title
* @param body
* @return
* @throws Exception
*/
private static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail,String title,String body) throws Exception {
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人
message.setFrom(new InternetAddress(sendMail, sendUser, "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, "尊敬的用户", "UTF-8"));
// 4. Subject: 邮件主题
message.setSubject(title, "UTF-8");
// 5. Content: 邮件正文(可以使用html标签)
message.setContent(body, "text/html;charset=UTF-8");
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
return message;
}
}
package com.xxxxxx.video.util;
/**
* Created By gf on 2017年7月19日
* Descr: 发送邮件的工具类
*
*/
public class SendEmailUtil {
/**
* 发送邮件
* Created by gf on 2017年7月19日
*
* @param receiveEmail 收件人邮箱地址
* @param emailTitle 邮件标题
* @param emailBody 邮件正文
* @return
*/
public static boolean sendEmail(String receiveEmail,String emailTitle,String emailBody){
System.out.println(">>>开发发送邮件:\n"+receiveEmail+"\n"+emailTitle+"\n"+emailBody+"\n");
try {
Mail.send(receiveEmail, emailTitle, emailBody);
} catch (Exception e) {
e.printStackTrace();
System.out.println("发送邮件出错了!!");
return false;
}
return true;
}
/**
* 发送找回密码验证码的邮件
* Created by gf on 2017年7月19日
*
* @param receiveEmail
* @param validateCode
* @return
*/
public static boolean sendPasswordCaptchaEmail(String receiveEmail,String validateCode){
return sendEmail(receiveEmail, "找回密码-验证码", "您正在进行找回密码操作,当前验证码为:"+validateCode);
}
}
service mapper 省略
<mapper namespace="com.xxxxxx.video.mapper.FrontUserMapper">
<resultMap type="UserModel" id="userMap">
<id property="id" column="id"/>
<result property="nickName" column="nick_name"/>
<result property="sex" column="sex"/>
<result property="birthday" column="birthday"/>
<result property="birthdayStr" column="birthday_str"/>
<result property="email" column="email"/>
<result property="province" column="province"/>
<result property="city" column="city"/>
<result property="headUrl" column="head_url"/>
<result property="password" column="password"/>
<result property="insertTime" column="insert_time"/>
<result property="updateTime" column="update_time"/>
<result property="captcha" column="captcha"/>
resultMap>
<select id="queryUserByEmail" resultMap="userMap">
select *,date_format(birthday,'%Y-%m-%d') as birthday_str from user where email=#{email}
select>
<insert id="insertUserModel" parameterType="UserModel" useGeneratedKeys="true" keyProperty="id">
insert into user(nick_name,password,sex,birthday,province,city,head_url,insert_time,update_time,email)
values(#{nickName},#{password},#{sex},#{birthday},#{province},#{city},#{headUrl},#{insertTime},#{updateTime},#{email})
insert>
<update id="updateUserInfo" parameterType="UserModel">
update user set nick_name=#{nickName},sex=#{sex},birthday=#{birthday},province=#{province},
city=#{city},update_time=#{updateTime}
where id=#{id}
update>
<select id="queryUserById" resultMap="userMap">
select *,date_format(birthday,'%Y-%m-%d') as birthday_str from user where id=#{id}
select>
<update id="updateCaptcha" parameterType="java.util.HashMap">
update user set captcha=#{captcha} where email=#{email}
update>
<update id="updateUserPwd" parameterType="java.util.HashMap">
update user set password=#{pwd} where id=#{id}
update>
<update id="updateUserHead" parameterType="java.util.HashMap">
update user set head_url=#{head} where id=#{id}
update>
mapper>