Commit c815e3f8 by 唐功亮

【新增】 用户密码找回

parent be93c5c5
......@@ -265,6 +265,13 @@
<version>2.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
</dependencies>
<build>
......
package io.office.modules.manage.controller;
import java.util.Arrays;
import java.util.Map;
import io.office.modules.app.annotation.Login;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.office.modules.manage.entity.TimescodeFindpsEntity;
import io.office.modules.manage.service.TimescodeFindpsService;
import io.office.common.utils.PageUtils;
import io.office.common.utils.R;
/**
* 忘记密码
*
* @author wudi
* @email
* @date 2021-12-09 17:41:41
*/
@RestController
@RequestMapping("/timescodefindps")
public class TimescodeFindpsController {
@Autowired
private TimescodeFindpsService timescodeFindpsService;
/**
*忘记密码(手机号找回)
*/
@RequestMapping("api/forgotPasswordPhone")
@Login
// @RequiresPermissions("manage:timescodefindps:list")
public R forgotPasswordPhone(@RequestBody Map<String, Object> params){
timescodeFindpsService.forgotPasswordPhone(params);
return R.ok();
}
/**
*忘记密码(邮箱找回)
*/
@RequestMapping("api/forgotPasswordEmiail")
@Login
// @RequiresPermissions("manage:timescodefindps:list")
public R forgotPasswordEmiail(@RequestBody Map<String, Object> params){
timescodeFindpsService.forgotPasswordEmiail(params);
return R.ok();
}
/**
* 列表
*/
@RequestMapping("/list")
// @RequiresPermissions("manage:timescodefindps:list")
public R list(@RequestParam Map<String, Object> params){
PageUtils page = timescodeFindpsService.queryPage(params);
return R.ok().put("page", page);
}
/**
* 信息
*/
@RequestMapping("/info/{phone}")
// @RequiresPermissions("manage:timescodefindps:info")
public R info(@PathVariable("phone") String phone){
TimescodeFindpsEntity timescodeFindps = timescodeFindpsService.getById(phone);
return R.ok().put("timescodeFindps", timescodeFindps);
}
/**
* 保存
*/
@RequestMapping("/save")
// @RequiresPermissions("manage:timescodefindps:save")
public R save(@RequestBody TimescodeFindpsEntity timescodeFindps){
timescodeFindpsService.save(timescodeFindps);
return R.ok();
}
/**
* 修改
*/
@RequestMapping("/update")
// @RequiresPermissions("manage:timescodefindps:update")
public R update(@RequestBody TimescodeFindpsEntity timescodeFindps){
timescodeFindpsService.updateById(timescodeFindps);
return R.ok();
}
/**
* 删除
*/
@RequestMapping("/delete")
// @RequiresPermissions("manage:timescodefindps:delete")
public R delete(@RequestBody String[] phones){
timescodeFindpsService.removeByIds(Arrays.asList(phones));
return R.ok();
}
}
......@@ -5,6 +5,7 @@ import io.office.modules.manage.entity.MemberEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.data.repository.query.Param;
/**
......@@ -19,4 +20,7 @@ public interface MemberDao extends BaseMapper<MemberEntity> {
@Select("select * from member where username=#{userName}")
MemberEntity selectByName(@Param("userName") String userName);
@Update("UPDATE member SET password = #{passWord} WHERE id = #{id}")
void updateByIdPassWord(@Param("id") Integer id,@Param("passWord") String passWord);
}
package io.office.modules.manage.dao;
import io.office.modules.manage.entity.TimescodeFindpsEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-09 17:41:41
*/
@Mapper
public interface TimescodeFindpsDao extends BaseMapper<TimescodeFindpsEntity> {
}
package io.office.modules.manage.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-09 17:41:41
*/
@Data
@TableName("timescode_findps")
public class TimescodeFindpsEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* $column.comments
*/
@TableId(type = IdType.INPUT)
private String phone;
/**
* $column.comments
*/
private String date;
/**
* $column.comments
*/
private Integer times;
public TimescodeFindpsEntity(String phone, String date, Integer times) {
this.phone = phone;
this.date = date;
this.times = times;
}
}
package io.office.modules.manage.service;
import com.baomidou.mybatisplus.extension.service.IService;
import io.office.common.utils.PageUtils;
import io.office.modules.manage.entity.TimescodeFindpsEntity;
import java.util.Map;
/**
* ${comments}
*
* @author wudi
* @email
* @date 2021-12-09 17:41:41
*/
public interface TimescodeFindpsService extends IService<TimescodeFindpsEntity> {
PageUtils queryPage(Map<String, Object> params);
void forgotPasswordPhone(Map<String, Object> params);
void forgotPasswordEmiail(Map<String, Object> params);
}
package io.office.modules.manage.service.impl;
import io.office.common.exception.RRException;
import io.office.modules.manage.dao.MemberDao;
import io.office.modules.manage.entity.MemberEntity;
import io.office.modules.manage.utils.*;
import org.apache.commons.httpclient.NameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.office.common.utils.PageUtils;
import io.office.common.utils.Query;
import io.office.modules.manage.dao.TimescodeFindpsDao;
import io.office.modules.manage.entity.TimescodeFindpsEntity;
import io.office.modules.manage.service.TimescodeFindpsService;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
@Service("timescodeFindpsService")
public class TimescodeFindpsServiceImpl extends ServiceImpl<TimescodeFindpsDao, TimescodeFindpsEntity> implements TimescodeFindpsService {
@Autowired
private TimescodeFindpsDao tFindpsDao;
@Autowired
private MemberDao memberDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<TimescodeFindpsEntity> page = this.page(
new Query<TimescodeFindpsEntity>().getPage(params),
new QueryWrapper<TimescodeFindpsEntity>()
);
return new PageUtils(page);
}
@Override
public void forgotPasswordPhone(Map<String, Object> params) {
try {
String state="";
String userName = params.get("userName")==null?"":String.valueOf(params.get("userName"));
String phone = params.get("phone")==null?"":String.valueOf(params.get("phone"));
if ("".equals(userName)){
throw new RRException("请输入用户名!");
}
if ("".equals(phone)){
throw new RRException("请输入手机号!");
}
if ("admin".equals(userName)){ //管理员不能修改密码
throw new RRException("用户名不存在!");
}
MemberEntity memberEntity = memberDao.selectByName(userName);
if (memberEntity!=null){
Integer id = memberEntity.getId();
String phone_DB = memberEntity.getPhone();
if (phone.equals(phone_DB)){
//获取6位随机字符串
String uuid = UUID.randomUUID().toString().replaceAll("-", "").substring(0,6);
//更换密码
memberDao.updateByIdPassWord(id,MD5Util.md5Encrypt32Upper(uuid));
///////// 以下添加短信发送次数验证
int flag = 1;
TimescodeFindpsEntity timescodeFindpsEntity = tFindpsDao.selectById(phone);
if (timescodeFindpsEntity!=null){
Integer times = timescodeFindpsEntity.getTimes();
String date = timescodeFindpsEntity.getDate();
String date_1 = DateUtils.getDay();
if (date_1.equals(date)){
times = times + 1;
date = date_1;
tFindpsDao.updateById(new TimescodeFindpsEntity(phone,date,times));
if (times <= 3){
flag = 1;
}else{
flag = 0;
}
}else {
times = 1;
date = date_1;
tFindpsDao.updateById(new TimescodeFindpsEntity(phone,date,times));
flag = 1;
}
}else {
String date = DateUtils.getDay();
int times = 1;
tFindpsDao.insert(new TimescodeFindpsEntity(phone,date,times));
}
if (flag==0){
throw new RRException("同一手机号每日最多操作三次!");
}else {
//调用短信接口发送短信
String res = "res";
String account = "gs1cn";
String password = "test@2021";
String userid = "9220";
String content="您在中国物品编码中心网站注册的用户昵称为:"+userName+", 密码为:"+uuid+",请妥善保管!同一手机号每日最多操作三次!【中国物品编码中心】";
String post= VerificationCodeUtils.sendVerificationCode(res,account,password,userid,content,phone);
Document document = DocumentHelper.parseText(post);
// 获取returnstatus节点对象
String returnstatus = document.selectSingleNode("//returnstatus").getText();
//获取发送结果
if (returnstatus.equals("Success")){
//保存验证码信息log_message表
state="发送成功";
}else if (returnstatus.equals("Faild")){
state="发送失败";
throw new RRException("验证码发送失败:"+ document.selectSingleNode("//message").getText());
}else {
state="发送失败";
throw new RRException("密码发送失败,请稍后再试!");
}
}
}else {
throw new RRException("手机号输入错误!");
}
}else {
throw new RRException("用户名不存在!");
}
} catch (RRException e) {
e.printStackTrace();
throw new RRException(e.getLocalizedMessage());
}catch (Exception e){
throw new RRException("密码发送失败");
}
}
@Override
public void forgotPasswordEmiail(Map<String, Object> params) {
try {
String state="";
String userName = params.get("userName")==null?"":String.valueOf(params.get("userName"));
String emiail = params.get("e-miail")==null?"":String.valueOf(params.get("e-miail"));
if ("".equals(userName)){
throw new RRException("请输入用户名!");
}
if ("".equals(emiail)){
throw new RRException("请输入邮箱!");
}
if ("admin".equals(userName)){ //管理员不能修改密码
throw new RRException("用户名不存在!");
}
MemberEntity memberEntity = memberDao.selectByName(userName);
if (memberEntity!=null){
Integer id = memberEntity.getId();
String emiail_DB = memberEntity.getEmail();
if (emiail.equals(emiail_DB)){
//获取6位随机字符串
String uuid = UUID.randomUUID().toString().replaceAll("-", "").substring(0,6);
//更换密码
memberDao.updateByIdPassWord(id,MD5Util.md5Encrypt32Upper(uuid));
//发送邮箱
String body="您在中国物品编码中心网站注册的用户昵称为:"+userName+", 密码为:"+uuid+",请妥善保管!";
MailUtlis.sendMail(userName,"中国编码用户密码找回",body,emiail);
}else {
throw new RRException("邮箱输入错误!");
}
}else {
throw new RRException("用户名不存在!");
}
} catch (RRException e) {
e.printStackTrace();
throw new RRException(e.getLocalizedMessage());
}catch (Exception e){
throw new RRException("密码发送失败");
}
}
public static void main(String[] args) throws Exception {
Properties props = new Properties(); // 用于连接邮件服务器的参数配置(发送邮件时才需要用到)
Session session= Session.getInstance(props);
}
}
\ No newline at end of file
package io.office.modules.manage.utils;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
public class HttpUtlis {
/**
* application/x-www-form-urlencoded 格式
* 发送post 数据
* @param urls
* @return
*/
public static String sendPostXwwwformurlencoded(String urls, NameValuePair[] data) {
try {
PostMethod postMethod = null;
postMethod = new PostMethod(urls) ;
postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8") ;
//参数设置,需要注意的就是里边不能传NULL,要传空字符串
postMethod.setRequestBody(data);
org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
int response = httpClient.executeMethod(postMethod); // 执行POST方法
String result = postMethod.getResponseBodyAsString() ;
return result;
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}
......@@ -48,7 +48,7 @@ public class MD5Util {
* @param string 需要进行MD5加密的字符串
* @return 加密后的字符串(小写)
*/
public static String md5Encrypt32Lower(String string) {
/* public static String md5Encrypt32Lower(String string) {
byte[] hash;
try {
//创建一个MD5算法对象,并获得MD5字节数组,16*8=128位
......@@ -66,7 +66,7 @@ public class MD5Util {
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString().toLowerCase();
}
}*/
/**
* 将二进制字节数组转换为十六进制字符串
......@@ -105,9 +105,9 @@ public class MD5Util {
}
public static void main(String[] args) {
String md5Encrypt32Lower = MD5Util.md5Encrypt32Lower("0123456789");
// String md5Encrypt32Lower = MD5Util.md5Encrypt32Lower("0123456789");
String md5Encrypt32Upper1 = MD5Util.md5Encrypt32Upper("1234");
System.out.println(md5Encrypt32Lower);
//System.out.println(md5Encrypt32Lower);
System.out.println(md5Encrypt32Upper1);
}
}
\ No newline at end of file
package io.office.modules.manage.utils;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;
public class MailUtlis {
// 发件人的 邮箱 和 密码(替换为自己的邮箱和密码)
// PS: 某些邮箱服务器为了增加邮箱本身密码的安全性,给 SMTP 客户端设置了独立密码(有的邮箱称为“授权码”),
// 对于开启了独立密码的邮箱, 这里的邮箱密码必需使用这个独立密码(授权码)。
public static String myEmailAccount = "1476171348@qq.com";
//public static String myEmailAccount = "webmaster@ancc.org.cn";
public static String myEmailPassword = "esaiwmaygjpdfjge";
// 发件人邮箱的 SMTP 服务器地址, 必须准确, 不同邮件服务器地址不同, 一般(只是一般, 绝非绝对)格式为: smtp.xxx.com
// 网易126邮箱的 SMTP 服务器地址为: smtp.126.com qq为: smtp.qq.com
public static String myEmailSMTPHost = "smtp.qq.com";
// 收件人邮箱(替换为自己知道的有效邮箱)
//public static String receiveMailAccount = "13159872863@163.com";
public static void sendMail(String userName,String theme,String body,String receiveMailAccount) throws Exception {
/*public static void main(String[] args) throws Exception {*/
// 1. 创建参数配置, 用于连接邮件服务器的参数配置
Properties props = new Properties(); // 参数配置
props.setProperty("mail.transport.protocol", "smtp"); // 使用的协议(JavaMail规范要求)
props.setProperty("mail.smtp.host", myEmailSMTPHost); // 发件人的邮箱的 SMTP 服务器地址
props.setProperty("mail.smtp.auth", "true"); // 需要请求认证
// PS: 某些邮箱服务器要求 SMTP 连接需要使用 SSL 安全认证 (为了提高安全性, 邮箱支持SSL连接, 也可以自己开启),
// 如果无法连接邮件服务器, 仔细查看控制台打印的 log, 如果有有类似 “连接失败, 要求 SSL 安全连接” 等错误,
// 取消下面 /* ... */ 之间的注释代码, 开启 SSL 安全连接。
/*
// SMTP 服务器的端口 (非 SSL 连接的端口一般默认为 25, 可以不添加, 如果开启了 SSL 连接,
// 需要改为对应邮箱的 SMTP 服务器的端口, 具体可查看对应邮箱服务的帮助,
// QQ邮箱的SMTP(SLL)端口为465或587, 其他邮箱自行去查看)
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);
*/
// 2. 根据配置创建会话对象, 用于和邮件服务器交互
Session session = Session.getInstance(props);
// 设置为debug模式, 可以查看详细的发送 log
session.setDebug(true);
// 3. 创建一封邮件
MimeMessage message = createMimeMessage(session, myEmailAccount, receiveMailAccount,userName,theme,body);
// 4. 根据 Session 获取邮件传输对象
Transport transport = session.getTransport();
// 5. 使用 邮箱账号 和 密码 连接邮件服务器, 这里认证的邮箱必须与 message 中的发件人邮箱一致, 否则报错
//
// PS_01: 如果连接服务器失败, 都会在控制台输出相应失败原因的log。
// 仔细查看失败原因, 有些邮箱服务器会返回错误码或查看错误类型的链接,
// 根据给出的错误类型到对应邮件服务器的帮助网站上查看具体失败原因。
//
// PS_02: 连接失败的原因通常为以下几点, 仔细检查代码:
// (1) 邮箱没有开启 SMTP 服务;
// (2) 邮箱密码错误, 例如某些邮箱开启了独立密码;
// (3) 邮箱服务器要求必须要使用 SSL 安全连接;
// (4) 请求过于频繁或其他原因, 被邮件服务器拒绝服务;
// (5) 如果以上几点都确定无误, 到邮件服务器网站查找帮助。
//
transport.connect(myEmailAccount, myEmailPassword);
// 6. 发送邮件, 发到所有的收件地址, message.getAllRecipients() 获取到的是在创建邮件对象时添加的所有收件人, 抄送人, 密送人
transport.sendMessage(message, message.getAllRecipients());
// 7. 关闭连接
transport.close();
}
/**
* 创建一封只包含文本的简单邮件
*
* @param session 和服务器交互的会话
* @param sendMail 发件人邮箱
* @param receiveMail 收件人邮箱
* @return
* @throws Exception
*/
public static MimeMessage createMimeMessage(Session session, String sendMail, String receiveMail,String userName,String theme,String body) throws Exception {
// 1. 创建一封邮件
MimeMessage message = new MimeMessage(session);
// 2. From: 发件人
message.setFrom(new InternetAddress(sendMail, "中国编码", "UTF-8"));
// 3. To: 收件人(可以增加多个收件人、抄送、密送)
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMail, userName+"用户", "UTF-8"));
// 4. Subject: 邮件主题
message.setSubject(theme, "UTF-8");
// 5. Content: 邮件正文(可以使用html标签)
message.setContent(body, "text/html;charset=UTF-8");
// 6. 设置发件时间
message.setSentDate(new Date());
// 7. 保存设置
message.saveChanges();
return message;
}
}
\ No newline at end of file
package io.office.modules.manage.utils;
import org.apache.commons.httpclient.NameValuePair;
//发送验证码工具类
public class VerificationCodeUtils {
/**
* 发送手机短信
* @param res
* @param account 用户名
* @param password 密码
* @param userid 用户ID
* @param content 短信内容
* @param phone 手机号
* @return
*/
public static String sendVerificationCode(String res, String account, String password, String userid, String content, String phone) {
NameValuePair[] data = {
new NameValuePair("action", "send"),
new NameValuePair("userid", userid),
new NameValuePair("account", account),
new NameValuePair("password", password),
new NameValuePair("mobile", phone),
new NameValuePair("content", content),
new NameValuePair("sendTime", DateUtils.getDay()),
new NameValuePair("mobilenumber", "2"),
new NameValuePair("countnumber", "2"),
new NameValuePair("telephonenumber", "0"),
};
return HttpUtlis.sendPostXwwwformurlencoded("http://39.106.204.178:8888/sms.aspx",data);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment